Class: Tomlrb::Handler
- Inherits:
-
Object
- Object
- Tomlrb::Handler
- Defined in:
- 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
- #set_context(identifiers, is_array_of_tables: false) ⇒ Object
- #start_(type) ⇒ Object
Constructor Details
#initialize(**options) ⇒ Handler
Returns a new instance of Handler.
5 6 7 8 9 10 11 |
# File 'lib/tomlrb/handler.rb', line 5 def initialize(**) @output = {} @current = @output @stack = [] @array_names = [] @symbolize_keys = [:symbolize_keys] end |
Instance Attribute Details
#output ⇒ Object (readonly)
Returns the value of attribute output.
3 4 5 |
# File 'lib/tomlrb/handler.rb', line 3 def output @output end |
#symbolize_keys ⇒ Object (readonly)
Returns the value of attribute symbolize_keys.
3 4 5 |
# File 'lib/tomlrb/handler.rb', line 3 def symbolize_keys @symbolize_keys end |
Instance Method Details
#assign(k) ⇒ Object
51 52 53 54 |
# File 'lib/tomlrb/handler.rb', line 51 def assign(k) k = k.to_sym if @symbolize_keys @current[k] = @stack.pop end |
#deal_with_array_of_tables(identifiers, is_array_of_tables) {|identifiers| ... } ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tomlrb/handler.rb', line 30 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] ||= [] @current[last_identifier] << {} @current = @current[last_identifier].last end end |
#end_(type) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/tomlrb/handler.rb', line 64 def end_(type) array = [] while (value = @stack.pop) != [type] raise ParseError, 'Unclosed table' if value.nil? array.unshift(value) end array end |
#push(o) ⇒ Object
56 57 58 |
# File 'lib/tomlrb/handler.rb', line 56 def push(o) @stack << o end |
#set_context(identifiers, is_array_of_tables: false) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/tomlrb/handler.rb', line 13 def set_context(identifiers, is_array_of_tables: false) @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) ⇒ Object
60 61 62 |
# File 'lib/tomlrb/handler.rb', line 60 def start_(type) push([type]) end |