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.
18
19
20
21
|
# File 'lib/dato/local/item.rb', line 18
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
113
114
115
116
117
118
119
120
|
# File 'lib/dato/local/item.rb', line 113
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
end
|
Instance Attribute Details
#entity ⇒ Object
Returns the value of attribute entity.
15
16
17
|
# File 'lib/dato/local/item.rb', line 15
def entity
@entity
end
|
Instance Method Details
#==(other) ⇒ Object
23
24
25
|
# File 'lib/dato/local/item.rb', line 23
def ==(other)
other.is_a?(Item) && other.id == id
end
|
#attributes ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/dato/local/item.rb', line 54
def attributes
@attributes ||= fields.each_with_object(
ActiveSupport::HashWithIndifferentAccess.new
) do |field, acc|
acc[field.api_key.to_sym] = send(field.api_key)
end
end
|
#fields ⇒ Object
50
51
52
|
# File 'lib/dato/local/item.rb', line 50
def fields
@fields ||= item_type.fields.sort_by(&:position)
end
|
#item_type ⇒ Object
Also known as:
content_type
45
46
47
|
# File 'lib/dato/local/item.rb', line 45
def item_type
@item_type ||= entity.item_type
end
|
#position ⇒ Object
62
63
64
|
# File 'lib/dato/local/item.rb', line 62
def position
entity.position
end
|
#respond_to_missing?(method, include_private = false) ⇒ Boolean
84
85
86
87
88
89
90
91
|
# File 'lib/dato/local/item.rb', line 84
def respond_to_missing?(method, include_private = false)
field = fields.find { |f| f.api_key.to_sym == method }
if field
true
else
super
end
end
|
#singleton? ⇒ Boolean
41
42
43
|
# File 'lib/dato/local/item.rb', line 41
def singleton?
item_type.singleton
end
|
#slug(prefix_with_id: true) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/dato/local/item.rb', line 27
def slug(prefix_with_id: true)
return item_type.api_key.humanize.parameterize if singleton?
return id.to_s unless title_attribute
title = send(title_attribute)
if title && prefix_with_id
"#{id}-#{title.parameterize[0..50]}"
elsif title
title.parameterize[0..50]
else
id.to_s
end
end
|
#title_attribute ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/dato/local/item.rb', line 76
def title_attribute
title_field = fields.find do |field|
field.field_type == 'string' &&
field.appeareance[:type] == 'title'
end
title_field && title_field.api_key
end
|
#to_s ⇒ Object
Also known as:
inspect
70
71
72
73
|
# File 'lib/dato/local/item.rb', line 70
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(entity.updated_at)
end
|