Class: Hexp::List

Inherits:
Array
  • Object
show all
Defined in:
lib/hexp/list.rb,
lib/hexp-rails.rb

Overview

A list of nodes

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes) ⇒ List

Create new Hexp::List

Examples:

Hexp::List.new([H[:p], H[:div]])

Parameters:

  • nodes (#to_ary)

    List of nodes



14
15
16
17
18
19
20
# File 'lib/hexp/list.rb', line 14

def initialize(nodes)
  if nodes.instance_of?(List)
    super(nodes.__getobj__).freeze
  else
    super nodes.to_ary.map(&Node::Normalize.method(:coerce_node)).freeze
  end
end

Class Method Details

.[](*args) ⇒ Hexp::List

Convenience constructor

Examples:

Hexp::List[
  Hexp::Node[:marquee, "Try Hexp for instanst satisfaction!"],
  Hexp::Node[:hr],
]

Parameters:

  • args (Array)

    individual nodes

Returns:



36
37
38
# File 'lib/hexp/list.rb', line 36

def self.[](*args)
  new(args)
end

Instance Method Details

#+(other) ⇒ Object



93
94
95
# File 'lib/hexp/list.rb', line 93

def +(other)
  self.class[*to_ary, *other.to_ary]
end

#append(*args) ⇒ Object



97
98
99
# File 'lib/hexp/list.rb', line 97

def append(*args)
  self + args
end

#eql?(other) ⇒ true, false

Value and type equality

Hexp::List is mostly interchangeable with a plain Array, and so equality with ‘==` delegates to the underlying array, making `Hexp::List[] == []` true.

If you want a stronger comparison, than this version will compare both the value (in this case : contents), and the type.

Examples:

H[:div, [[:span]]].children == [H[:span]]             #=> true
H[:div, [[:span]]].children.eql? [H[:span]]           #=> false
H[:div, [[:span]]].children.eql? Hexp::List[H[:span]] #=> true

Parameters:

  • other (Object)

    Object to compare with

Returns:

  • (true, false)


85
86
87
# File 'lib/hexp/list.rb', line 85

def eql?(other)
  self == other && self.class == other.class
end

#inspectString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

String representation

This delegates to the underlying array, so it’s not obvious from the output that this is a wrapping class. This is convenient when inspecting nested hexps, but probably something we want to solve differently.

Returns:

  • (String)


49
50
51
# File 'lib/hexp/list.rb', line 49

def inspect
  __getobj__.inspect
end

#to_aryArray<Hexp::Node>

Implicit conversion to Array

Examples:

Hexp::List[ H[:p], H[:span] ].to_ary #=> [H[:p], H[:span]]

Returns:



61
62
63
# File 'lib/hexp/list.rb', line 61

def to_ary
  __getobj__
end

#to_htmlObject



89
90
91
# File 'lib/hexp/list.rb', line 89

def to_html
  each_with_object('') {|n,s| s << n.to_html}
end

#to_sObject



13
14
15
# File 'lib/hexp-rails.rb', line 13

def to_s
  to_html.html_safe
end