Sha256: b8f04725e03e43ddda3640037fcc962e4546c8eb9aa69e9be139647012afabe5

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require_relative "base"
require_relative "array_value"
require_relative "array_of"
require_relative "hash_builder"
require_relative "string_value"
require_relative "value_builder"
require_relative "within"
require_relative "expression"
require_relative "unescape"

module Extract
  class ValueBuilder < Base
    def value
      props = node.props
      case props
      when String then value_for_string
      when Array then value_for_array
      when Hash then value_for_hash
      else
        raise "Invalid kind #{props.class} (#{props})"
      end
    end

    private

    def value_for_hash
      props = node.props
      
      Unescape.new(node, extractor).unescape! if props[:unescape]

      fixed_value = props[:fixed]
      return fixed_value if fixed_value
      return ArrayOf.new(node, extractor).value if props[:array_of]
      return Within.new(node, extractor).value if props[:within]
      return StringValue.new(node, extractor).value if (props.keys & %i[path attr]).any?

      HashBuilder.new(node, extractor).value
    end

    def value_for_string
      StringValue.new(Node.new({ path: node.props }, node.path), extractor).value
    end

    def value_for_array
      ArrayValue.new(node, extractor).value
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xml_data_extractor-0.5.0 lib/src/extract/value_builder.rb
xml_data_extractor-0.4.0 lib/src/extract/value_builder.rb