Class: Dato::Local::Item
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/dato/local/item.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(entity, items_repo) ⇒ Item
Returns a new instance of Item.
20
21
22
23
|
# File 'lib/dato/local/item.rb', line 20
def initialize(entity, items_repo)
@entity = entity
@items_repo = items_repo
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments, &block) ⇒ Object
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/dato/local/item.rb', line 149
def method_missing(method, *arguments, &block)
field = fields.find { |f| f.api_key.to_sym == method }
if field && arguments.empty?
read_attribute(method, field)
else
super
end
rescue NoMethodError => e
if e.name === method
message = []
message << "Undefined method `#{method}`"
message << "Available fields for a `#{item_type.api_key}` item:"
message += fields.map do |f|
"* .#{f.api_key}"
end
raise NoMethodError, message.join("\n")
else
raise e
end
end
|
Instance Attribute Details
#entity ⇒ Object
Returns the value of attribute entity.
17
18
19
|
# File 'lib/dato/local/item.rb', line 17
def entity
@entity
end
|
Instance Method Details
#==(other) ⇒ Object
25
26
27
|
# File 'lib/dato/local/item.rb', line 25
def ==(other)
other.is_a?(Item) && other.id == id
end
|
#[](key) ⇒ Object
80
81
82
|
# File 'lib/dato/local/item.rb', line 80
def [](key)
attributes[key.to_sym]
end
|
#attributes ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/dato/local/item.rb', line 46
def attributes
fields.each_with_object(
ActiveSupport::HashWithIndifferentAccess.new
) do |field, acc|
acc[field.api_key.to_sym] = read_attribute(field.api_key, field)
end
end
|
#children ⇒ Object
62
63
64
|
# File 'lib/dato/local/item.rb', line 62
def children
@items_repo.children_of(id).sort_by(&:position) if item_type.tree
end
|
#created_at ⇒ Object
70
71
72
|
# File 'lib/dato/local/item.rb', line 70
def created_at
Time.parse(meta.created_at).utc
end
|
#fields ⇒ Object
42
43
44
|
# File 'lib/dato/local/item.rb', line 42
def fields
@fields ||= item_type.fields.sort_by(&:position)
end
|
#item_type ⇒ Object
38
39
40
|
# File 'lib/dato/local/item.rb', line 38
def item_type
@item_type ||= entity.item_type
end
|
#parent ⇒ Object
58
59
60
|
# File 'lib/dato/local/item.rb', line 58
def parent
@items_repo.find(entity.parent_id) if item_type.tree && entity.parent_id
end
|
#position ⇒ Object
54
55
56
|
# File 'lib/dato/local/item.rb', line 54
def position
entity.position
end
|
#singleton? ⇒ Boolean
Also known as:
single_instance?
33
34
35
|
# File 'lib/dato/local/item.rb', line 33
def singleton?
item_type.singleton
end
|
#to_hash(max_depth = 3, current_depth = 0) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/dato/local/item.rb', line 84
def to_hash(max_depth = 3, current_depth = 0)
return id if current_depth >= max_depth
base = {
id: id,
item_type: item_type.api_key,
updated_at: updated_at,
created_at: created_at
}
base[:position] = position if item_type.sortable
if item_type.tree
base[:position] = position
base[:children] = children.map do |child|
child.to_hash(
max_depth,
current_depth + 1
)
end
end
fields.each_with_object(base) do |field, result|
value = send(field.api_key)
result[field.api_key.to_sym] = if value.respond_to?(:to_hash)
value.to_hash(
max_depth,
current_depth + 1
)
else
value
end
end
end
|
#to_s ⇒ Object
Also known as:
inspect
74
75
76
77
|
# File 'lib/dato/local/item.rb', line 74
def to_s
api_key = item_type.api_key
"#<Item id=#{id} item_type=#{api_key} attributes=#{attributes}>"
end
|
#updated_at ⇒ Object
66
67
68
|
# File 'lib/dato/local/item.rb', line 66
def updated_at
Time.parse(meta.updated_at).utc
end
|