Sha256: c078d3c7478d9ac5eb2e478958dcabf579371e965c9a384e24a015b9394f6f64
Contents?: true
Size: 1.23 KB
Versions: 2
Compression:
Stored size: 1.23 KB
Contents
module MotionResource class Base attr_accessor :id def self.subclasses @subclasses ||= [] end def self.inherited(subclass) self.subclasses << subclass end def initialize(params = {}) @new_record = true update_attributes(params) end def new_record? @new_record end class << self def instantiate(json) json = json.symbolize_keys raise ArgumentError, "No :id parameter given for #{self.name}.instantiate" unless json[:id] klass = if json[:type] begin Object.const_get(json[:type].to_s) rescue NameError self end else self end if result = klass.recall(json[:id]) result.update_attributes(json) else result = klass.new(json) klass.remember(result.id, result) end result.send(:instance_variable_set, "@new_record", false) result end def identity_map @identity_map ||= {} end def remember(id, value) identity_map[id] = value end def recall(id) identity_map[id] end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
motion-resource-0.1.0 | lib/motion-resource/base.rb |
motion-resource-0.0.2 | lib/motion-resource/base.rb |