Class: ToonMyJson::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/toon_my_json/decoder.rb

Overview

Decodes TOON format back to Ruby objects

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent: 2, delimiter: ',') ⇒ Decoder

Returns a new instance of Decoder.



8
9
10
11
# File 'lib/toon_my_json/decoder.rb', line 8

def initialize(indent: 2, delimiter: ',')
  @indent_size = indent
  @delimiter = delimiter
end

Instance Attribute Details

#current_lineObject (readonly)

Returns the value of attribute current_line.



6
7
8
# File 'lib/toon_my_json/decoder.rb', line 6

def current_line
  @current_line
end

#delimiterObject (readonly)

Returns the value of attribute delimiter.



6
7
8
# File 'lib/toon_my_json/decoder.rb', line 6

def delimiter
  @delimiter
end

#linesObject (readonly)

Returns the value of attribute lines.



6
7
8
# File 'lib/toon_my_json/decoder.rb', line 6

def lines
  @lines
end

Instance Method Details

#decode(toon_string) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/toon_my_json/decoder.rb', line 13

def decode(toon_string)
  @lines = toon_string.split("\n")
  @current_line = 0

  # Detect if it's a single line
  if @lines.length == 1
    content = @lines[0].strip

    # Check if it's a key-value (check this first!)
    return parse_hash(0) if key_value_line?(content)

    # Check if it's a primitive array (contains delimiter but not quotes around everything)
    return parse_primitive_array(content) if content.include?(@delimiter) && !content.match(/^".*"$/)

    # Otherwise it's a single primitive
    return parse_primitive(content)
  end

  # Multi-line parsing
  parse_value(0)
end