Skip to content

Commit

Permalink
bump to beta7
Browse files Browse the repository at this point in the history
  • Loading branch information
mnelson committed Mar 13, 2024
1 parent cd5729d commit f61bcfd
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fixtury (1.0.0.beta6)
fixtury (1.0.0.beta7)

GEM
remote: https://rubygems.org/
Expand Down
35 changes: 29 additions & 6 deletions lib/fixtury/definition_executor.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
# frozen_string_literal: true

require "benchmark"

module Fixtury
# A container that manages the execution of a definition in the context of a store.
class DefinitionExecutor

attr_reader :value, :definition, :store
class Output

attr_accessor :value, :metadata

def initialize
@value = nil
@metadata = {}
end

end

attr_reader :output, :definition, :store

def initialize(store: nil, definition:)
@store = store
@definition = definition
@value = nil
@output = Output.new
end

def call
run_definition
value
output
end

private
Expand All @@ -25,13 +38,13 @@ def call
def run_definition
callable = definition.callable

@value = if callable.arity.positive?
if callable.arity.positive?
deps = build_dependency_store
::Fixtury.hooks.call(:execution, self) do
around_execution do
instance_exec(deps, &callable)
end
else
::Fixtury.hooks.call(:execution, self) do
around_execution do
instance_eval(&callable)
end
end
Expand All @@ -41,6 +54,16 @@ def run_definition
raise Errors::DefinitionExecutionError.new(definition.pathname, e)
end

def around_execution(&block)
measure_timing do
@output.value = ::Fixtury.hooks.call(:execution, self, &block)
end
end

def measure_timing(&block)
@output.metadata[:duration] = Benchmark.realtime(&block)
end

def build_dependency_store
DependencyStore.new(definition: definition, store: store)
end
Expand Down
7 changes: 4 additions & 3 deletions lib/fixtury/reference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ def self.holder(name)
new(name, HOLDER_KEY)
end

attr_reader :name, :locator_key, :created_at, :options
attr_reader :name, :locator_key, :created_at, :metadata
alias options metadata # backwards compatibility

def initialize(name, locator_key, **options)
def initialize(name, locator_key, **metadata)
@name = name
@locator_key = locator_key
@created_at = Time.now.to_i
@options = options
@metadata = metadata
end

def holder?
Expand Down
6 changes: 4 additions & 2 deletions lib/fixtury/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,18 @@ def get(search)

begin
executor = ::Fixtury::DefinitionExecutor.new(store: self, definition: dfn)
value = executor.call
output = executor.call
value = output.value
rescue StandardError
clear_reference(pathname)
raise
end

log("store #{pathname}", level: LOG_LEVEL_DEBUG)

locator_key = locator.dump(value, context: pathname)
locator_key = locator.dump(output.value, context: pathname)
reference_opts = {}
reference_opts.merge!(output.metadata)
reference_opts[:isolation_key] = isokey unless isokey == pathname
references[pathname] = ::Fixtury::Reference.new(
pathname,
Expand Down
2 changes: 1 addition & 1 deletion lib/fixtury/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module Fixtury

VERSION = "1.0.0.beta6"
VERSION = "1.0.0.beta7"

end
10 changes: 10 additions & 0 deletions test/fixtury/definition_executor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ def test_definition_yields_a_dependency_store_if_arity_is_positive
executor.call
end

def test_return_value_is_an_output_object
dfn = ::Fixtury::Definition.new(name: "foo") { |s| s.get("thing") }
executor = ::Fixtury::DefinitionExecutor.new(definition: dfn)
Fixtury::DependencyStore.any_instance.expects(:get).with("thing").once.returns("foobar")
output = executor.call
assert_instance_of ::Fixtury::DefinitionExecutor::Output, output
assert_equal "foobar", output.value
assert output.metadata.key?(:duration)
end

end
end
26 changes: 24 additions & 2 deletions test/fixtury/store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def test_a_store_holds_references_to_fixtures

def test_a_store_returns_an_existing_reference_rather_than_reinvoking_the_definition
store = ::Fixtury::Store.new(schema: schema)
Fixtury::DefinitionExecutor.any_instance.expects(:call).once.returns("baz")
output = DefinitionExecutor::Output.new
output.stubs(:value).returns("baz")
Fixtury::DefinitionExecutor.any_instance.expects(:call).once.returns(output)

assert_equal "baz", store["foo"]
assert_equal "baz", store["foo"]
Expand Down Expand Up @@ -68,6 +70,24 @@ def test_a_ttl_store_does_not_return_expired_refs
refute_equal ref.created_at, new_ref.created_at
end

def test_generated_references_hold_metadata
dfn = schema.get("foo")
dfn.stubs(:isolation_key).returns("some_isolation_key")
store = ::Fixtury::Store.new(schema: schema)
output = DefinitionExecutor::Output.new
output.stubs(:value).returns("baz")
output.stubs(:metadata).returns({ meta_foo: "metabar" })
Fixtury::DefinitionExecutor.any_instance.expects(:call).once.returns(output)

store.get("foo")
ref = store.references[dfn.pathname]

assert_equal({
isolation_key: "some_isolation_key",
meta_foo: "metabar"
}, ref.metadata)
end

def test_a_store_doesnt_allow_circular_references
store = ::Fixtury::Store.new(schema: circular_schema)
assert_raises Errors::CircularDependencyError do
Expand All @@ -83,7 +103,9 @@ def test_a_store_doesnt_allow_circular_references

def test_store_reloads_value_if_locator_cannot_find
store = ::Fixtury::Store.new(schema: schema)
Fixtury::DefinitionExecutor.any_instance.expects(:call).twice.returns("baz")
output = DefinitionExecutor::Output.new
output.stubs(:value).returns("baz")
Fixtury::DefinitionExecutor.any_instance.expects(:call).twice.returns(output)

assert_equal "baz", store["foo"]
store.locator.stubs(:load).returns(nil)
Expand Down

0 comments on commit f61bcfd

Please sign in to comment.