Sha256: 8bc9d5f65b1c4849787b8d54936724efe179141f8586eaa59c390409e8d11d8c
Contents?: true
Size: 1.35 KB
Versions: 5
Compression:
Stored size: 1.35 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 /(date)|(timestamp)/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
5 entries across 5 versions & 1 rubygems