Sha256: 013e6c4723e63e1aae5dd269f9725d1898c46e7a06f5f190c7a36b0d9eb916e4

Contents?: true

Size: 931 Bytes

Versions: 2

Compression:

Stored size: 931 Bytes

Contents

# frozen_string_literal: true

module Extract
  class HashBuilder < Base
    INTERNAL_FIELDS = %i[array_of keep_if within unescape].freeze

    def value(index = 0)
      path, props = node.to_h.values_at(:path, :props)

      hash = {}
      props.each do |field_name, nested_props|
        next unless valuable_field? field_name, nested_props, index

        value = ValueBuilder.new(Node.new(nested_props, path), extractor).value
        hash[field_name.to_sym] = value if value.present?
      end

      keep_hash?(hash, props) ? hash : nil
    end

    private

    def keep_hash?(hash, props)
      expression = props[:keep_if]
      expression.present? ? Expression.new(expression, hash).evaluate : true
    end

    def valuable_field?(field_name, props, index)
      return false if INTERNAL_FIELDS.include? field_name
      return false if index.positive? && Node.new(props, "").first_only?

      true
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xml_data_extractor-0.7.0 lib/src/extract/hash_builder.rb
xml_data_extractor-0.6.0 lib/src/extract/hash_builder.rb