Class: Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/src/extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(xml, yml, modifiers) ⇒ Extractor

Returns a new instance of Extractor.



182
183
184
185
186
187
# File 'lib/src/extractor.rb', line 182

def initialize(xml, yml, modifiers)
  @node_extractor = NodeExtractor.new(xml)
  @node_value_extractor = NodeValueExtractor.new(node_extractor)
  @path_manipulator = PathManipulator.new(node_value_extractor)
  @formatter = Format::Formatter.new(yml, modifiers)
end

Instance Method Details

#extract(node) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/src/extractor.rb', line 189

def extract(node)
  base, parent, tag, link, attribute = NodeParamsExtractor.new(node).extract
  path = PathBuilder.new(base: base, parent: parent, tag: tag).build

  if link.present?
    link_path = PathBuilder.new(base: base, parent: parent, tag: link).build

    if tag.is_a? Array
      tag = tag.map { |tag_path| replace_link(tag_path, link_path) }
    else
      path = replace_link(path, link_path)
    end
  end

  value = path_value(path, tag, attribute)
  format_value(value, node.props)
end

#format_value(value, props) ⇒ Object



211
212
213
# File 'lib/src/extractor.rb', line 211

def format_value(value, props)
  formatter.format_value(value, props)
end

#paths_of(base_path, tag_path, link_path = nil) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/src/extractor.rb', line 219

def paths_of(base_path, tag_path, link_path = nil)
  path = PathBuilder.new(base: base_path, tag: tag_path).build

  if link_path.present?
    link_path = PathBuilder.new(base: base_path, tag: link_path).build
    path = replace_link(path, link_path)
  end

  node = node_extractor.extract(path)
  (node || []).size.times.map do |index|
    "#{path}[#{index + 1}]"
  end
end


215
216
217
# File 'lib/src/extractor.rb', line 215

def replace_link(original_path, link_path)
  path_manipulator.replace_link(original_path, link_path)
end

#unescape!(path) ⇒ Object



207
208
209
# File 'lib/src/extractor.rb', line 207

def unescape!(path)
  node_extractor.unescape!(path)
end

#uniq_paths(paths, uniq_by_path) ⇒ Object



233
234
235
236
237
# File 'lib/src/extractor.rb', line 233

def uniq_paths(paths, uniq_by_path)
  return paths if uniq_by_path.blank?

  path_manipulator.uniq_paths(paths, uniq_by_path)
end