Module: Recurly::Helper

Extended by:
Helper
Included in:
Helper
Defined in:
lib/recurly/helper.rb

Instance Method Summary collapse

Instance Method Details

#camelize(underscored_word) ⇒ Object



3
4
5
# File 'lib/recurly/helper.rb', line 3

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

#classify(table_name) ⇒ Object



7
8
9
# File 'lib/recurly/helper.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/recurly/helper.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
# File 'lib/recurly/helper.rb', line 33

def hash_with_indifferent_read_access base = {}
  indifferent = Hash.new { |hash, key| hash[key.to_s] if key.is_a? Symbol }
  base.each_pair { |key, value| indifferent[key.to_s] = value }
  indifferent
end

#pluralize(word) ⇒ Object



15
16
17
# File 'lib/recurly/helper.rb', line 15

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

#singularize(word) ⇒ Object



19
20
21
# File 'lib/recurly/helper.rb', line 19

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

#underscore(camel_cased_word) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/recurly/helper.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