Sha256: f69e0a8bec98f6d0c5b646fad0f21cded9921007e0fb3662c474ee5b1de95fd0

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

module Extract
  class ArrayOf < Base
    def initialize(node, extractor, index = 0)
      super(node, extractor)
      @index = index
    end

    def value
      process_paths.flatten.compact
    end

    private

    attr_reader :index

    def array_items
      arr_path, link_path, uniq_by = node.array_of_paths

      paths = extractor.paths_of(node.path, arr_path, link_path)
      paths = uniq_paths(paths, uniq_by) if uniq_by

      paths.each_with_index.map do |path, idx|
        HashBuilder.new(Node.new(node.props, path), extractor).value(index + idx)
      end.compact
    end

    def process_paths
      paths = paths_from_props

      if paths.size > 1
        process_path(paths.shift, paths)
      else
        node.props[:array_of] = paths.first
        array_items
      end
    end

    def process_path(path, inner_paths)
      path = build_path(path) if path.is_a?(Hash)

      extractor.paths_of(node.path, path).each_with_index.map do |some, idx|
        ArrayOf.new(Node.new(node.props.merge(array_of: inner_paths), some), extractor, index + idx).value
      end
    end

    def uniq_paths(paths, uniq_by)
      extractor.uniq_paths(paths, uniq_by)
    end

    def build_path(hash)
      extractor.replace_link(hash[:path], [node.path, hash[:link]].join("/"))
    end

    def paths_from_props
      [node.props[:array_of]].flatten
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
xml_data_extractor-0.5.0 lib/src/extract/array_of.rb
xml_data_extractor-0.4.0 lib/src/extract/array_of.rb
xml_data_extractor-0.3.0 lib/src/extract/array_of.rb
xml_data_extractor-0.2.0 lib/src/extract/array_of.rb
xml_data_extractor-0.1.0 lib/src/extract/array_of.rb