Class: Tamiyo::YamlHelper::EventStream

Inherits:
Object
  • Object
show all
Defined in:
lib/tamiyo/yaml/yaml_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue) ⇒ EventStream

Returns a new instance of EventStream.



97
98
99
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 97

def initialize(queue)
  @queue = queue
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



94
95
96
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 94

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



95
96
97
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 95

def value
  @value
end

Instance Method Details

#assume_endObject

Raises:

  • (StopIteration)


146
147
148
149
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 146

def assume_end
  self.next
  raise StopIteration, "Assumed end but found #{@type}" unless @type == :end
end

#assume_mappingObject

Raises:

  • (StopIteration)


141
142
143
144
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 141

def assume_mapping
  self.next
  raise StopIteration, "Assumed map but found #{@type}" unless @type == :map
end

#assume_scalarObject

Raises:

  • (StopIteration)


130
131
132
133
134
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 130

def assume_scalar
  self.next
  raise StopIteration, "Assumed scalar but found #{@type}" unless @type == :scalar
  @value
end

#assume_sequenceObject

Raises:

  • (StopIteration)


136
137
138
139
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 136

def assume_sequence
  self.next
  raise StopIteration, "Assumed seq but found #{@type}" unless @type == :seq
end

#each_within_mappingObject



109
110
111
112
113
114
115
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 109

def each_within_mapping
  assume_mapping
  until peak_next_is_end
    yield
  end
  assume_end
end

#each_within_sequenceObject



101
102
103
104
105
106
107
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 101

def each_within_sequence
  assume_sequence
  until peak_next_is_end
    yield
  end
  assume_end
end

#nextObject



156
157
158
159
160
161
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 156

def next
  event = @peak || @queue.pop
  @peak = nil
  @type, @value = event
  event
end

#on_keys(cases) ⇒ Object

Raises:

  • (StopIteration)


124
125
126
127
128
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 124

def on_keys(cases)
  value = assume_scalar
  raise StopIteration, "No case for key #{value} available" unless cases.include? value
  cases[value][]
end

#on_types(cases) ⇒ Object

Raises:

  • (StopIteration)


117
118
119
120
121
122
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 117

def on_types(cases)
  type, _ = peak
  raise StopIteration, "No case for type #{type} available" unless cases.include? type
  self.next if type == :scalar
  cases[type][]
end

#peakObject



163
164
165
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 163

def peak
  @peak ||= @queue.pop
end

#peak_next_is_endObject



151
152
153
154
# File 'lib/tamiyo/yaml/yaml_helper.rb', line 151

def peak_next_is_end
  type, _ = peak
  type == :end
end