class ToJbuilder::Emitter
Constants
- ARRAY_END
- ARRAY_START
- DOCUMENT_END
- DOCUMENT_START
- EMPTY
- HASH_END
- HASH_START
- PAIR_KEY
- PAIR_VALUE
Public Class Methods
new(io, resource_name)
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 52 def initialize(io, resource_name) @io, @resource_name = io, resource_name @jbuilder_array = nil @jbuilder_lines = [] @nesting = [Nesting.new(@resource_name, :root)] @trace = [] end
Public Instance Methods
alias(anchor)
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 164 def alias(anchor) # nothing to do... end
end_document(implicit_end)
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 70 def end_document(implicit_end) if @trace.include?(DOCUMENT_END) raise "#{__method__} was called more than once." else @trace << DOCUMENT_END end end
end_mapping()
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 98 def end_mapping flush! @nesting.last.sequence_processed = true if within_array? && !@nesting.last.sequence_processed? if @nesting.size > 1 && !within_array? @nesting.pop @io.write "#{indent}end\n" end @trace << HASH_END end
end_sequence()
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 125 def end_sequence case @trace.last when ARRAY_START @nesting.pop @jbuilder_lines << @jbuilder_array.line unless @nesting.last.sequence_processed? when PAIR_VALUE @nesting.pop @jbuilder_lines << @jbuilder_array.line when HASH_END @nesting.pop @io.write "#{indent}end\n" unless @nesting.last.sequence_processed? end @trace << ARRAY_END end
end_stream()
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 61 def end_stream # nothing to do... end
scalar(value, anchor, tag, plain, quoted, style)
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 141 def scalar(value, anchor, tag, plain, quoted, style) return if @nesting.last.sequence_processed? case @trace.last when DOCUMENT_START, HASH_START, HASH_END, ARRAY_END, PAIR_VALUE line_break = @trace.last == ARRAY_END || @trace.last == HASH_END prefix = within_array? ? "" : "@" @jbuilder_lines << JbuilderLine.new(@nesting.last.name, value, nil, prefix, indent, line_break) @trace << PAIR_KEY when ARRAY_START @nesting.last.sequence_processed = true @trace << PAIR_VALUE when PAIR_KEY @jbuilder_lines.last.value = value @trace << PAIR_VALUE else raise "Syntax error." end end
start_document(version, tag_directives, implicit)
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 65 def start_document(version, tag_directives, implicit) raise "#{__method__} was called more than once." if @trace.include?(DOCUMENT_START) @trace << DOCUMENT_START end
start_mapping(anchor, tag, implicit, style)
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 78 def start_mapping(anchor, tag, implicit, style) case @trace.last when ARRAY_START flush! unless @nesting.last.sequence_processed? @io.write "\n" if @trace[-3] != ARRAY_END && @trace[-3] != HASH_START @io.write @jbuilder_array.to_jbuilder end when PAIR_KEY line = @jbuilder_lines.pop flush! line.line_break = true if @trace[-2] != HASH_START @io.write line.to_hash_key @nesting.push Nesting.new(line.key.singularize, HASH_START) end @trace << HASH_START end
start_sequence(anchor, tag, implicit, style)
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 110 def start_sequence(anchor, tag, implicit, style) case @trace.last when PAIR_KEY line = @jbuilder_lines.pop line.line_break = false if @trace[-2] == HASH_END @jbuilder_array = JbuilderArray.new(line) when DOCUMENT_START @jbuilder_array = JbuilderArray.new(nil, @nesting.last.name) end @nesting.push Nesting.new((@jbuilder_array.line || @jbuilder_array).key.singularize, ARRAY_START, @nesting.last.sequence_processed) @trace << ARRAY_START end
Private Instance Methods
flush!()
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 178 def flush! key_length = @jbuilder_lines.map(&:key).map(&:length).sort.last @jbuilder_lines.each do |line| @io.write line.to_jbuilder(key_length) end @jbuilder_lines.clear end
indent()
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 174 def indent EMPTY * (@nesting.size - 1) * 2 end
within_array?()
click to toggle source
# File lib/to_jbuilder/emitter.rb, line 170 def within_array? @nesting.last.type == ARRAY_START end