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 Connectors
|
||||||
module Helpers
|
module Helpers
|
||||||
module DeleteCommand
|
module DeleteCommand
|
||||||
@ -9,7 +7,7 @@ module Connectors
|
|||||||
# We need this alias to forward methods from MongoConnector to resources connectors.
|
# We need this alias to forward methods from MongoConnector to resources connectors.
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
resource_name = StringHelper.underscore_class(base)
|
resource_name = base.to_s.underscore_class
|
||||||
method_name = "#{resource_name}_delete".to_sym
|
method_name = "#{resource_name}_delete".to_sym
|
||||||
alias_method method_name, :delete
|
alias_method method_name, :delete
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
require 'lib/string_helper'
|
|
||||||
|
|
||||||
module Connectors
|
module Connectors
|
||||||
module Helpers
|
module Helpers
|
||||||
module InsertCommand
|
module InsertCommand
|
||||||
@ -9,7 +7,7 @@ module Connectors
|
|||||||
# We need this alias to forward methods from MongoConnector to resources connectors.
|
# We need this alias to forward methods from MongoConnector to resources connectors.
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
resource_name = StringHelper.underscore_class(base)
|
resource_name = base.to_s.underscore_class
|
||||||
method_name = "#{resource_name}_insert".to_sym
|
method_name = "#{resource_name}_insert".to_sym
|
||||||
alias_method method_name, :insert
|
alias_method method_name, :insert
|
||||||
end
|
end
|
||||||
@ -24,7 +22,7 @@ module Connectors
|
|||||||
rescue Mongo::OperationFailure => e
|
rescue Mongo::OperationFailure => e
|
||||||
# exception's message doesn't always start from error code
|
# exception's message doesn't always start from error code
|
||||||
if e.message =~ /11000/
|
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}'")
|
raise InvalidRecord.new("Duplicate key error: #{resource_name} with id '#{record.id}'")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
require 'lib/string_helper'
|
|
||||||
|
|
||||||
module Connectors
|
module Connectors
|
||||||
module Helpers
|
module Helpers
|
||||||
module ListCommand
|
module ListCommand
|
||||||
@ -9,9 +7,7 @@ module Connectors
|
|||||||
# We need this alias to forward methods from MongoConnector to resources connectors.
|
# We need this alias to forward methods from MongoConnector to resources connectors.
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
resource_name = StringHelper.underscore_class(base).to_sym
|
alias_method base.to_s.underscore_class.pluralize, :list
|
||||||
method_name = StringHelper.pluralize(resource_name)
|
|
||||||
alias_method method_name, :list
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# query options is needed, for example, for fields limiting
|
# query options is needed, for example, for fields limiting
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
require 'lib/string_helper'
|
|
||||||
|
|
||||||
module Connectors
|
module Connectors
|
||||||
module Helpers
|
module Helpers
|
||||||
module ShowCommand
|
module ShowCommand
|
||||||
@ -9,7 +7,7 @@ module Connectors
|
|||||||
# We need this alias to forward methods from MongoConnector to resources connectors.
|
# We need this alias to forward methods from MongoConnector to resources connectors.
|
||||||
|
|
||||||
def self.included(base)
|
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
|
alias_method method_name, :show
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,3 @@
|
|||||||
require 'lib/string_helper'
|
|
||||||
|
|
||||||
module Connectors
|
module Connectors
|
||||||
module Helpers
|
module Helpers
|
||||||
module UpdateCommand
|
module UpdateCommand
|
||||||
@ -8,8 +6,7 @@ module Connectors
|
|||||||
# We need second method name to forward methods from MongoConnector to resources connectors.
|
# We need second method name to forward methods from MongoConnector to resources connectors.
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
resource_name = StringHelper.underscore_class(base)
|
method_name = "#{base.to_s.underscore_class}_update".to_sym
|
||||||
method_name = "#{resource_name}_update".to_sym
|
|
||||||
alias_method method_name, :update
|
alias_method method_name, :update
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
require 'lib/string_helper'
|
|
||||||
require 'db/mongo/models/stack_template/stack_template_factory'
|
require 'db/mongo/models/stack_template/stack_template_factory'
|
||||||
|
|
||||||
module Devops
|
module Devops
|
||||||
@ -6,7 +5,7 @@ module Devops
|
|||||||
class Base
|
class Base
|
||||||
|
|
||||||
def id
|
def id
|
||||||
StringHelper.underscore_class(self.class)
|
self.class.to_s.underscore_class
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_hash
|
def to_hash
|
||||||
|
|||||||
@ -6,4 +6,22 @@ class String
|
|||||||
def blank?
|
def blank?
|
||||||
empty?
|
empty?
|
||||||
end
|
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
|
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