Class: Onceler::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/onceler/recorder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group_class) ⇒ Recorder

Returns a new instance of Recorder.



15
16
17
18
19
20
# File 'lib/onceler/recorder.rb', line 15

def initialize(group_class)
  @group_class = group_class
  @recordings = []
  @named_recordings = []
  @arounds = []
end

Instance Attribute Details

#tapeObject

Returns the value of attribute tape.



13
14
15
# File 'lib/onceler/recorder.rb', line 13

def tape
  @tape
end

Instance Method Details

#<<(block) ⇒ Object



26
27
28
# File 'lib/onceler/recorder.rb', line 26

def <<(block)
  @recordings << Recording.new(block)
end

#[](name) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/onceler/recorder.rb', line 35

def [](name)
  if @retvals && @retvals.key?(name)
    @retvals[name]
  elsif parent
    parent[name]
  end
end

#[]=(name, block) ⇒ Object



30
31
32
33
# File 'lib/onceler/recorder.rb', line 30

def []=(name, block)
  @named_recordings << name
  @recordings << NamedRecording.new(block, name)
end

#add_around(block) ⇒ Object



43
44
45
# File 'lib/onceler/recorder.rb', line 43

def add_around(block)
  @arounds.unshift(block)
end

#aroundsObject



47
48
49
# File 'lib/onceler/recorder.rb', line 47

def arounds
  (parent ? parent.arounds : []) + @arounds
end

#hooksObject



92
93
94
95
96
97
# File 'lib/onceler/recorder.rb', line 92

def hooks
  @hooks ||= {
    before: {record: [], reset: []},
    after:  {record: [], reset: []}
  }
end

#parentObject



88
89
90
# File 'lib/onceler/recorder.rb', line 88

def parent
  @group_class.parent_onceler
end

#parent_tapeObject



22
23
24
# File 'lib/onceler/recorder.rb', line 22

def parent_tape
  parent.tape || parent.parent_tape if parent
end

#record!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/onceler/recorder.rb', line 51

def record!
  Onceler.recording = true
  @tape = @group_class.new
  @tape.setup_fixtures
  @tape.send :extend, Recordable
  @tape.copy_from(parent_tape) if parent_tape

  run_before_hooks(:record, @tape)
  # we don't know the order named recordings will be called (or if
  # they'll call each other), so prep everything first
  @recordings.each do |recording|
    recording.prepare_medium!(@tape)
  end

  # wrap the before in a lambda
  stack = -> do
    @recordings.each do |recording|
      recording.record_onto!(@tape)
    end
  end
  # and then stack each around block on top
  arounds.inject(stack) do |old_stack, hook|
    -> { @tape.instance_exec(old_stack, &hook) }
  end.call

  run_after_hooks(:record, @tape)
  @data = @tape.__data
ensure
  Onceler.recording = false
end

#replay_into!(instance) ⇒ Object



121
122
123
124
125
126
# File 'lib/onceler/recorder.rb', line 121

def replay_into!(instance)
  @ivars, @retvals = Marshal.load(@data)
  @ivars.each do |key, value|
    instance.instance_variable_set(key, value)
  end
end

#reset!Object



82
83
84
85
86
# File 'lib/onceler/recorder.rb', line 82

def reset!
  run_before_hooks(:reset)
  @tape&.teardown_fixtures
  run_after_hooks(:reset)
end

#run_after_hooks(scope, context = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/onceler/recorder.rb', line 110

def run_after_hooks(scope, context = nil)
  hooks[:after][scope].each do |hook|
    context ? context.instance_eval(&hook) : hook.call
  end
  if parent
    parent.run_after_hooks(scope, context)
  else
    Onceler.configuration.run_hooks(:after, scope, context)
  end
end

#run_before_hooks(scope, context = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/onceler/recorder.rb', line 99

def run_before_hooks(scope, context = nil)
  if parent
    parent.run_before_hooks(scope, context)
  else
    Onceler.configuration.run_hooks(:before, scope, context)
  end
  hooks[:before][scope].each do |hook|
    context ? context.instance_eval(&hook) : hook.call
  end
end