Class: Iter::Elem

Inherits:
BasicObject
Includes:
Comparable
Defined in:
lib/epitools/iter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iter, val) ⇒ Elem

Returns a new instance of Elem.



74
75
76
77
78
# File 'lib/epitools/iter.rb', line 74

def initialize(iter, val)
  @iter = iter
  @val  = val.is_a?(Elem) ? val.value : val
  @visited = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



153
154
155
# File 'lib/epitools/iter.rb', line 153

def method_missing(name, *args)
  @val.send(name, *args)
end

Instance Attribute Details

#valObject

Returns the value of attribute val.



72
73
74
# File 'lib/epitools/iter.rb', line 72

def val
  @val
end

#visitedObject

Returns the value of attribute visited.



72
73
74
# File 'lib/epitools/iter.rb', line 72

def visited
  @visited
end

Instance Method Details

#<=>(other) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/epitools/iter.rb', line 164

def <=>(other)
  case other
  when Elem
    @val <=> other.value
  else
    @val <=> other
  end
end

#containerObject



84
85
86
# File 'lib/epitools/iter.rb', line 84

def container
  @iter.container
end

#currentObject



88
89
90
# File 'lib/epitools/iter.rb', line 88

def current
  self
end

#elem?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/epitools/iter.rb', line 80

def elem?
  true
end

#inspectObject



157
158
159
# File 'lib/epitools/iter.rb', line 157

def inspect
  "<Elem: #{@val.inspect}>"
end

#move_after(other) ⇒ Object



132
133
134
135
# File 'lib/epitools/iter.rb', line 132

def move_after(other)
  remove
  container.insert(other.pos+1, self) # insert after pos
end

#move_before(other) ⇒ Object



127
128
129
130
# File 'lib/epitools/iter.rb', line 127

def move_before(other)
  remove
  container.insert(other.pos, self) # insert at pos and shift everything over
end

#move_firstObject Also known as: move_start



137
138
139
140
# File 'lib/epitools/iter.rb', line 137

def move_first
  remove
  container.insert(0, self) # insert at beginning
end

#move_lastObject Also known as: move_end



143
144
145
146
# File 'lib/epitools/iter.rb', line 143

def move_last
  remove
  container.insert(-1, self) # insert at end
end

#nextObject



96
97
98
99
100
101
102
103
# File 'lib/epitools/iter.rb', line 96

def next
  p = pos+1
  if p >= container.size
    nil
  else
    container[p]
  end
end

#posObject



123
124
125
# File 'lib/epitools/iter.rb', line 123

def pos
  container.index(self)
end

#prevObject



105
106
107
108
109
110
111
112
# File 'lib/epitools/iter.rb', line 105

def prev
  p = pos-1
  if p < 0
    nil
  else
    container[p]
  end
end

#removeObject Also known as: delete



114
115
116
# File 'lib/epitools/iter.rb', line 114

def remove
  container.delete_at(pos)
end

#replace_with(replacement) ⇒ Object



119
120
121
# File 'lib/epitools/iter.rb', line 119

def replace_with(replacement)
  container[pos] = Elem.new(@iter, replacement)
end

#valueObject



149
150
151
# File 'lib/epitools/iter.rb', line 149

def value
  @val
end

#visited?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/epitools/iter.rb', line 92

def visited?
  @visited
end