move string helper methods to core extensions
This commit is contained in:
parent
87811cca25
commit
83cbfd7a48
@ -1,5 +1,3 @@
|
||||
require 'lib/string_helper'
|
||||
|
||||
module Connectors
|
||||
module Helpers
|
||||
module DeleteCommand
|
||||
@ -9,7 +7,7 @@ module Connectors
|
||||
# We need this alias to forward methods from MongoConnector to resources connectors.
|
||||
|
||||
def self.included(base)
|
||||
resource_name = StringHelper.underscore_class(base)
|
||||
resource_name = base.to_s.underscore_class
|
||||
method_name = "#{resource_name}_delete".to_sym
|
||||
alias_method method_name, :delete
|
||||
end
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
require 'lib/string_helper'
|
||||
|
||||
module Connectors
|
||||
module Helpers
|
||||
module InsertCommand
|
||||
@ -9,7 +7,7 @@ module Connectors
|
||||
# We need this alias to forward methods from MongoConnector to resources connectors.
|
||||
|
||||
def self.included(base)
|
||||
resource_name = StringHelper.underscore_class(base)
|
||||
resource_name = base.to_s.underscore_class
|
||||
method_name = "#{resource_name}_insert".to_sym
|
||||
alias_method method_name, :insert
|
||||
end
|
||||
@ -24,7 +22,7 @@ module Connectors
|
||||
rescue Mongo::OperationFailure => e
|
||||
# exception's message doesn't always start from error code
|
||||
if e.message =~ /11000/
|
||||
resource_name = StringHelper.underscore_class(record.class)
|
||||
resource_name = record.class.to_s.underscore_class
|
||||
raise InvalidRecord.new("Duplicate key error: #{resource_name} with id '#{record.id}'")
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
require 'lib/string_helper'
|
||||
|
||||
module Connectors
|
||||
module Helpers
|
||||
module ListCommand
|
||||
@ -9,9 +7,7 @@ module Connectors
|
||||
# We need this alias to forward methods from MongoConnector to resources connectors.
|
||||
|
||||
def self.included(base)
|
||||
resource_name = StringHelper.underscore_class(base).to_sym
|
||||
method_name = StringHelper.pluralize(resource_name)
|
||||
alias_method method_name, :list
|
||||
alias_method base.to_s.underscore_class.pluralize, :list
|
||||
end
|
||||
|
||||
# query options is needed, for example, for fields limiting
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
require 'lib/string_helper'
|
||||
|
||||
module Connectors
|
||||
module Helpers
|
||||
module ShowCommand
|
||||
@ -9,7 +7,7 @@ module Connectors
|
||||
# We need this alias to forward methods from MongoConnector to resources connectors.
|
||||
|
||||
def self.included(base)
|
||||
method_name = StringHelper.underscore_class(base).to_sym
|
||||
method_name = base.to_s.underscore_class.to_sym
|
||||
alias_method method_name, :show
|
||||
end
|
||||
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
require 'lib/string_helper'
|
||||
|
||||
module Connectors
|
||||
module Helpers
|
||||
module UpdateCommand
|
||||
@ -8,8 +6,7 @@ module Connectors
|
||||
# We need second method name to forward methods from MongoConnector to resources connectors.
|
||||
|
||||
def self.included(base)
|
||||
resource_name = StringHelper.underscore_class(base)
|
||||
method_name = "#{resource_name}_update".to_sym
|
||||
method_name = "#{base.to_s.underscore_class}_update".to_sym
|
||||
alias_method method_name, :update
|
||||
end
|
||||
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
require 'lib/string_helper'
|
||||
require 'db/mongo/models/stack_template/stack_template_factory'
|
||||
|
||||
module Devops
|
||||
@ -6,7 +5,7 @@ module Devops
|
||||
class Base
|
||||
|
||||
def id
|
||||
StringHelper.underscore_class(self.class)
|
||||
self.class.to_s.underscore_class
|
||||
end
|
||||
|
||||
def to_hash
|
||||
|
||||
@ -6,4 +6,22 @@ class String
|
||||
def blank?
|
||||
empty?
|
||||
end
|
||||
|
||||
# from ActiveSupport
|
||||
def underscore
|
||||
gsub(/::/, '/').
|
||||
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
||||
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
||||
tr("-", "_").
|
||||
downcase
|
||||
end
|
||||
|
||||
def underscore_class
|
||||
split('::').last.underscore
|
||||
end
|
||||
|
||||
# rough simplification
|
||||
def pluralize
|
||||
"#{self}s"
|
||||
end
|
||||
end
|
||||
@ -1,35 +0,0 @@
|
||||
module StringHelper
|
||||
extend self
|
||||
|
||||
# from Rails' ActiveSupport
|
||||
def underscore(string)
|
||||
string.gsub(/::/, '/').
|
||||
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
||||
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
||||
tr("-", "_").
|
||||
downcase
|
||||
end
|
||||
|
||||
def underscore_class(klass, without_ancestors=true)
|
||||
class_name = if without_ancestors
|
||||
klass.to_s.split('::').last
|
||||
else
|
||||
klass.to_s
|
||||
end
|
||||
StringHelper.underscore(class_name)
|
||||
end
|
||||
|
||||
# from Rails' ActiveSupport
|
||||
def camelize(term)
|
||||
string = term.to_s
|
||||
string = string.sub(/^[a-z\d]*/) { $&.capitalize }
|
||||
string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
|
||||
string.gsub!(/\//, '::')
|
||||
string
|
||||
end
|
||||
|
||||
# rough simplification
|
||||
def pluralize(string)
|
||||
"#{string}s"
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user