Class: Interfacets::Shared::Entities::Specs::Handlers::Collection
- Inherits:
-
Handler
- Object
- Handler
- Interfacets::Shared::Entities::Specs::Handlers::Collection
show all
- Defined in:
- lib/interfacets/shared/entities/specs/handlers.rb
Instance Attribute Summary
Attributes inherited from Handler
#cache, #entity, #spec
Instance Method Summary
collapse
Methods inherited from Handler
#initialize, #type, #unwrap, #wrap
Instance Method Details
#build(**attributes) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/interfacets/shared/entities/specs/handlers.rb', line 112
def build(**attributes)
value = unwrap(entity.instance_exec(&spec.builder))
attributes.each do |name, val|
value.send("#{name}=", val)
end
items = entity.instance_exec(&spec.getter)
if items.nil?
set([value])
elsif !items.include?(value)
items << value
end
wrap(value)
end
|
#collection? ⇒ Boolean
81
82
83
|
# File 'lib/interfacets/shared/entities/specs/handlers.rb', line 81
def collection?
true
end
|
#get ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/interfacets/shared/entities/specs/handlers.rb', line 89
def get
values = entity.instance_exec(&spec.getter) || []
CollectionProxy.new(
values,
wrap: ->(val) { wrap(val) },
unwrap: ->(val) { unwrap(val) },
).tap { |entities|
@cache = entities.map { [_1.store, _1] }.to_h
}
end
|
#reference? ⇒ Boolean
85
86
87
|
# File 'lib/interfacets/shared/entities/specs/handlers.rb', line 85
def reference?
false
end
|
#set(val) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/interfacets/shared/entities/specs/handlers.rb', line 101
def set(val)
@cache ||= {}
val.each do |item|
if item.is_a?(Entity)
@cache[item.store] = item
end
end
entity.instance_exec(unwrap(val), &spec.setter)
end
|