Module: Helper

Defined in:
lib/greenbutton/helpers.rb

Defined Under Namespace

Classes: Rule

Constant Summary collapse

SERVICE_KIND_HASH =
{
  "0" => :electricity,
  "1" => :gas,
  "2" => :water,
  "3" => :time,
  "4" => :heat,
  "5" => :refuse,
  "6" => :sewerage,
  "7" => :rates,
  "8" => :tv_licence,
  "9" => :internet   
}

Class Method Summary collapse

Class Method Details

.pluralize(name) ⇒ Object


35
36
37
38
# File 'lib/greenbutton/helpers.rb', line 35

def self.pluralize(name)
  name = name.to_s.sub(/(y)$/i, 'ie')
  name += 's'
end

.translate(type, input_to_translate) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/greenbutton/helpers.rb', line 17

def self.translate(type, input_to_translate)
    if !input_to_translate.nil?
      case type
      when :ServiceKind
        translated = SERVICE_KIND_HASH[input_to_translate] || input_to_translate
      when :datetime
        translated = DateTime.parse(input_to_translate).to_time.utc
      when :unix_time
        translated = Time.at(input_to_translate.to_i).utc
      when :integer
        translated = input_to_translate.to_i
      else
        translated = input_to_translate
      end
      translated
    end
end

.underscore(camel_cased_word) ⇒ Object


41
42
43
44
45
46
47
# File 'lib/greenbutton/helpers.rb', line 41

def self.underscore(camel_cased_word)
 camel_cased_word.to_s.gsub(/::/, '/').
 gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
 gsub(/([a-z\d])([A-Z])/,'\1_\2').
 tr("-", "_").
 downcase
end