fixturama 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 779a1dc8057604e6a5524eff0ef07520ecdbaf62895711411a312fb5c81a7558
4
- data.tar.gz: 7976718e10f2fe8036d9e934d6e720cdcb91890f4263221720bfe76815eca071
3
+ metadata.gz: 4e21218a92d62cab4b0bbc73fe1f2ac02eb5c66a7315357a68189067d138d92a
4
+ data.tar.gz: 81861ef1dbf1eba81874fb929875c89f04dc3ebdeba9694ae02989762dd7aca5
5
5
  SHA512:
6
- metadata.gz: 25047655a38dedca47af366563ad14c7f0bea54ddd0230097f6a940b127593a70a6144cf556670308dc1ad1048cce74180cecfe7c89871682602e5e5bbca0988
7
- data.tar.gz: c23c3fe8662badf370ffc5cb02c756d510ca2be2f75639bc77e8d54f4243b49be74f17f54561329861865778a06b7e08efcf1c24d6fbb873dfacbb912f53618b
6
+ metadata.gz: f09063278691d752987cc1b9bbc8d20a715624cce81f81e9c61b521d52af28b9fbe03e09ac9357e01c154b987608558231b3c0ca8d1bdd5677303f887630b3ad
7
+ data.tar.gz: b93be44d444875df3d793bb7947bbd9c906a0fcc699c3b1ec4625186a38eed3b58b8a11f11d6e7ae421db4615534533029c2fece7d592a357649ef24598e63a9
data/CHANGELOG.md CHANGED
@@ -5,6 +5,29 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/).
7
7
 
8
+ ## [0.5.0] - [2021-04-03]
9
+
10
+ ### Added
11
+
12
+ - Support for <de>serialization PORO objects (nepalez)
13
+
14
+ ```yaml
15
+ # target.yml
16
+ ---
17
+ number: <%= object(be_positive) %>
18
+ ```
19
+
20
+ ```ruby
21
+ RSpec.describe "something" do
22
+ subject { { "number" => 42 } }
23
+
24
+ # no explicit params is needed here
25
+ let(:target) { load_fixture "target.yml" }
26
+
27
+ it { is_expected.to match(target) }
28
+ end
29
+ ```
30
+
8
31
  ## [0.4.1] - [2021-03-31]
9
32
 
10
33
  ### Fixed
@@ -275,3 +298,4 @@ This is a first public release with features extracted from production app.
275
298
  [0.3.0]: https://github.com/nepalez/fixturama/compare/v0.2.0...v0.3.0
276
299
  [0.4.0]: https://github.com/nepalez/fixturama/compare/v0.3.0...v0.4.0
277
300
  [0.4.1]: https://github.com/nepalez/fixturama/compare/v0.4.0...v0.4.1
