Module: FlightStats::Helper

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

Instance Method Summary collapse

Instance Method Details

#camelize(underscored_word) ⇒ Object



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

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

#classify(value) ⇒ Object



7
8
9
# File 'lib/flightstats/helper.rb', line 7

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

#demodulize(class_name_in_module) ⇒ Object



11
12
13
# File 'lib/flightstats/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



37
38
39
40
41
# File 'lib/flightstats/helper.rb', line 37

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/flightstats/helper.rb', line 15

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

#singularize(word) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/flightstats/helper.rb', line 19

def singularize word
  if word.to_s =~ /ses$/
    word.to_s.sub(/es$/, '')
  else
    word.to_s.sub(/s$/, '').sub(/ie$/, 'y')
  end
end

#stringify_keys!(hash) ⇒ Object



43
44
45
46
47
48
# File 'lib/flightstats/helper.rb', line 43

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



27
28
29
30
31
32
33
34
35
# File 'lib/flightstats/helper.rb', line 27

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