lib/fixtury/definition_executor.rb in fixtury-0.4.1 vs lib/fixtury/definition_executor.rb in fixtury-1.0.0.beta1
- old
+ new
@@ -1,78 +1,49 @@
# frozen_string_literal: true
module Fixtury
+ # A container that manages the execution of a definition in the context of a store.
class DefinitionExecutor
- attr_reader :value, :execution_type, :definition, :store, :execution_context
+ attr_reader :value, :definition, :store
- def initialize(store: nil, execution_context: nil, definition:)
+ def initialize(store: nil, definition:)
@store = store
@definition = definition
- @execution_context = execution_context
- @execution_type = nil
@value = nil
end
- def __call
- maybe_set_store_context do
- provide_schema_hooks do
- run_callable(callable: definition.callable, type: :definition)
- definition.enhancements.each do |e|
- run_callable(callable: e, type: :enhancement)
- end
- end
- end
-
+ def call
+ run_definition
value
end
- def get(name)
- raise ArgumentError, "A store is required for #{definition.name}" unless store
-
- store.get(name, execution_context: execution_context)
- end
- alias [] get
-
- def method_missing(method_name, *args, &block)
- return super unless execution_context
-
- execution_context.send(method_name, *args, &block)
- end
-
- def respond_to_missing?(method_name)
- return super unless execution_context
-
- execution_context.respond_to?(method_name, true)
- end
-
private
- def run_callable(callable:, type:)
- @execution_type = type
+ # If the callable has a positive arity we generate a DependencyStore
+ # and yield it to the callable. Otherwise we just instance_eval the callable.
+ # We wrap the actual execution of the definition with a hook for observation.
+ def run_definition
+ callable = definition.callable
@value = if callable.arity.positive?
- instance_exec(self, &callable)
+ deps = build_dependency_store
+ ::Fixtury.hooks.call(:execution, self) do
+ instance_exec(deps, &callable)
+ end
else
- instance_eval(&callable)
+ ::Fixtury.hooks.call(:execution, self) do
+ instance_eval(&callable)
+ end
end
+ rescue Errors::Base
+ raise
+ rescue => e
+ raise Errors::DefinitionExecutionError.new(definition.pathname, e)
end
- def maybe_set_store_context
- return yield unless store
-
- store.with_relative_schema(definition.schema) do
- yield
- end
- end
-
- def provide_schema_hooks
- return yield unless definition.schema
-
- @value = definition.schema.around_fixture_hook(self) do
- yield
- value
- end
+ def build_dependency_store
+ DependencyStore.new(definition: definition, store: store)
end
end
end