class Tomlrb::Handler
Attributes
Public Class Methods
Source
# File lib/tomlrb/handler.rb, line 7 def initialize(**options) @output = {} @current = @output @stack = [] @array_names = [] @current_table = [] @keys = Keys.new @symbolize_keys = options[:symbolize_keys] end
Public Instance Methods
Source
# File lib/tomlrb/handler.rb, line 60 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
Source
# File lib/tomlrb/handler.rb, line 39 def deal_with_array_of_tables(identifiers, is_array_of_tables) 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
Source
# File lib/tomlrb/handler.rb, line 108 def end_(type) array = [] while (value = @stack.pop) != [type] raise ParseError, 'Unclosed table' if @stack.empty? array.unshift(value) end array end
Source
# File lib/tomlrb/handler.rb, line 73 def push_inline(inline_arrays) merged_inline = {} keys = Keys.new inline_arrays.each do |inline_array| current = merged_inline value = inline_array.pop keys.add_table_key inline_array, value.is_a?(Array) inline_array.each_with_index do |inline_key, inline_index| inline_key = inline_key.to_sym if @symbolize_keys last_key = inline_index == inline_array.size - 1 if last_key if current[inline_key].nil? keys.add_pair_key [inline_key], [] current[inline_key] = value else raise Key::KeyConflict, "Inline key #{inline_key} is already used" end else current[inline_key] ||= {} current = current[inline_key] end end end push(merged_inline) end
Source
# File lib/tomlrb/handler.rb, line 17 def set_context(identifiers, is_array_of_tables: false) if identifiers.empty? raise ParseError, 'Array needs a name' end @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 = @current[k].last else @current[k] ||= {} @current = @current[k] end end end end
Source
# File lib/tomlrb/handler.rb, line 118 def validate_value(value) if value.nil? raise ParseError, 'Value must be present' end end
Private Instance Methods
Source
# File lib/tomlrb/handler.rb, line 126 def assign_key_path(current, key, key_emptied) existed = current.key?(key) raise ParseError, "Cannot overwrite value with key #{key}" if existed && !current[key].is_a?(Hash) if key_emptied raise ParseError, "Cannot overwrite value with key #{key}" unless current.is_a?(Hash) value = @stack.pop raise ParseError, "Cannot overwrite value with key #{key}" if current[key].is_a?(Hash) && !value.is_a?(Hash) current[key] = value return current end current[key] ||= {} current[key] end