Class: Tomlrb::Handler
- Inherits:
-
Object
- Object
- Tomlrb::Handler
- Defined in:
- lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#symbolize_keys ⇒ Object
readonly
Returns the value of attribute symbolize_keys.
Instance Method Summary collapse
- #assign(k) ⇒ Object
- #deal_with_array_of_tables(identifiers, is_array_of_tables) {|identifiers| ... } ⇒ Object
- #end_(type) ⇒ Object
-
#initialize(**options) ⇒ Handler
constructor
A new instance of Handler.
- #push(o) ⇒ Object
- #push_inline(inline_arrays) ⇒ Object
- #set_context(identifiers, is_array_of_tables: false) ⇒ Object
- #start_(type) ⇒ Object
- #validate_value(value) ⇒ Object
Constructor Details
#initialize(**options) ⇒ Handler
Returns a new instance of Handler.
7 8 9 10 11 12 13 14 15 |
# File 'lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb', line 7 def initialize(**) @output = {} @current = @output @stack = [] @array_names = [] @current_table = [] @keys = Keys.new @symbolize_keys = [:symbolize_keys] end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
5 6 7 |
# File 'lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb', line 5 def output @output end |
#symbolize_keys ⇒ Object (readonly)
Returns the value of attribute symbolize_keys.
5 6 7 |
# File 'lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb', line 5 def symbolize_keys @symbolize_keys end |
Instance Method Details
#assign(k) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/llm/shell/internal/tomlrb/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 |
#deal_with_array_of_tables(identifiers, is_array_of_tables) {|identifiers| ... } ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/llm/shell/internal/tomlrb/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 |
#end_(type) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb', line 103 def end_(type) array = [] while (value = @stack.pop) != [type] raise ParseError, 'Unclosed table' if @stack.empty? array.unshift(value) end array end |
#push(o) ⇒ Object
69 70 71 |
# File 'lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb', line 69 def push(o) @stack << o end |
#push_inline(inline_arrays) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb', line 73 def push_inline(inline_arrays) merged_inline = {} inline_arrays.each do |inline_array| current = merged_inline value = inline_array.pop 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? 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 |
#set_context(identifiers, is_array_of_tables: false) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/llm/shell/internal/tomlrb/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 |
#start_(type) ⇒ Object
99 100 101 |
# File 'lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb', line 99 def start_(type) push([type]) end |
#validate_value(value) ⇒ Object
113 114 115 116 117 |
# File 'lib/llm/shell/internal/tomlrb/lib/tomlrb/handler.rb', line 113 def validate_value(value) if value.nil? raise ParseError, 'Value must be present' end end |