Module: ElvantoAPI::Utils
Instance Method Summary collapse
- #callable(callable_or_not) ⇒ Object
- #camelize(underscored_word) ⇒ Object
- #classify(table_name) ⇒ Object
- #demodulize(class_name_in_module) ⇒ Object
- #extract_href_from_object(object) ⇒ Object
- #indifferent_read_access(base = {}) ⇒ Object
- #pluralize(word) ⇒ Object
- #singularize(word) ⇒ Object
- #stringify_keys!(hash) ⇒ Object
- #underscore(camel_cased_word) ⇒ Object
Instance Method Details
#callable(callable_or_not) ⇒ Object
8 9 10 |
# File 'lib/elvanto/utils.rb', line 8 def callable( callable_or_not ) callable_or_not.respond_to?(:call) ? callable_or_not : lambda { callable_or_not } end |
#camelize(underscored_word) ⇒ Object
12 13 14 |
# File 'lib/elvanto/utils.rb', line 12 def camelize(underscored_word) underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase } end |
#classify(table_name) ⇒ Object
16 17 18 19 20 |
# File 'lib/elvanto/utils.rb', line 16 def classify(table_name) class_name = camelize singularize(table_name.to_s.sub(/.*\./, '')) class_name = CLASS_MAPPING[class_name] if CLASS_MAPPING.keys.include? class_name return class_name end |
#demodulize(class_name_in_module) ⇒ Object
22 23 24 |
# File 'lib/elvanto/utils.rb', line 22 def demodulize(class_name_in_module) class_name_in_module.to_s.sub(/^.*::/, '') end |
#extract_href_from_object(object) ⇒ Object
46 47 48 |
# File 'lib/elvanto/utils.rb', line 46 def extract_href_from_object(object) object.respond_to?(:href) ? object.href : object end |
#indifferent_read_access(base = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/elvanto/utils.rb', line 50 def indifferent_read_access(base = {}) indifferent = Hash.new do |hash, key| hash[key.to_s] if key.is_a? Symbol end base.each_pair do |key, value| if value.is_a? Hash value = indifferent_read_access value elsif value.respond_to? :each if value.respond_to? :map! value.map! do |v| if v.is_a? Hash v = indifferent_read_access v end v end else value.map do |v| if v.is_a? Hash v = indifferent_read_access v end v end end end indifferent[key.to_s] = value end indifferent end |
#pluralize(word) ⇒ Object
26 27 28 29 |
# File 'lib/elvanto/utils.rb', line 26 def pluralize(word) return "people" if word == "person" word.to_s.pluralize end |
#singularize(word) ⇒ Object
31 32 33 34 |
# File 'lib/elvanto/utils.rb', line 31 def singularize(word) return "person" if word == "people" word.to_s.sub(/s$/, '').sub(/ie$/, 'y') end |
#stringify_keys!(hash) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/elvanto/utils.rb', line 79 def stringify_keys!(hash) hash.keys.each do |key| stringify_keys! hash[key] if hash[key].is_a? Hash hash[key.to_s] = hash.delete key if key.is_a? Symbol end end |
#underscore(camel_cased_word) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/elvanto/utils.rb', line 36 def underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') word.gsub!(/([a-z\d])([A-Z])/, '\1_\2') word.tr! '-', '_' word.downcase! word end |