Module: Typekit::Helper
- Defined in:
- lib/typekit/helper.rb
Class Method Summary collapse
-
.constantize(object) ⇒ Symbol
Constructs a symbol suitable for looking up modules among the constants defined in a module.
-
.extract_array!(array) ⇒ Array
Removes and returns the last element of the given array if that element is an array; otherwise, a new array is returned.
-
.extract_hash!(array) ⇒ Hash
Removes and returns the last element of the given array if that element is a hash; otherwise, a new hash is returned.
-
.pluralize(word) ⇒ String
Returns the plural form of the given word.
-
.singularize(word) ⇒ String
Returns the singular form of the given word.
-
.symbolize_keys(hash) ⇒ Hash
Copies the given hash while converting its keys into symbols.
-
.tokenize(object, options = {}) ⇒ Object
Constructs a symbolic representation of the given object.
Class Method Details
.constantize(object) ⇒ Symbol
Constructs a symbol suitable for looking up modules among the constants defined in a module.
38 39 40 |
# File 'lib/typekit/helper.rb', line 38 def self.constantize(object) object.to_s.sub(/^.*::/, '').capitalize.to_sym end |
.extract_array!(array) ⇒ Array
Removes and returns the last element of the given array if that element is an array; otherwise, a new array is returned.
74 75 76 |
# File 'lib/typekit/helper.rb', line 74 def self.extract_array!(array) array.last.is_a?(Array) ? array.pop : {} end |
.extract_hash!(array) ⇒ Hash
Removes and returns the last element of the given array if that element is a hash; otherwise, a new hash is returned.
65 66 67 |
# File 'lib/typekit/helper.rb', line 65 def self.extract_hash!(array) array.last.is_a?(Hash) ? array.pop : {} end |
.pluralize(word) ⇒ String
Returns the plural form of the given word.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/typekit/helper.rb', line 7 def self.pluralize(word) case word when /^.*s$/ word when /^(?<root>.*)y$/ "#{Regexp.last_match(:root)}ies" else "#{word}s" end end |
.singularize(word) ⇒ String
Returns the singular form of the given word.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/typekit/helper.rb', line 22 def self.singularize(word) case word when /^(?<root>.*)ies$/ "#{Regexp.last_match(:root)}y" when /^(?<root>.*)s$/ Regexp.last_match(:root) else word end end |
.symbolize_keys(hash) ⇒ Hash
Copies the given hash while converting its keys into symbols.
56 57 58 |
# File 'lib/typekit/helper.rb', line 56 def self.symbolize_keys(hash) Hash[hash.map { |k, v| [k.to_sym, v] }] end |
.tokenize(object, options = {}) ⇒ Object
Constructs a symbolic representation of the given object.
47 48 49 50 |
# File 'lib/typekit/helper.rb', line 47 def self.tokenize(object, = {}) name = object.to_s.sub(/^.*::/, '').downcase (.fetch(:pluralize, true) ? pluralize(name) : name).to_sym end |