Class: JsonProjection::Fifo

Inherits:
Object
  • Object
show all
Defined in:
lib/json-projection/parser/fifo.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack = []) ⇒ Fifo

Returns a new instance of Fifo.



12
13
14
# File 'lib/json-projection/parser/fifo.rb', line 12

def initialize(stack = [])
  @stack = stack
end

Class Method Details

.emptyObject



4
5
6
# File 'lib/json-projection/parser/fifo.rb', line 4

def self.empty
  @empty ||= self.new
end

.pure(val) ⇒ Object



8
9
10
# File 'lib/json-projection/parser/fifo.rb', line 8

def self.pure(val)
  Fifo.new([val])
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
36
# File 'lib/json-projection/parser/fifo.rb', line 33

def ==(other)
  return false unless other.is_a?(Fifo)
  return stack == other.stack
end

#append(fifo) ⇒ Object



42
43
44
45
46
# File 'lib/json-projection/parser/fifo.rb', line 42

def append(fifo)
  return self if fifo.empty?
  return fifo if self.empty?
  Fifo.new(@stack.concat(fifo.stack))
end

#empty?Boolean

Returns:



29
30
31
# File 'lib/json-projection/parser/fifo.rb', line 29

def empty?
  @stack.empty?
end

#hashObject



38
39
40
# File 'lib/json-projection/parser/fifo.rb', line 38

def hash
  stack.hash
end

#popObject



20
21
22
23
24
25
26
27
# File 'lib/json-projection/parser/fifo.rb', line 20

def pop()
  return self, nil if empty?

  init = @stack.slice(0, @stack.size - 1)
  last = @stack.last

  return Fifo.new(init), last
end

#push(val) ⇒ Object



16
17
18
# File 'lib/json-projection/parser/fifo.rb', line 16

def push(val)
  Fifo.new(@stack.dup.insert(0, val))
end