Module: Datmachine::Utils
Instance Method Summary collapse
- #assert_required_keys(hash, params) ⇒ Object
- #assert_valid_keys(hash, *valid_keys) ⇒ Object
- #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
#assert_required_keys(hash, params) ⇒ Object
84 85 86 87 88 |
# File 'lib/datmachine/utils.rb', line 84 def assert_required_keys(hash, params) params[:required] ||= [] pending_keys = params[:required] - hash.keys raise(ArgumentError, "Required key(s) not present: #{pending_keys.join(', ')}") unless pending_keys.empty? end |
#assert_valid_keys(hash, *valid_keys) ⇒ Object
79 80 81 82 |
# File 'lib/datmachine/utils.rb', line 79 def assert_valid_keys(hash, *valid_keys) unknown_keys = hash.keys - [valid_keys].flatten raise(ArgumentError, "Unknown key(s): #{unknown_keys.join(', ')}") unless unknown_keys.empty? end |
#callable(callable_or_not) ⇒ Object
5 6 7 |
# File 'lib/datmachine/utils.rb', line 5 def callable( callable_or_not ) callable_or_not.respond_to?(:call) ? callable_or_not : lambda { callable_or_not } end |
#camelize(underscored_word) ⇒ Object
9 10 11 |
# File 'lib/datmachine/utils.rb', line 9 def camelize(underscored_word) underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase } end |
#classify(table_name) ⇒ Object
13 14 15 |
# File 'lib/datmachine/utils.rb', line 13 def classify(table_name) camelize singularize(table_name.to_s.sub(/.*\./, '')) end |
#demodulize(class_name_in_module) ⇒ Object
17 18 19 |
# File 'lib/datmachine/utils.rb', line 17 def demodulize(class_name_in_module) class_name_in_module.to_s.sub(/^.*::/, '') end |
#extract_href_from_object(object) ⇒ Object
39 40 41 |
# File 'lib/datmachine/utils.rb', line 39 def extract_href_from_object(object) object.respond_to?(:href) ? object.href : object end |
#indifferent_read_access(base = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/datmachine/utils.rb', line 43 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
21 22 23 |
# File 'lib/datmachine/utils.rb', line 21 def pluralize(word) word.to_s.sub(/([^s])$/, '\1s') end |
#singularize(word) ⇒ Object
25 26 27 |
# File 'lib/datmachine/utils.rb', line 25 def singularize(word) word.to_s.sub(/s$/, '').sub(/ie$/, 'y') end |
#stringify_keys!(hash) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/datmachine/utils.rb', line 72 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
29 30 31 32 33 34 35 36 37 |
# File 'lib/datmachine/utils.rb', line 29 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 |