Module: ToonFormat

Defined in:
lib/toon_format.rb,
lib/toon_format/errors.rb,
lib/toon_format/parser.rb,
lib/toon_format/decoder.rb,
lib/toon_format/encoder.rb,
lib/toon_format/railtie.rb,
lib/toon_format/version.rb,
lib/toon_format/validator.rb,
lib/toon_format/rails/extensions.rb

Defined Under Namespace

Modules: Rails Classes: DecodeError, Decoder, EncodeError, Encoder, Error, ParseError, Parser, Railtie, ValidationError, Validator

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.decode(toon_string, strict: true) ⇒ Object

Decode TOON format string to Ruby object

Parameters:

  • toon_string (String)

    TOON formatted string

  • strict (Boolean) (defaults to: true)

    Enable strict validation (default: true)

Returns:

  • (Object)

    Decoded Ruby object

Raises:



41
42
43
# File 'lib/toon_format.rb', line 41

def decode(toon_string, strict: true)
  Decoder.decode(toon_string, strict: strict)
end

.encode(data, **options) ⇒ String

Encode Ruby object to TOON format string

Parameters:

  • data (Object)

    Ruby object to encode

  • options (Hash)

    Encoding options

Options Hash (**options):

  • :delimiter (String) — default: ','

    Field delimiter

  • :indent (Integer) — default: 2

    Indentation spaces

  • :length_marker (Boolean) — default: true

    Include array lengths

Returns:

  • (String)

    TOON formatted string

Raises:



28
29
30
# File 'lib/toon_format.rb', line 28

def encode(data, **options)
  Encoder.encode(data, options)
end

.estimate_savings(data) ⇒ Hash

Estimate token savings compared to JSON

Parameters:

  • data (Object)

    Ruby object to analyze

Returns:

  • (Hash)

    Statistics including token counts and savings percentage



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/toon_format.rb', line 50

def estimate_savings(data)
  json_str = JSON.generate(data)
  toon_str = encode(data)

  json_size = json_str.bytesize
  toon_size = toon_str.bytesize

  {
    json_tokens: estimate_tokens(json_str),
    toon_tokens: estimate_tokens(toon_str),
    savings_percent: ((json_size - toon_size) / json_size.to_f * 100).round(1),
    json_size: json_size,
    toon_size: toon_size
  }
end