Class: DatoDast::Nodes::Item

Inherits:
Base
  • Object
show all
Defined in:
lib/dato_dast/nodes/item.rb

Direct Known Subclasses

Block, InlineItem

Constant Summary

Constants inherited from Base

Base::EMPTY, Base::NEWLINE

Instance Method Summary collapse

Methods inherited from Base

#config, #css_class, #initialize, #meta, #render_children, #tag, #tag_info, #type, type, #wrappers

Constructor Details

This class inherits a constructor from DatoDast::Nodes::Base

Instance Method Details

#childrenObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dato_dast/nodes/item.rb', line 24

def children
  Array.wrap(structure).map do |child|
    case child["type"]
    when "field"
      build_field(child)
    when "value"
      build_value(child)
    when "item"
      build_item(item[child["field"].to_sym])
    when "items"
      build_items(child)
    else
      raise DatoDast::Errors::InvalidStructureType.new(error_label)
    end.merge(extract_tags(child)).compact
  end.flatten
end

#itemObject



8
9
10
11
12
# File 'lib/dato_dast/nodes/item.rb', line 8

def item
  return @item if @item

  @item ||= item_repo.find { |item| item[:id] == item_id }
end

#item_idObject



4
5
6
# File 'lib/dato_dast/nodes/item.rb', line 4

def item_id
  @node["item"]
end

#item_typeObject



14
15
16
# File 'lib/dato_dast/nodes/item.rb', line 14

def item_type
  item[:item_type]
end

#nodeObject



18
19
20
21
22
# File 'lib/dato_dast/nodes/item.rb', line 18

def node
  raise DatoDast::Errors::MissingItemConfiguration.new(error_label) unless node_config

  node_config["node"]
end

#renderObject



49
50
51
52
53
54
55
56
57
# File 'lib/dato_dast/nodes/item.rb', line 49

def render
  return super unless node

  begin
    node.new(item).render
  rescue NoMethodError => _e
    raise DatoDast::Errors::MissingRenderFunction.new([error_label])
  end
end

#render_valueObject



41
42
43
44
45
46
47
# File 'lib/dato_dast/nodes/item.rb', line 41

def render_value
  if node_config["render_value"]
    node_config["render_value"].call(item)
  else
    render_children
  end
end