Module: ToonFu

Defined in:
lib/toon_fu.rb,
lib/toon_fu/fields.rb,
lib/toon_fu/writer.rb,
lib/toon_fu/encoder.rb,
lib/toon_fu/version.rb,
lib/toon_fu/encodable.rb,
lib/toon_fu/normalizer.rb,
lib/toon_fu/float_literal.rb,
lib/toon_fu/string_literal.rb,
lib/toon_fu/decimal_literal.rb

Overview

TOON (Token-Oriented Object Notation) for Ruby.

Defined Under Namespace

Modules: Encodable Classes: Encoder, Error

Constant Summary collapse

VERSION =
"4.1.2"

Class Method Summary collapse

Class Method Details

.encode(value = (missing = true), **options) ⇒ String

Encodes a value as TOON.

ToonFu.encode("hello")                # => "hello"
ToonFu.encode("a,b")                  # => "\"a,b\""
ToonFu.encode("a,b", delimiter: "|")  # => "a,b"
ToonFu.encode(1e-7)                   # => "1e-7"
ToonFu.encode({user: {id: 1}})        # => "user:\n  id: 1"
ToonFu.encode({tags: ["a", "b"]})     # => "tags[2]: a,b"
ToonFu.encode([{id: 1}, {id: 2}])     # => "[2]{id}:\n  1\n  2"
ToonFu.encode({a: {x: 1}, b: {x: 2}}) # => "[2:]{x}:\n  a: 1\n  b: 2"

Parameters:

Returns:

  • (String)

Raises:



40
41
42
43
44
# File 'lib/toon_fu.rb', line 40

def self.encode(value = (missing = true), **options)
  raise ArgumentError, "ToonFu.encode takes the value as its first argument; wrap a Hash in braces: ToonFu.encode({key: value})" if missing

  (options.empty? ? DEFAULT_ENCODER : Encoder.new(**options)).encode(value)
end