Sha256: 9e64c4b3fdbdc24e0ada5ba063b81db93a857af09b74c91ae5329fc1f66b7935

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

require 'marc'
require 'marc/spec/queries/query'

module MARC
  module Spec
    module Queries
      class QueryExecutor
        include Part

        attr_reader :marc_record, :root_query, :root_tag, :root_fields

        def initialize(marc_record, root, cache = {})
          @marc_record = ensure_type(marc_record, MARC::Record)
          @root_query = as_query(root)
          @cache = cache

          @root_tag = root_query.tag || Tag.new('...')
          @root_fields = apply_tag(root_tag)
        end

        def execute
          root_query.execute(self, root_fields)
        end

        def apply_tag(tag)
          cache[tag] ||= tag.apply(marc_record)
        end

        def apply_selector(selector, field)
          return [field] unless selector

          cache_key = [selector, field]
          cache[cache_key] ||= selector.apply(field)
        end

        def condition_met?(condition, context_field, context_result)
          cond_ctx = ConditionContext.new(context_field, context_result, self)
          condition.met?(cond_ctx)
        end

        private

        def as_query(root)
          # TODO: unify these?
          return root if root.is_a?(Query)
          return Query.new(tag: root) if root.is_a?(Tag)
        end

        attr_reader :cache
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-marc-spec-0.1.1 lib/marc/spec/queries/query_executor.rb