Class: Undies::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/undies/node.rb

Direct Known Subclasses

Element

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, mode = :inline) ⇒ Node

Returns a new instance of Node.



70
71
72
73
74
75
76
77
78
# File 'lib/undies/node.rb', line 70

def initialize(content, mode=:inline)
  @start_tag = nil
  @end_tag = nil
  @content = content
  @builds = []
  @attrs = {}
  @children = false
  @mode = mode
end

Class Method Details

.add_build(node, build_block) ⇒ Object



24
25
26
# File 'lib/undies/node.rb', line 24

def self.add_build(node, build_block)
  node.instance_variable_set("@builds", builds(node) + [build_block])
end

.attrs(element) ⇒ Object



36
37
38
# File 'lib/undies/node.rb', line 36

def self.attrs(element)
  element.instance_variable_get("@attrs")
end

.builds(node) ⇒ Object



20
21
22
# File 'lib/undies/node.rb', line 20

def self.builds(node)
  node.instance_variable_get("@builds") || []
end

.children(node) ⇒ Object



28
29
30
# File 'lib/undies/node.rb', line 28

def self.children(node)
  node.instance_variable_get("@children")
end

.content(node) ⇒ Object



50
51
52
# File 'lib/undies/node.rb', line 50

def self.content(node)
  node.instance_variable_get("@content") || ""
end

.end_tag(node) ⇒ Object



13
14
15
# File 'lib/undies/node.rb', line 13

def self.end_tag(node)
  node.instance_variable_get("@end_tag") || ""
end

.merge_attrs(element, value = {}) ⇒ Object



40
41
42
43
44
# File 'lib/undies/node.rb', line 40

def self.merge_attrs(element, value={})
  attrs(element).merge(value).tap do |a|
    element.instance_variable_set("@attrs", a)
  end
end

.mode(node) ⇒ Object



54
55
56
# File 'lib/undies/node.rb', line 54

def self.mode(node)
  node.instance_variable_get("@mode") || :inline
end

.node_name(node) ⇒ Object



46
47
48
# File 'lib/undies/node.rb', line 46

def self.node_name(node)
  node.instance_variable_get("@name") || ""
end

.prefix(node, meth, level, indent) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/undies/node.rb', line 58

def self.prefix(node, meth, level, indent)
  "".tap do |value|
    if mode(node) != :inline && indent > 0
      if meth == 'start_tag'
        value << "#{level > 0 ? "\n": ''}#{' '*level*indent}"
      elsif meth == 'end_tag'
        value << "\n#{' '*(level > 0 ? level-1 : level)*indent}"
      end
    end
  end
end

.set_children(node, value) ⇒ Object



32
33
34
# File 'lib/undies/node.rb', line 32

def self.set_children(node, value)
  node.instance_variable_set("@children", value)
end

.set_end_tag(node) ⇒ Object



18
# File 'lib/undies/node.rb', line 18

def self.set_end_tag(node); end

.set_start_tag(node) ⇒ Object



17
# File 'lib/undies/node.rb', line 17

def self.set_start_tag(node); end

.start_tag(node) ⇒ Object

have as many methods to the class level as possible to keep from polluting the public instance methods and to maximize the effectiveness of the Element#method_missing logic



9
10
11
# File 'lib/undies/node.rb', line 9

def self.start_tag(node)
  node.instance_variable_get("@start_tag") || ""
end

Instance Method Details

#==(other_node) ⇒ Object



80
81
82
83
84
# File 'lib/undies/node.rb', line 80

def ==(other_node)
  self.class.content(self) == other_node.class.content(other_node) &&
  self.instance_variable_get("@start_tag") == other_node.instance_variable_get("@start_tag") &&
  self.instance_variable_get("@end_tag") == other_node.instance_variable_get("@end_tag")
end