301
+ [0.5.0]: https://github.com/nepalez/fixturama/compare/v0.4.1...v0.5.0
data/README.md CHANGED
@@ -99,6 +99,38 @@ This feature can also be useful to produce a "partially defined" fixtures with [
99
99
  subject { load_fixture "#{__dir__}/data.yml", user: kind_of(ActiveRecord::Base) }
100
100
  ```
101
101
 
102
+ Since the v0.5.0 we support another way to serialize PORO objects in fixtures. Just wrap them to the `object()` method:
103
+
104
+ ```yaml
105
+ ---
106
+ :account: <%= object(user) %>
107
+ ```
108
+
109
+ This time you don't need sending objects explicitly.
110
+
111
+ ```ruby
112
+ RSpec.describe "example" do
113
+ subject { load_fixture "#{__dir__}/data.yml" }
114
+
115
+ let(:user) { FactoryBot.create(:user) }
116
+
117
+ # The same object will be returned
118
+ it { is_expected.to eq(account: user) }
119
+ end
120
+ ```
121
+
122
+ Under the hood we use `Marshal.dump` and `Marshal.restore` to serialize and deserialize the object back.
123
+
124
+ **Notice**, that deserialization creates a new instance of the object which is not equivalent to the source (`user` in the example above)!
125
+ In most cases this is enough. For example, you can provide matchers like:
126
+
127
+ ```yaml
128
+ ---
129
+ number: <%= object(be_positive) %>
130
+ ```
131
+
132
+ The loaded object would contain `{ "number" => be_positive }`.
133
+
102
134
  ### Seeding
103
135
 
104
136
  The seed (`seed_fixture`) file should be a YAML/JSON with opinionated parameters, namely:
data/lib/fixturama.rb CHANGED
@@ -42,7 +42,7 @@ module Fixturama
42
42
  # @param (see #read_fixture)
43
43
  # @return [Object]
44
44
  def load_fixture(path, **options)
45
- Loader.new(path, options).call
45
+ Loader.new(self, path, options).call
46
46
  end
47
47
 
48
48
  # @!method call_fixture(path, options)
@@ -14,7 +14,7 @@ class Fixturama::Changes
14
14
  # @return [Fixturama::Changes::Base]
15
15
  def merge(other)
16
16
  # By default just take the other change if applicable
17
- other.class == self.class && other.key == key ? other : self
17
+ other.instance_of?(self.class) && other.key == key ? other : self
18
18
  end
19
19
 
20
20
  # @abstract
@@ -14,7 +14,7 @@ class Fixturama::Changes
14
14
  end
15
15
 
16
16
  def merge(other)
17
- return self unless other.class == self.class && other.key == key
17
+ return self unless other.instance_of?(self.class) && other.key == key
18
18
 
19
19
  tap { @arguments = (other.arguments | arguments).sort_by(&:order) }
20
20
  end
@@ -15,7 +15,8 @@ class Fixturama::Loader
15
15
 
16
16
  private
17
17
 
18
- def initialize(path, opts = {})
18
+ def initialize(example, path, opts = {})
19
+ @example = example
19
20
  @path = path
20
21
  @opts = opts.to_h
21
22
  end
@@ -33,7 +34,7 @@ class Fixturama::Loader
33
34
  end
34
35
 
35
36
  def context
36
- @context ||= (yaml? || json?) ? Context.new(@opts) : Hashie::Mash.new(@opts)
37
+ @context ||= Context.new(@example, @opts)
37
38
  end
38
39
 
39
40
  def content
@@ -71,6 +72,8 @@ class Fixturama::Loader
71
72
  # @param [String] string
72
73
  # @return [Object]
73
74
  def finalize_string(string)
75
+ Marshal.restore(string)
76
+ rescue TypeError, RuntimeError
74
77
  key = string.match(Value::MATCHER)&.captures&.first&.to_s
75
78
  key ? context[key] : string
76
79
  end
@@ -1,9 +1,13 @@
1
1
  class Fixturama::Loader
2
2
  #
3
3
  # @private
4
- # The context of some fixture
4
+ # The context bound to some fixture
5
5
  #
6
6
  class Context
7
+ def object(value)
8
+ Marshal.dump(value).dump
9
+ end
10
+
7
11
  # Get value by key
8
12
  # @param [#to_s] key
9
13
  # @return [Object]
@@ -13,7 +17,8 @@ class Fixturama::Loader
13
17
 
14
18
  private
15
19
 
16
- def initialize(values)
20
+ def initialize(example, values)
21
+ @example = example
17
22
  @values = \
18
23
  Hash(values).each_with_object(Hashie::Mash.new) do |(key, val), obj|
19
24
  obj[key] = Value.new(key, val)
@@ -21,11 +26,14 @@ class Fixturama::Loader
21
26
  end
22
27
 
23
28
  def respond_to_missing?(name, *)
24
- @values.respond_to?(name) || super
29
+ @values.key?(name) || @example.respond_to?(name) || super
25
30
  end
26
31
 
27
- def method_missing(name, *args)
28
- @values.respond_to?(name) ? @values.send(name, *args) : super
32
+ def method_missing(name, *args, &block)
33
+ return @values[name] if @values.key?(name)
34
+ return super unless @example.respond_to?(name)
35
+
36
+ @example.send(name, *args, &block)
29
37
  end
30
38
  end
31
39
  end
@@ -1,4 +1,4 @@
1
1
  module Fixturama
2
2
  # The current version of the gem
3
- VERSION = "0.4.1"
3
+ VERSION = "0.5.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixturama
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kozin (nepalez)
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-31 00:00:00.000000000 Z
11
+ date: 2021-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: factory_bot