module Attributor::ExampleMixin

Public Class Methods

extended(obj) click to toggle source
# File lib/attributor/example_mixin.rb, line 6
def self.extended(obj)
  if obj.is_a? Attributor::Model
    obj.class.attributes.each do |name, _|
      obj.define_singleton_method(name) do
        get(name)
      end
    end
  end
end

Public Instance Methods

[](k) click to toggle source
# File lib/attributor/example_mixin.rb, line 24
def [](k)
  unless @contents.key?(k)
    if (proc = lazy_attributes.delete k)
      @contents[k] = proc.call
    end
  end
  @contents[k]
end
[]=(k, v) click to toggle source
# File lib/attributor/example_mixin.rb, line 33
def []=(k, v)
  lazy_attributes.delete k
  @contents[k] = v
end
attributes() click to toggle source
Calls superclass method
# File lib/attributor/example_mixin.rb, line 77
def attributes
  lazy_attributes.keys.each do |name|
    __send__(name)
  end

  super
end
contents() click to toggle source
Calls superclass method
# File lib/attributor/example_mixin.rb, line 85
def contents
  lazy_attributes.keys.each do |key|
    proc = lazy_attributes.delete(key)
    @contents[key] = proc.call(self) unless @contents.key?(key)
  end

  super
end
each(&block) click to toggle source
# File lib/attributor/example_mixin.rb, line 38
def each(&block)
  contents.each(&block)
end
Also aliased as: each_pair
each_pair(&block)
Alias for: each
empty?() click to toggle source
# File lib/attributor/example_mixin.rb, line 48
def empty?
  contents.empty?
end
get(key, context: generate_subcontext(Attributor::DEFAULT_ROOT_CONTEXT, key)) click to toggle source
Calls superclass method
# File lib/attributor/example_mixin.rb, line 64
def get(key, context: 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
key?(key) click to toggle source
# File lib/attributor/example_mixin.rb, line 60
def key?(key)
  @contents.key?(key) || lazy_attributes.key?(key)
end
keys() click to toggle source
# File lib/attributor/example_mixin.rb, line 56
def keys
  @contents.keys | lazy_attributes.keys
end
lazy_attributes() click to toggle source
# File lib/attributor/example_mixin.rb, line 16
def lazy_attributes
  @lazy_attributes ||= {}
end
lazy_attributes=(val) click to toggle source
# File lib/attributor/example_mixin.rb, line 20
def lazy_attributes=(val)
  @lazy_attributes = val
end
size() click to toggle source
# File lib/attributor/example_mixin.rb, line 52
def size
  keys.size
end
values() click to toggle source
# File lib/attributor/example_mixin.rb, line 44
def values
  contents.values
end