once-ler 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/onceler/recordable.rb +28 -17
  2. metadata +2 -2
@@ -3,7 +3,7 @@ module Onceler
3
3
  def self.extended(instance)
4
4
  instance.instance_eval do
5
5
  @__retvals = {}
6
- @__retvals_recorded = {} # we might override an inherited one, so we need to differentiate
6
+ @__inherited_retvals = {}
7
7
  @__ignore_ivars = instance_variables
8
8
  end
9
9
  end
@@ -11,10 +11,9 @@ module Onceler
11
11
  def __prepare_recording(recording)
12
12
  method = recording.name
13
13
  define_singleton_method(method) do
14
- if @__retvals_recorded[method]
14
+ if @__retvals.key?(method)
15
15
  @__retvals[method]
16
16
  else
17
- @__retvals_recorded[method] = true
18
17
  @__retvals[method] = __record(recording)
19
18
  end
20
19
  end
@@ -24,39 +23,51 @@ module Onceler
24
23
  instance_eval(&recording.block)
25
24
  end
26
25
 
27
- def __retvals
28
- @__retvals.slice(*@__retvals_recorded.keys)
26
+ def __retvals(inherit = false)
27
+ retvals = @__inherited_retvals.merge(@__retvals)
28
+ retvals.inject({}) do |hash, (key, val)|
29
+ hash[key] = val if __mutated?(key, val) || inherit
30
+ hash
31
+ end
29
32
  end
30
33
 
31
- def __ivars
34
+ def __ivars(inherit = false)
32
35
  ivars = instance_variables - @__ignore_ivars
33
36
  ivars.inject({}) do |hash, key|
34
37
  if key.to_s !~ /\A@__/
35
38
  val = instance_variable_get(key)
36
- hash[key] = val unless __inherited_ivar?(key, val)
39
+ hash[key] = val if __mutated?(key, val) || inherit
37
40
  end
38
41
  hash
39
42
  end
40
43
  end
41
44
 
42
- def __inherited_ivar?(key, val)
43
- return false unless @__inherited_ivar_cache
45
+ # we don't include inherited stuff in __data, because we might need to
46
+ # interleave things from an intermediate before(:each) at run time
47
+ def __mutated?(key, val)
48
+ return true unless @__inherited_cache
44
49
  # need to do both types of comparison, i.e. it's the same object in
45
- # memory, and nothing about it has been changed
46
- @__inherited_ivar_cache[key] == val && @__inherited_ivars.equal?(val)
50
+ # memory (not reassigned), and nothing about it has been changed
51
+ return false if @__inherited_values.equal?(val)
52
+ return false if @__inherited_cache[key] == val
53
+ true
47
54
  end
48
55
 
49
- def __data
50
- @__data ||= Marshal.dump([__ivars, __retvals])
56
+ def __data(inherit = false)
57
+ @__data ||= {}
58
+ @__data[inherit] ||= Marshal.dump([__ivars(inherit), __retvals(inherit)])
51
59
  end
52
60
 
53
61
  def copy_from(other)
54
- @__inherited_ivar_cache = Marshal.load(other.__data).first
55
- @__inherited_ivars, @__retvals = Marshal.load(other.__data)
56
- @__inherited_ivars.each do |key, value|
62
+ # need two copies of things for __mutated? checks (see above)
63
+ @__inherited_cache = Marshal.load(other.__data(:inherit)).inject(&:merge)
64
+ ivars, retvals = Marshal.load(other.__data(:inherit))
65
+ @__inherited_retvals = retvals
66
+ @__inherited_values = ivars.merge(retvals)
67
+ ivars.each do |key, value|
57
68
  instance_variable_set(key, value)
58
69
  end
59
- @__retvals.each do |key, value|
70
+ retvals.each do |key, value|
60
71
  define_singleton_method(key) { value }
61
72
  end
62
73
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: once-ler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-30 00:00:00.000000000 Z
12
+ date: 2014-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord