Class: Dato::Local::Item

Inherits:
Object
  • 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.



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
    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

#entityObject (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

#attributesObject



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

#childrenObject



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_atObject



69
70
71
# File 'lib/dato/local/item.rb', line 69

def created_at
  Time.parse(meta.created_at).utc
end

#fieldsObject



41
42
43
# File 'lib/dato/local/item.rb', line 41

def fields
  @fields ||= item_type.fields.sort_by(&:position)
end

#item_typeObject



37
38
39
# File 'lib/dato/local/item.rb', line 37

def item_type
  @item_type ||= entity.item_type
end

#parentObject



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

#positionObject



53
54
55
# File 'lib/dato/local/item.rb', line 53

def position
  entity.position
end

#seo_meta_tagsObject



28
29
30
# File 'lib/dato/local/item.rb', line 28

def seo_meta_tags
  Utils::SeoTagsBuilder.new(self, @items_repo.site).meta_tags
end

#singleton?Boolean Also known as: single_instance?

Returns:

  • (Boolean)


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_sObject 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_atObject



65
66
67
# File 'lib/dato/local/item.rb', line 65

def updated_at
  Time.parse(meta.updated_at).utc
end