Sha256: dd0c34b89456b5eec84582d65e79a6d614309e7c7148c0d94a515b0205d87b7f

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 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

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

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

        def root_fields
          @root_fields ||= apply_tag(root_tag)
        end

        def execute
          root_query.execute(self, root_fields)
        end

        def any_results?
          root_query.any_results?(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

        attr_reader :cache
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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