Class: Vapid::Template::Node

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

Overview

A wrapper around Nokogiri’s node, so we can manipulate using our own methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, clone_index = 0) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
# File 'lib/vapid/template/node.rb', line 7

def initialize(node, clone_index = 0)
  @node = node
  @directives = parse_directives
  @clone_index = node["vp-clone"].present? ? node["vp-clone"].to_i : clone_index
end

Instance Attribute Details

#clone_indexObject (readonly)

Returns the value of attribute clone_index.



5
6
7
# File 'lib/vapid/template/node.rb', line 5

def clone_index
  @clone_index
end

#directivesObject (readonly)

Returns the value of attribute directives.



5
6
7
# File 'lib/vapid/template/node.rb', line 5

def directives
  @directives
end

Instance Method Details

#clone(num_clones = 1) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/vapid/template/node.rb', line 21

def clone(num_clones = 1)
  num_clones.times do |n|
    cloned = @node.dup
    cloned["vp-clone"] = num_clones - n
    @node.after cloned
  end
end

#clone?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/vapid/template/node.rb', line 29

def clone?
  clone_index > 0
end

#cloneable?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/vapid/template/node.rb', line 33

def cloneable?
  group? && !clone?
end

#directives?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/vapid/template/node.rb', line 37

def directives?
  @directives.any?
end

#first_childObject



41
42
43
# File 'lib/vapid/template/node.rb', line 41

def first_child
  wrap @node.first_element_child
end

#group?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/vapid/template/node.rb', line 17

def group?
  group_expr.present?
end

#group_exprObject



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

def group_expr
  @directives["group"]
end

#modify(obj) ⇒ Object

TODO: Abstract this, so that directives only need to pass in

html, text, etc, and don't have to know about Nokogiri methods/concepts

rubocop:disable MethodLength



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/vapid/template/node.rb', line 52

def modify(obj)
  return unless obj

  obj.each do |prop, value|
    if value.is_a?(String)
      @node.send "#{prop}=", value
    elsif prop == :attributes
      modify_attributes(value)
    elsif value.is_a?(Hash)
      modify_properties(prop, value)
    elsif prop == :placeholder
      @node["vp-placeholder"] = true
    end
  end
end

#next_siblingObject



45
46
47
# File 'lib/vapid/template/node.rb', line 45

def next_sibling
  wrap @node.next_element
end