Sha256: 4298c2601d05cd06454a17d338cea63a98e5d740cb8303d169281dee66950e13
Contents?: true
Size: 1.46 KB
Versions: 13
Compression:
Stored size: 1.46 KB
Contents
module MyJohnDeere module JSONAttributes module ClassMethods attr_accessor :json_attributes def attributes_to_pull_from_json(*attribs) self.json_attributes = attribs self.json_attributes.each do |attribute| attribute = attribute.to_s.underscore define_method("#{attribute}=") do |val| instance_variable_set("@#{attribute}", val) end define_method(attribute) do return instance_variable_get("@#{attribute}") end end end end module InstanceMethods def setup_attributes(json_data) return if self.class.json_attributes.nil? self.class.json_attributes.each do |attrib| attrib = attrib.to_s val = json_data[attrib] if val =~ /\A(true)|(false)\z/i then val = /true/i.match(val) ? true : false elsif /(date)|(timestamp)|(time\Z)/i.match(attrib) then # try to parse it val = Time.parse(val) rescue val end instance_variable_set("@#{attrib.underscore}", val) end end def to_s the_hash = {} self.class.json_attributes.each do |attrib| the_hash[attrib] = send(attrib.to_s.underscore) end return "#{self.class}: #{the_hash}" end end def self.included(receiver) receiver.extend ClassMethods receiver.send :include, InstanceMethods end end end
Version data entries
13 entries across 13 versions & 1 rubygems