Class: Arbre::ElementCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/arbre/element_collection.rb

Direct Known Subclasses

ChildElementCollection

Instance Method Summary collapse

Constructor Details

#initialize(elements = []) ⇒ ElementCollection

Initialization



8
9
10
# File 'lib/arbre/element_collection.rb', line 8

def initialize(elements = [])
  @elements = elements.to_a
end

Instance Method Details

#&(other) ⇒ Object



69
70
71
# File 'lib/arbre/element_collection.rb', line 69

def &(other)
  self.class.new(@elements & other)
end

#+(other) ⇒ Object

Set operations



61
62
63
# File 'lib/arbre/element_collection.rb', line 61

def +(other)
  self.class.new((@elements + other).uniq)
end

#-(other) ⇒ Object



65
66
67
# File 'lib/arbre/element_collection.rb', line 65

def -(other)
  self.class.new(@elements - other)
end

#<<(element) ⇒ Object



29
30
31
# File 'lib/arbre/element_collection.rb', line 29

def <<(element)
  add element
end

#==(other) ⇒ Object



50
51
52
# File 'lib/arbre/element_collection.rb', line 50

def ==(other)
  to_a == other.to_a
end

#[](*args) ⇒ Object



19
20
21
22
23
# File 'lib/arbre/element_collection.rb', line 19

def [](*args)
  result = @elements[*args]
  result = self.class.new(result) if result.is_a?(Enumerable)
  result
end

#add(element) ⇒ Object



25
26
27
28
# File 'lib/arbre/element_collection.rb', line 25

def add(element)
  @elements << element unless include?(element)
  self
end

#concat(elements) ⇒ Object



33
34
35
36
37
# File 'lib/arbre/element_collection.rb', line 33

def concat(elements)
  elements.each do |element|
    self << element
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/arbre/element_collection.rb', line 54

def eql?(other)
  other.is_a?(ElementCollection) && self == other
end

#remove(element) ⇒ Object



39
40
41
# File 'lib/arbre/element_collection.rb', line 39

def remove(element)
  @elements.delete element
end

#to_aObject



46
47
48
# File 'lib/arbre/element_collection.rb', line 46

def to_a
  @elements.dup
end

#to_aryObject



43
44
45
# File 'lib/arbre/element_collection.rb', line 43

def to_ary
  @elements
end

#to_sObject

String conversion



76
77
78
# File 'lib/arbre/element_collection.rb', line 76

def to_s
  html_safe_join(map(&:to_s), "\n")
end