Class: DatoDast::Nodes::Base

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

Constant Summary collapse

EMPTY =
""
NEWLINE =
"\n"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, links = [], blocks = [], config = nil) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
# File 'lib/dato_dast/nodes/base.rb', line 11

def initialize(node, links = [], blocks = [], config = nil)
  @node = node
  @links = links
  @blocks = blocks
  @config = config
end

Class Method Details

.typeObject



7
8
9
# File 'lib/dato_dast/nodes/base.rb', line 7

def self.type
  name.demodulize.camelize(:lower)
end

Instance Method Details

#childrenObject



26
27
28
# File 'lib/dato_dast/nodes/base.rb', line 26

def children
  @node["children"]
end

#configObject



18
19
20
# File 'lib/dato_dast/nodes/base.rb', line 18

def config
  @config ||= DatoDast.configuration
end

#css_classObject



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

def css_class
  if node_config && node_config["css_class"].is_a?(Proc)
    node_config["css_class"].call(proc_object)
  else
    @node["css_class"] || node_config["css_class"]
  end
end

#metaObject



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

def meta
  if node_config && node_config["meta"].is_a?(Proc)
    node_config["meta"].call(proc_object)
  else
    @node["meta"] || node_config["meta"]
  end
end

#node_configObject



58
59
60
# File 'lib/dato_dast/nodes/base.rb', line 58

def node_config
  @node_config ||= config.types[type]
end

#renderObject



70
71
72
73
74
75
76
# File 'lib/dato_dast/nodes/base.rb', line 70

def render
  open_wrappers +
    html_tag.open +
    render_value +
    html_tag.close +
    close_wrappers
end

#render_childrenObject



82
83
84
85
86
87
88
# File 'lib/dato_dast/nodes/base.rb', line 82

def render_children
  return EMPTY unless children.present?

  children.map do |child|
    Nodes.wrap(child, @links, @blocks, config).render
  end.join("\n").gsub(/\n+/, "\n")
end

#render_valueObject



78
79
80
# File 'lib/dato_dast/nodes/base.rb', line 78

def render_value
  render_children
end

#tagObject



34
35
36
37
38
39
40
# File 'lib/dato_dast/nodes/base.rb', line 34

def tag
  if node_config && node_config["tag"].is_a?(Proc)
    node_config["tag"].call(proc_object)
  else
    @node["tag"] || node_config["tag"]
  end
end

#tag_infoObject



62
63
64
65
66
67
68
# File 'lib/dato_dast/nodes/base.rb', line 62

def tag_info
  {
    "tag" => tag,
    "css_class" => css_class,
    "meta" => meta,
  }
end

#typeObject



22
23
24
# File 'lib/dato_dast/nodes/base.rb', line 22

def type
  @node["type"]
end

#wrappersObject



30
31
32
# File 'lib/dato_dast/nodes/base.rb', line 30

def wrappers
  @node["wrappers"] || Array.wrap(node_config["wrappers"])
end