Class: ToonFormat::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/toon_format/encoder.rb

Overview

Encodes Ruby objects to TOON format strings

Constant Summary collapse

DEFAULT_OPTIONS =
{
  delimiter: ",",
  indent: 2,
  length_marker: true
}.freeze
MAX_DEPTH =
100
MAX_ARRAY_SIZE =
100_000

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Encoder

Returns a new instance of Encoder.



24
25
26
27
28
# File 'lib/toon_format/encoder.rb', line 24

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  @indent_level = 0
  @visited = Set.new
end

Class Method Details

.encode(data, options = {}) ⇒ String

Encode Ruby object to TOON format string

Parameters:

  • data (Object)

    Ruby object to encode

  • options (Hash) (defaults to: {})

    Encoding options

Returns:

  • (String)

    TOON formatted string



20
21
22
# File 'lib/toon_format/encoder.rb', line 20

def self.encode(data, options = {})
  new(options).encode(data)
end

Instance Method Details

#encode(data, depth = 0) ⇒ Object

Main encode method



31
32
33
34
# File 'lib/toon_format/encoder.rb', line 31

def encode(data, depth = 0)
  check_depth(depth)
  encode_value(data, depth)
end