Sha256: 7e63dd8a705960df0fe3b5ce63db1bd9fc05b78fac79d34b87d4c969b867b155
Contents?: true
Size: 1.72 KB
Versions: 3
Compression:
Stored size: 1.72 KB
Contents
class Hooloo::MozartHash class << self def field_mapping(fields) fields = [fields] unless fields.is_a? Array fields.map do |field| if field.is_a?(Hash) arr = field.to_a[0] [arr[0], arr[1].to_s] else [field, field.to_s] end end end def date(*mappings) field_mapping(mappings).each do |field, mapping| define_method(field) { Date.iso8601(@obj[mapping.to_s]) } end end def bool(*mappings) field_mapping(mappings).each do |field, mapping| define_method(field.to_s + '?') { @obj[mapping] } end end def uri(*mappings) field_mapping(mappings).each do |field, mapping| define_method(field.to_s) { URI @obj[mapping.to_s] } end end def float(*mappings) field_mapping(mappings).each do |field, mapping| define_method(field.to_s) { @obj[mapping.to_s].to_f } end end def cast(klass, mappings, opts={map_array: true, map: false}) field_mapping(mappings).each do |field, mapping| define_method(field) do if opts[:map] || (@obj[mapping].is_a?(Array) && opts[:map_array]) @obj[mapping].map { |x| klass.new(x) } else klass.new @obj[mapping] end end end end private :field_mapping end def initialize(o) @obj = o end # Honestly, we should generate methods when we initially parse the Hash # instead of doing this crap in method_missing. I'll do that later. def method_missing(method, *args, &block) @obj[method.to_s] or super end def respond_to?(method) @obj.has_key? method.to_s or super end def inspect "#<%s:%014x>" % [self.class.name, object_id] end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
hooloo-0.3.2 | lib/hooloo/mozart_hash.rb |
hooloo-0.3.1 | lib/hooloo/mozart_hash.rb |
hooloo-0.3.0 | lib/hooloo/mozart_hash.rb |