Class: Xignite::Hash

Inherits:
Hash
  • Object
show all
Extended by:
Helpers
Defined in:
lib/xignite/hash.rb

Class Method Summary collapse

Methods included from Helpers

constantize, underscore

Class Method Details

.build(data, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/xignite/hash.rb', line 5

def self.build(data, options={})
  new.tap do |obj|
    data.each do |k, v|
      v = self.build(v, options) if v.class.name == 'Hash'
      obj[underscore(k)] = case v
        when /^[\d,\.]+$/
          v.to_f
        when /^[\d,\/]+$/
          Date.strptime(v, '%m/%d/%Y')
        when /^\d+:\d+:\d+ [APM]{2}$/
          tz = ::TZInfo::Timezone.get(options[:timezone] || 'GMT')
          tz.local_to_utc(DateTime.strptime("#{data['Date']} #{v}", "#{DATE_FORMAT} #{TIME_FORMAT}"))
        else
          v
      end
    end
  end
end