Class: Chameleon::Support

Inherits:
Object
  • Object
show all
Defined in:
lib/chameleon/support.rb

Class Method Summary collapse

Class Method Details

.pluralize(string) ⇒ Object

super naive yes, but effective for the use case here; special cases can be added if needed



35
36
37
38
# File 'lib/chameleon/support.rb', line 35

def self.pluralize(string)
  string += 's'
  string
end

.underscore(string) ⇒ Object



25
26
27
28
29
30
# File 'lib/chameleon/support.rb', line 25

def self.underscore(string)
  string.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze)
  string.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze)
  string.downcase!
  string
end

.value_of(value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/chameleon/support.rb', line 2

def self.value_of(value)
  value = case value
  when Array then value.map { |v| value_of(v) }.compact
  when Hash then value.dup.tap { |v| v.each_key { |k| v[k] = value_of(v[k]) } }
  when String
    value = value.strip

    case value
    when '' then nil
    when /^true$/i then true
    when /^false$/i then false
    when '$now' then Time.now
    when /^\d{4}-(0[1-9]|1[0-2])-([0-2][1-9]|[1-3]0|3[01])/ then DateTime.parse(value).to_time
    when /^-?\d+\.\d+(e\d+)?$/ then value.to_f
    when /^-?\d+$/ then value.to_i
    else value
    end
  else value
  end

  value
end