module PSON::Pure::Generator::GeneratorMethods::Hash

Public Instance Methods

to_pson(state = nil, depth = 0, *) click to toggle source

Returns a PSON string containing a PSON object, that is unparsed from this Hash instance. state is a PSON::State object, that can also be used to configure the produced PSON string output further. depth is used to find out nesting depth, to indent accordingly.

# File lib/octocatalog-diff/external/pson/pure/generator.rb, line 208
def to_pson(state = nil, depth = 0, *)
  if state
    state = PSON.state.from_state(state)
    state.check_max_nesting(depth)
    pson_check_circular(state) { pson_transform(state, depth) }
  else
    pson_transform(state, depth)
  end
end

Private Instance Methods

pson_check_circular(state) { || ... } click to toggle source
# File lib/octocatalog-diff/external/pson/pure/generator.rb, line 220
def pson_check_circular(state)
  if state and state.check_circular?
    state.seen?(self) and raise PSON::CircularDatastructure,
      "circular data structures not supported!"
    state.remember self
  end
  yield
ensure
  state and state.forget self
end
pson_shift(state, depth) click to toggle source
# File lib/octocatalog-diff/external/pson/pure/generator.rb, line 231
def pson_shift(state, depth)
  state and not state.object_nl.empty? or return ''
  state.indent * depth
end
pson_transform(state, depth) click to toggle source
# File lib/octocatalog-diff/external/pson/pure/generator.rb, line 236
def pson_transform(state, depth)
  delim = ','
  if state
    delim << state.object_nl
    result = '{'
    result << state.object_nl
    result << map { |key,value|
      s = pson_shift(state, depth + 1)
      s << key.to_s.to_pson(state, depth + 1)
      s << state.space_before
      s << ':'
      s << state.space
      s << value.to_pson(state, depth + 1)
    }.join(delim)
    result << state.object_nl
    result << pson_shift(state, depth)
    result << '}'
  else
    result = '{'
    result << map { |key,value|
      key.to_s.to_pson << ':' << value.to_pson
    }.join(delim)
    result << '}'
  end
  result
end