class Tomlrb::Handler

Attributes

output[R]
symbolize_keys[R]

Public Class Methods

new(**options) click to toggle source
# File lib/tomlrb/handler.rb, line 5
def initialize(**options)
  @output = {}
  @current = @output
  @stack = []
  @array_names = []
  @current_table = []
  @keys = Keys.new
  @symbolize_keys = options[:symbolize_keys]
end

Public Instance Methods

assign(k) click to toggle source
# File lib/tomlrb/handler.rb, line 56
def assign(k)
  @keys.add_pair_key k, @current_table
  current = @current
  while key = k.shift
    key = key.to_sym if @symbolize_keys
    current = assign_key_path(current, key, k.empty?)
  end
end
deal_with_array_of_tables(identifiers, is_array_of_tables) { |identifiers| ... } click to toggle source
# File lib/tomlrb/handler.rb, line 34
def deal_with_array_of_tables(identifiers, is_array_of_tables)
  identifiers.map!{|n| n.gsub("\"", '')}
  stringified_identifier = identifiers.join('.')

  if is_array_of_tables
    @array_names << stringified_identifier
    last_identifier = identifiers.pop
  elsif @array_names.include?(stringified_identifier)
    raise ParseError, 'Cannot define a normal table with the same name as an already established array'
  end

  yield(identifiers)

  if is_array_of_tables
    last_identifier = last_identifier.to_sym if @symbolize_keys
    @current[last_identifier] ||= []
    raise ParseError, "Cannot use key #{last_identifier} for both table and array at once" unless @current[last_identifier].respond_to?(:<<)
    @current[last_identifier] << {}
    @current = @current[last_identifier].last
  end
end
end_(type) click to toggle source
# File lib/tomlrb/handler.rb, line 73
def end_(type)
  array = []
  while (value = @stack.pop) != [type]
    raise ParseError, 'Unclosed table' if value.nil?
    array.unshift(value)
  end
  array
end
push(o) click to toggle source
# File lib/tomlrb/handler.rb, line 65
def push(o)
  @stack << o
end
set_context(identifiers, is_array_of_tables: false) click to toggle source
# File lib/tomlrb/handler.rb, line 15
def set_context(identifiers, is_array_of_tables: false)
  @current_table = identifiers.dup
  @keys.add_table_key identifiers, is_array_of_tables
  @current = @output

  deal_with_array_of_tables(identifiers, is_array_of_tables) do |identifierz|
    identifierz.each do |k|
      k = k.to_sym if @symbolize_keys
      if @current[k].is_a?(Array)
        @current[k] << {} if @current[k].empty?
        @current = @current[k].last
      else
        @current[k] ||= {}
        @current = @current[k]
      end
    end
  end
end
start_(type) click to toggle source
# File lib/tomlrb/handler.rb, line 69
def start_(type)
  push([type])
end

Private Instance Methods

assign_key_path(current, key, key_emptied) click to toggle source
# File lib/tomlrb/handler.rb, line 84
def assign_key_path(current, key, key_emptied)
  if key_emptied
    raise ParseError, "Cannot overwrite value with key #{key}" unless current.kind_of?(Hash)
    current[key] = @stack.pop
    return current
  end
  current[key] ||= {}
  current = current[key]
  current
end