Class: Toml::Body

Inherits:
Treetop::Runtime::SyntaxNode
  • Object
show all
Defined in:
lib/tomlp/token_extensions.rb

Instance Method Summary collapse

Instance Method Details

#evaluateObject



136
137
138
# File 'lib/tomlp/token_extensions.rb', line 136

def evaluate
  parse(self.elements.map {|x| x.evaluate})
end

#parse(ds) ⇒ Object

TODO

Refactor this method



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tomlp/token_extensions.rb', line 141

def parse(ds)
  final_hash = {}
  current_key, inner_key = nil, nil
  current_hash = final_hash
  ds.each_with_index do |value, index|
    if value.is_a? Hash
      if current_key == nil
        current_hash.merge!(value)
      elsif inner_key
        current_hash[inner_key].merge!(value)
      else
        current_hash[current_key].merge!(value)
      end
    elsif value.is_a? Array
      value = value.first
      parent_key = value.count > 1 ? value[-2] : value.first
      if parent_key == current_key
        current_hash = final_hash[current_key]
        inner_key = value.last
        current_hash[inner_key] = {}
      elsif inner_key && parent_key == inner_key
        current_hash = current_hash[inner_key]
        inner_key = value.last
        current_hash[inner_key] = {}
      elsif value.count > 1
        value.each_with_index do |key, nindex|
          current_hash[key] = {}
          current_hash = current_hash[key] if nindex + 1 < value.count
        end
        inner_key = value.last
        current_key = value.first
      else
        current_hash = final_hash
        current_key = value.first
        inner_key = nil
        final_hash[current_key] = {}
      end
    end
  end
  final_hash
end