Module: Attributor::ExampleMixin

Defined in:
lib/attributor/example_mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(obj) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/attributor/example_mixin.rb', line 8

def self.extended(obj)
  obj.class.attributes.each do |name, _|
    obj.define_singleton_method(name) do
      get(name)
    end
  end
end

Instance Method Details

#attributesObject



45
46
47
48
49
50
51
# File 'lib/attributor/example_mixin.rb', line 45

def attributes
  lazy_attributes.keys.each do |name|
    self.__send__(name)
  end

  super
end

#contentsObject



53
54
55
56
57
58
59
60
# File 'lib/attributor/example_mixin.rb', line 53

def contents
  lazy_attributes.keys.each do |key|
    proc = lazy_attributes.delete(key)
    @contents[key] = proc.call(self)
  end
  
  super
end

#get(key, context: self.generate_subcontext(Attributor::DEFAULT_ROOT_CONTEXT,key)) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/attributor/example_mixin.rb', line 32

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

#key?(key) ⇒ Boolean

Returns:



28
29
30
# File 'lib/attributor/example_mixin.rb', line 28

def key?(key)
  @contents.key?(key) || lazy_attributes.key?(key)
end

#keysObject



24
25
26
# File 'lib/attributor/example_mixin.rb', line 24

def keys
  @contents.keys | lazy_attributes.keys
end

#lazy_attributesObject



16
17
18
# File 'lib/attributor/example_mixin.rb', line 16

def lazy_attributes
  @lazy_attributes ||= {}
end

#lazy_attributes=(val) ⇒ Object



20
21
22
# File 'lib/attributor/example_mixin.rb', line 20

def lazy_attributes=(val)
  @lazy_attributes = val
end