fixtury 0.2.1 → 0.3.0.beta

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9090092db55914068c6478939047d638ee8bb678ee53a41f0d36380bf2df7dea
4
- data.tar.gz: 4e58164635a72d63e059a5622b1c867556235ba09537e127b5498f94a3bf8e60
3
+ metadata.gz: 965698b591633e500ea9a84c99f860e333abc4b43670adda323a39f156834971
4
+ data.tar.gz: 05157e6800473b9eb4d9a34d5f72572726db3318e1e415b5c9307b243c94e524
5
5
  SHA512:
6
- metadata.gz: 05e1ddd158beafe83cbe9e9d32abf10a49dcdb59709cc372f31a29a1a52c3eafe0b802eba8015ccdb429444678d94941db42dedb6202396b958c8d53d2e6f9d2
7
- data.tar.gz: 70e2abc1abc6324e3523631a6f2caf936d0b0859eeb14b41fec843b766bf648d047b1e09e9d9bb6da60131f06be23e7e02c1adcf86be30910387581b7e191d3e
6
+ metadata.gz: 8cfe05cd9ee47eb81c70247ef902870bebeaa563035ab19ae920646fcf0421483f13ab0efd23a3ee2e0293092a70be7259b4088a3fbf56ebaf585788c8160993
7
+ data.tar.gz: 96797ae9dbf6b4ccc6d9e3149be61dc5b4cc57d7b4baec2f9355aff35f89d0b6a3fc25b3f11fc2c7198eabeaed0298f97e51c49f96fadce6a13709bf493a845c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fixtury (0.2.1)
4
+ fixtury (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "fixtury/definition_executor"
4
+
3
5
  module Fixtury
4
6
  class Definition
5
7
 
@@ -24,55 +26,23 @@ module Fixtury
24
26
  @enhancements.any?
25
27
  end
26
28
 
27
- def call(store: nil, execution_context: nil)
28
- maybe_set_store_context(store: store) do
29
- value = run_callable(store: store, callable: callable, execution_context: execution_context, value: nil)
30
- enhancements.each do |e|
31
- value = run_callable(store: store, callable: e, execution_context: execution_context, value: value)
32
- end
33
- value
34
- end
29
+ def info
30
+ {
31
+ name: name,
32
+ loc: location_from_callable(callable),
33
+ enhancements: enhancements.map { |e| location_from_callable(e) },
34
+ }
35
35
  end
36
36
 
37
- protected
38
-
39
- def maybe_set_store_context(store:)
40
- return yield unless store
41
-
42
- store.with_relative_schema(schema) do
43
- yield
44
- end
45
- end
46
-
47
- def run_callable(store:, callable:, execution_context: nil, value:)
48
- args = []
49
- args << value unless value.nil?
50
- if callable.arity > args.length
51
- raise ArgumentError, "A store store must be provided if the definition expects it." unless store
52
-
53
- args << store
54
- end
55
-
56
- provide_execution_context_hooks(execution_context) do |ctxt|
57
- if args.length.positive?
58
- ctxt.instance_exec(*args, &callable)
59
- else
60
- ctxt.instance_eval(&callable)
61
- end
62
- end
37
+ def call(store: nil, execution_context: nil)
38
+ executor = ::Fixtury::DefinitionExecutor.new(store: store, definition: self, execution_context: execution_context)
39
+ executor.__call
63
40
  end
64
41
 
65
- def provide_execution_context_hooks(execution_context)
66
- return yield self unless execution_context
42
+ def location_from_callable(callable)
43
+ return nil unless callable.respond_to?(:source_location)
67
44
 
68
- execution_context.before_fixture(self) if execution_context.respond_to?(:before_fixture)
69
- value = if execution_context.respond_to?(:around_fixture)
70
- execution_context.around_fixture(self) { yield execution_context }
71
- else
72
- yield execution_context
73
- end
74
- execution_context.after_fixture(self, value) if execution_context.respond_to?(:after_fixture)
75
- value
45
+ callable.source_location.join(":")
76
46
  end
77
47
 
78
48
  end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fixtury
4
+ class DefinitionExecutor
5
+
6
+ attr_reader :value, :execution_type, :definition, :store, :execution_context
7
+
8
+ def initialize(store: nil, execution_context: nil, definition:)
9
+ @store = store
10
+ @definition = definition
11
+ @execution_context = execution_context
12
+ @execution_type = nil
13
+ @value = nil
14
+ end
15
+
16
+ def __call
17
+ maybe_set_store_context do
18
+ provide_schema_hooks do
19
+ run_callable(callable: definition.callable, type: :definition)
20
+ definition.enhancements.each do |e|
21
+ run_callable(callable: e, type: :enhancement)
22
+ end
23
+ end
24
+ end
25
+
26
+ value
27
+ end
28
+
29
+ def get(name)
30
+ raise ArgumentError, "A store is required for #{definition.name}" unless store
31
+
32
+ store.get(name, execution_context: execution_context)
33
+ end
34
+ alias [] get
35
+
36
+ def method_missing(method_name, *args, &block)
37
+ return super unless execution_context
38
+
39
+ execution_context.send(method_name, *args, &block)
40
+ end
41
+
42
+ def respond_to_missing?(method_name)
43
+ return super unless execution_context
44
+
45
+ execution_context.respond_to?(method_name, true)
46
+ end
47
+
48
+ private
49
+
50
+ def run_callable(callable:, type:)
51
+ @execution_type = type
52
+
53
+ @value = if callable.arity.positive?
54
+ instance_exec(self, &callable)
55
+ else
56
+ instance_eval(&callable)
57
+ end
58
+ end
59
+
60
+ def maybe_set_store_context
61
+ return yield unless store
62
+
63
+ store.with_relative_schema(definition.schema) do
64
+ yield
65
+ end
66
+ end
67
+
68
+ def provide_schema_hooks
69
+ return yield unless definition.schema
70
+
71
+ @value = definition.schema.around_fixture_hook(self) do
72
+ yield
73
+ value
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -9,16 +9,37 @@ require "fixtury/errors/schema_frozen_error"
9
9
  module Fixtury
10
10
  class Schema
11
11
 
12
- attr_reader :definitions, :children, :name, :parent, :relative_name
12
+ attr_reader :definitions, :children, :name, :parent, :relative_name, :around_fixture_definition
13
13
 
14
14
  def initialize(parent:, name:)
15
15
  @name = name
16
16
  @parent = parent
17
17
  @relative_name = @name.split("/").last
18
+ @around_fixture_definition = nil
18
19
  @frozen = false
19
20
  reset!
20
21
  end
21
22
 
23
+ def around_fixture(&block)
24
+ @around_fixture_definition = block
25
+ end
26
+
27
+ def around_fixture_hook(executor, &definition)
28
+ maybe_invoke_parent_around_fixture_hook(executor) do
29
+ if around_fixture_definition.nil?
30
+ yield
31
+ else
32
+ around_fixture_definition.call(executor, definition)
33
+ end
34
+ end
35
+ end
36
+
37
+ def maybe_invoke_parent_around_fixture_hook(executor, &block)
38
+ return yield unless parent
39
+
40
+ parent.around_fixture_hook(executor, &block)
41
+ end
42
+
22
43
  def reset!
23
44
  @children = {}
24
45
  @definitions = {}
@@ -99,6 +120,8 @@ module Fixtury
99
120
  end
100
121
  end
101
122
 
123
+ around_fixture(&other_ns.around_fixture_definition) if other_ns.around_fixture_definition
124
+
102
125
  self
103
126
  end
104
127
 
@@ -172,6 +195,7 @@ module Fixtury
172
195
  self.class.new(name: child_name, parent: self)
173
196
  end
174
197
  end
198
+ child
175
199
  end
176
200
 
177
201
  def find_child_definition(name:)
@@ -187,7 +211,7 @@ module Fixtury
187
211
  def build_child_name(name:)
188
212
  name = name&.to_s
189
213
  raise ArgumentError, "`name` must be provided" if name.nil?
190
- raise ArgumentError, "#{name} is invalid. `name` must contain only a-z, A-Z, 0-9, and _." unless name.match(/^[a-zA-Z_0-9]+$/)
214
+ raise ArgumentError, "#{name} is invalid. `name` must contain only a-z, A-Z, 0-9, and _." unless /^[a-zA-Z_0-9]+$/.match?(name)
191
215
 
192
216
  arr = ["", self.name, name]
193
217
  arr.join("/").gsub(%r{/{2,}}, "/")
@@ -21,7 +21,6 @@ module Fixtury
21
21
  attr_reader :filepath, :references, :ttl, :auto_refresh_expired
22
22
  attr_reader :schema, :locator
23
23
  attr_reader :log_level
24
- attr_reader :execution_context
25
24
 
26
25
  def initialize(
27
26
  filepath: nil,
@@ -29,7 +28,6 @@ module Fixtury
29
28
  log_level: nil,
30
29
  ttl: nil,
31
30
  schema: nil,
32
- execution_context: nil,
33
31
  auto_refresh_expired: false
34
32
  )
35
33
  @schema = schema || ::Fixtury.schema
@@ -39,7 +37,6 @@ module Fixtury
39
37
  @locator = locator
40
38
  @filepath = filepath
41
39
  @references = @filepath && ::File.file?(@filepath) ? ::YAML.load_file(@filepath) : {}
42
- @execution_context = execution_context
43
40
  @ttl = ttl ? ttl.to_i : ttl
44
41
  @auto_refresh_expired = !!auto_refresh_expired
45
42
  self.class.instance ||= self
@@ -107,7 +104,7 @@ module Fixtury
107
104
  result
108
105
  end
109
106
 
110
- def get(name)
107
+ def get(name, execution_context: nil)
111
108
  dfn = schema.get_definition!(name)
112
109
  full_name = dfn.name
113
110
  ref = references[full_name]
@@ -40,14 +40,16 @@ module Fixtury
40
40
  self.fixtury_dependencies += names.flatten.compact.map(&:to_s)
41
41
  end
42
42
 
43
- if opts[:accessor]
43
+ accessor_option = opts.key?(:accessor) ? opts[:accessor] : true
44
44
 
45
- if opts[:accessor] != true && names.length > 1
45
+ if accessor_option
46
+
47
+ if accessor_option != true && names.length > 1
46
48
  raise ArgumentError, "A named :accessor option is only available when providing one fixture"
47
49
  end
48
50
 
49
51
  names.each do |fixture_name|
50
- method_name = opts[:accessor] == true ? fixture_name.split("/").last : opts[:accessor]
52
+ method_name = accessor_option == true ? fixture_name.split("/").last : accessor_option
51
53
  ivar = :"@#{method_name}"
52
54
 
53
55
  class_eval <<-EV, __FILE__, __LINE__ + 1
@@ -72,7 +74,7 @@ module Fixtury
72
74
  unless name.include?("/")
73
75
  local_name = "#{self.class.name.underscore}/#{name}"
74
76
  if self.fixtury_dependencies.include?(local_name)
75
- return fixtury_store.get(local_name)
77
+ return fixtury_store.get(local_name, execution_context: self)
76
78
  end
77
79
  end
78
80
 
@@ -80,7 +82,7 @@ module Fixtury
80
82
  raise ArgumentError, "Unrecognized fixtury dependency `#{name}` for #{self.class}"
81
83
  end
82
84
 
83
- fixtury_store.get(name)
85
+ fixtury_store.get(name, execution_context: self)
84
86
  end
85
87
 
86
88
  def fixtury_store
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Fixtury
4
4
 
5
- VERSION = "0.2.1"
5
+ VERSION = "0.3.0.beta"
6
6
 
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixtury
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0.beta
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Nelson
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-13 00:00:00.000000000 Z
11
+ date: 2020-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: autotest
@@ -155,6 +155,7 @@ files:
155
155
  - fixtury.gemspec
156
156
  - lib/fixtury.rb
157
157
  - lib/fixtury/definition.rb
158
+ - lib/fixtury/definition_executor.rb
158
159
  - lib/fixtury/errors/already_defined_error.rb
159
160
  - lib/fixtury/errors/circular_dependency_error.rb
160
161
  - lib/fixtury/errors/fixture_not_defined_error.rb
@@ -190,9 +191,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
190
191
  version: '0'
191
192
  required_rubygems_version: !ruby/object:Gem::Requirement
192
193
  requirements:
193
- - - ">="
194
+ - - ">"
194
195
  - !ruby/object:Gem::Version
195
- version: '0'
196
+ version: 1.3.1
196
197
  requirements: []
197
198
  rubygems_version: 3.0.6
198
199
  signing_key: