Sha256: 1de50d3d5b6ca2c28c5e42abffedf89e04b47bdbb970515efd3ba06252629141
Contents?: true
Size: 1.23 KB
Versions: 2
Compression:
Stored size: 1.23 KB
Contents
# poorly-optimized, but handy, mixin for Hash and Model examples. # primarily enables support for lazy values. module Attributor module ExampleMixin def self.extended(obj) obj.class.attributes.each do |name, _| obj.define_singleton_method(name) do get(name) end end end def lazy_attributes @lazy_attributes ||= {} end def lazy_attributes=(val) @lazy_attributes = val end def keys @contents.keys | lazy_attributes.keys end def key?(key) @contents.key?(key) || lazy_attributes.key?(key) end def get(key, context: self.generate_subcontext(Attributor::DEFAULT_ROOT_CONTEXT,key)) key = self.class.key_attribute.load(key, context) unless @contents.key? key if lazy_attributes.key?(key) proc = lazy_attributes.delete(key) @contents[key] = proc.call(self) end end super end def attributes lazy_attributes.keys.each do |name| self.__send__(name) end super end def contents lazy_attributes.keys do |key| proc = lazy_attributes.delete(key) @contents[key] = proc.call(self) end super end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
attributor-2.5.0 | lib/attributor/example_mixin.rb |
attributor-2.4.0 | lib/attributor/example_mixin.rb |