Module: Balanced::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/balanced_ach/utils.rb

Instance Method Summary collapse

Instance Method Details

#camelize(underscored_word) ⇒ Object



3
4
5
# File 'lib/balanced_ach/utils.rb', line 3

def camelize underscored_word
  underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
end

#classify(table_name) ⇒ Object



7
8
9
# File 'lib/balanced_ach/utils.rb', line 7

def classify table_name
  camelize singularize(table_name.to_s.sub(/.*\./, ''))
end

#demodulize(class_name_in_module) ⇒ Object



11
12
13
# File 'lib/balanced_ach/utils.rb', line 11

def demodulize class_name_in_module
  class_name_in_module.to_s.sub(/^.*::/, '')
end

#hash_with_indifferent_read_access(base = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/balanced_ach/utils.rb', line 33

def hash_with_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 = hash_with_indifferent_read_access value
    elsif value.respond_to? :each
      value.map! do |v|
        if v.is_a? Hash
           v = hash_with_indifferent_read_access v
        end
        v
      end
    end
    indifferent[key.to_s] = value
  end
  indifferent
end

#pluralize(word) ⇒ Object



15
16
17
# File 'lib/balanced_ach/utils.rb', line 15

def pluralize word
  word.to_s.sub(/([^s])$/, '\1s')
end

#singularize(word) ⇒ Object



19
20
21
# File 'lib/balanced_ach/utils.rb', line 19

def singularize word
  word.to_s.sub(/s$/, '').sub(/ie$/, 'y')
end

#stringify_keys!(hash) ⇒ Object



53
54
55
56
57
58
# File 'lib/balanced_ach/utils.rb', line 53

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



23
24
25
26
27
28
29
30
31
# File 'lib/balanced_ach/utils.rb', line 23

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