Class: SetAttributes
- Inherits:
-
Object
show all
- Defined in:
- lib/set_attributes/attribute.rb,
lib/set_attributes/controls/hash.rb,
lib/set_attributes/set_attributes.rb,
lib/set_attributes/controls/object.rb
Defined Under Namespace
Modules: Attribute, Controls
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.build(receiver, data, copy: nil, include: nil, exclude: nil, strict: nil) ⇒ Object
-
.call(receiver, data, include: nil, copy: nil, exclude: nil, strict: nil) ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(receiver, data) ⇒ SetAttributes
Returns a new instance of SetAttributes.
20
21
22
23
|
# File 'lib/set_attributes/set_attributes.rb', line 20
def initialize(receiver, data)
@receiver = receiver
@data = data
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
3
4
5
|
# File 'lib/set_attributes/set_attributes.rb', line 3
def data
@data
end
|
#exclude ⇒ Object
12
13
14
|
# File 'lib/set_attributes/set_attributes.rb', line 12
def exclude
@exclude ||= []
end
|
#include ⇒ Object
8
9
10
|
# File 'lib/set_attributes/set_attributes.rb', line 8
def include
@include ||= []
end
|
#receiver ⇒ Object
Returns the value of attribute receiver.
2
3
4
|
# File 'lib/set_attributes/set_attributes.rb', line 2
def receiver
@receiver
end
|
#strict ⇒ Object
16
17
18
|
# File 'lib/set_attributes/set_attributes.rb', line 16
def strict
@strict ||= false
end
|
Class Method Details
.build(receiver, data, copy: nil, include: nil, exclude: nil, strict: nil) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/set_attributes/set_attributes.rb', line 25
def self.build(receiver, data, copy: nil, include: nil, exclude: nil, strict: nil)
strict ||= false
unless data.respond_to? :to_h
raise ArgumentError, "#{data} can't be used to set attributes. It can't be converted to Hash."
end
unless data.is_a? Hash
data = data.to_h
end
exclude ||= []
exclude = Array(exclude)
unless copy.nil?
include = copy
end
include ||= []
include = Array(include)
include = data.keys if include.empty?
new(receiver, data).tap do |instance|
instance.include = include
instance.exclude = exclude
instance.strict = strict
end
end
|
.call(receiver, data, include: nil, copy: nil, exclude: nil, strict: nil) ⇒ Object
54
55
56
57
|
# File 'lib/set_attributes/set_attributes.rb', line 54
def self.call(receiver, data, include: nil, copy: nil, exclude: nil, strict: nil)
instance = build(receiver, data, copy: copy, include: include, exclude: exclude, strict: strict)
instance.()
end
|
Instance Method Details
#call ⇒ Object
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/set_attributes/set_attributes.rb', line 59
def call
include_mapping = self.include_mapping
attributes = (data.keys & include_mapping.keys) - exclude
set_attributes = []
attributes.each do |from_attribute|
to_attribute = include_mapping[from_attribute]
value = data[from_attribute]
Attribute.set(receiver, to_attribute, value, strict: strict)
set_attributes << to_attribute
end
set_attributes
end
|
#include_mapping ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/set_attributes/set_attributes.rb', line 76
def include_mapping
mapping = {}
include.each do |item|
if item.is_a? Hash
mapping[item.keys.first] = item.values.first
else
mapping[item] = item
end
end
mapping
end
|