Class: Dato::Local::Item
- Inherits:
-
Object
- Object
- Dato::Local::Item
- Extended by:
- Forwardable
- Defined in:
- lib/dato/local/item.rb
Instance Attribute Summary collapse
-
#entity ⇒ Object
readonly
Returns the value of attribute entity.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #attributes ⇒ Object
- #children ⇒ Object
- #created_at ⇒ Object
- #fields ⇒ Object
-
#initialize(entity, items_repo) ⇒ Item
constructor
A new instance of Item.
- #item_type ⇒ Object
- #parent ⇒ Object
- #position ⇒ Object
- #seo_meta_tags ⇒ Object
- #singleton? ⇒ Boolean (also: #single_instance?)
- #to_hash(max_depth = 3, current_depth = 0) ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #updated_at ⇒ Object
Constructor Details
#initialize(entity, items_repo) ⇒ Item
Returns a new instance of Item.
19 20 21 22 |
# File 'lib/dato/local/item.rb', line 19 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 (private)
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/dato/local/item.rb', line 148 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 = [] << "Undefined method `#{method}`" << "Available fields for a `#{item_type.api_key}` item:" += fields.map do |f| "* .#{f.api_key}" end raise NoMethodError, .join("\n") else raise e end end |
Instance Attribute Details
#entity ⇒ Object (readonly)
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
24 25 26 |
# File 'lib/dato/local/item.rb', line 24 def ==(other) other.is_a?(Item) && other.id == id end |
#[](key) ⇒ Object
79 80 81 |
# File 'lib/dato/local/item.rb', line 79 def [](key) attributes[key.to_sym] end |
#attributes ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/dato/local/item.rb', line 45 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
61 62 63 |
# File 'lib/dato/local/item.rb', line 61 def children @items_repo.children_of(id).sort_by(&:position) if item_type.tree end |
#created_at ⇒ Object
69 70 71 |
# File 'lib/dato/local/item.rb', line 69 def created_at Time.parse(.created_at).utc end |
#fields ⇒ Object
41 42 43 |
# File 'lib/dato/local/item.rb', line 41 def fields @fields ||= item_type.fields.sort_by(&:position) end |
#item_type ⇒ Object
37 38 39 |
# File 'lib/dato/local/item.rb', line 37 def item_type @item_type ||= entity.item_type end |
#parent ⇒ Object
57 58 59 |
# File 'lib/dato/local/item.rb', line 57 def parent @items_repo.find(entity.parent_id) if item_type.tree && entity.parent_id end |
#position ⇒ Object
53 54 55 |
# File 'lib/dato/local/item.rb', line 53 def position entity.position end |
#seo_meta_tags ⇒ Object
28 29 30 |
# File 'lib/dato/local/item.rb', line 28 def Utils::SeoTagsBuilder.new(self, @items_repo.site). end |
#singleton? ⇒ Boolean Also known as: single_instance?
32 33 34 |
# File 'lib/dato/local/item.rb', line 32 def singleton? item_type.singleton end |
#to_hash(max_depth = 3, current_depth = 0) ⇒ Object
83 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 |
# File 'lib/dato/local/item.rb', line 83 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
73 74 75 76 |
# File 'lib/dato/local/item.rb', line 73 def to_s api_key = item_type.api_key "#<Item id=#{id} item_type=#{api_key} attributes=#{attributes}>" end |
#updated_at ⇒ Object
65 66 67 |
# File 'lib/dato/local/item.rb', line 65 def updated_at Time.parse(.updated_at).utc end |