Module: SyncwiseApi::Inflector

Extended by:
Inflector
Included in:
Inflector
Defined in:
lib/syncwise_api/ext/inflector_methods.rb,
lib/syncwise_api/ext/inflector/inflections.rb

Defined Under Namespace

Classes: Inflections

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.camelize(term, uppercase_first_letter = true) ⇒ Object

By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to :lower then camelize produces lowerCamelCase.

camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.

Examples:

"active_model".camelize                # => "ActiveModel"
"active_model".camelize(:lower)        # => "activeModel"
"active_model/errors".camelize         # => "ActiveModel::Errors"
"active_model/errors".camelize(:lower) # => "activeModel::Errors"

As a rule of thumb you can think of camelize as the inverse of underscore, though there are cases where that does not hold:

"SSLError".underscore.camelize # => "SslError"


49
50
51
52
53
54
55
56
57
# File 'lib/syncwise_api/ext/inflector_methods.rb', line 49

def camelize(term, uppercase_first_letter = true)
  string = term.to_s
  if uppercase_first_letter
    string = string.sub(/^[a-z\d]*/) { inflections.acronyms[$&] || $&.capitalize }
  else
    string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { $&.downcase }
  end
  string.gsub(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" }.gsub('/', '::')
end

.dehumanize(word) ⇒ Object

downcase words, and replace ‘ ’ with ‘_’. Example ‘Big Bob’.dehumanize => ‘big_bob’



8
9
10
11
12
13
14
15
16
17
# File 'lib/syncwise_api/ext/inflector_methods.rb', line 8

def dehumanize(word)
  # should be OK to remove the respond_to? as long as we ensure we call this only on Strings or things that can be
  # cast to Strings
  #if word.respond_to?(:to_s)
  dh_word = word.to_s.dup
  dh_word.gsub!(/ /, '_')
  dh_word.downcase!
  dh_word
  #end
end

.underscore(camel_cased_word) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/syncwise_api/ext/inflector_methods.rb', line 19

def underscore(camel_cased_word)
  # should be OK to remove the respond_to? as long as we ensure we call this only on Strings or things that can be
  # cast to Strings
  #if camel_cased_word.respond_to?(:to_s)
  word = camel_cased_word.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/(?:([A-Za-z\d])|^)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1}#{$1 && '_'}#{$2.downcase}" }
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
  #end
end

Instance Method Details

#inflectionsObject

Yields a singleton instance of Inflector::Inflections so you can specify additional inflector rules.

Example:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "rails"
end


169
170
171
172
173
174
175
# File 'lib/syncwise_api/ext/inflector/inflections.rb', line 169

def inflections
  if block_given?
    yield Inflections.instance
  else
    Inflections.instance
  end
end