Sha256: 5401abeb75e59d441f6d58eff454d6e841709e42b223c84db7d635554d4e0aab

Contents?: true

Size: 1.92 KB

Versions: 50

Compression:

Stored size: 1.92 KB

Contents

module Appfuel
  # Used in the validation system by custom predicates to ask the question if
  # an entity exists in the database
  module Repository
    class Runner
      attr_reader :repo_namespace, :criteria_class

      # The call relies on the fact that we can build a criteria find the
      # correct repo and call the exists? interface on that repo. The identity
      # of any given repo requires its namespace + its class name.
      #
      # @param namespace [String] fully qualified namespace string fro repos
      # @param criteria_class [Class] class used to represent the criteria
      # @returns [ExistsInDbRunner]
      def initialize(namespace, criteria_class)
        @repo_namespace = namespace
        @criteria_class = criteria_class
      end

      def create_criteria(entity_key, opts = {})
        criteria_class.new(entity_key, opts)
      end

      def query(criteria)
        load_repo(criteria).query(criteria)
      end

      # @param entity_key [String] the type identifier for an entity
      # @param opts [Hash] one attr => value pair is required
      #                        repo => name is optional
      #
      # @return [Bool]
      def exists?(entity_key, opts = {})
        fail "opts must be a hash" unless opts.is_a?(Hash)

        criteria_opts = {}
        if opts.key?(:repo)
          criteria_opts[:repo] = opts.delete(:repo)
        end
        fail "opts hash must have one attr => value pair" if opts.empty?

        property, value = opts.first
        criteria = create_criteria(entity_key, criteria_opts)
        criteria.exists(property, value)

        load_repo(criteria).exists?(criteria)
      end


      private

      def load_repo(criteria)
        klass  = "#{repo_namespace}::#{criteria.repo_name}"
        unless Kernel.const_defined?(klass)
          fail "RepositoryRunner: failed - repo #{klass} not defined"
        end

        Kernel.const_get(klass).new
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
appfuel-0.7.0 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.16 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.15 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.14 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.13 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.12 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.11 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.10 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.9 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.8 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.7 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.6 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.5 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.4 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.3 lib/appfuel/storage/repository/runner.rb
appfuel-0.6.1 lib/appfuel/storage/repository/runner.rb
appfuel-0.5.16 lib/appfuel/storage/repository/runner.rb
appfuel-0.5.15 lib/appfuel/storage/repository/runner.rb
appfuel-0.5.14 lib/appfuel/storage/repository/runner.rb
appfuel-0.5.13 lib/appfuel/storage/repository/runner.rb