Class: Onceler::BlankTape

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

Instance Method Summary collapse

Constructor Details

#initialize(modules) ⇒ BlankTape

Returns a new instance of BlankTape.



3
4
5
6
7
# File 'lib/onceler/blank_tape.rb', line 3

def initialize(modules)
  modules.each { |mod| extend mod }
  @__retvals = {}
  @__retvals_recorded = {} # we might override an inherited one, so we need to differentiate
end

Instance Method Details

#__dataObject



34
35
36
# File 'lib/onceler/blank_tape.rb', line 34

def __data
  @__data ||= Marshal.dump([__ivars, @__retvals])
end

#__ivarsObject



25
26
27
28
29
30
31
32
# File 'lib/onceler/blank_tape.rb', line 25

def __ivars
  ivars = instance_variables - [:@__retvals, :@__retvals_recorded]
  ivars.inject({}) do |hash, key|
    val = instance_variable_get(key)
    hash[key] = val
    hash
  end
end

#__prepare_recording(recording) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/onceler/blank_tape.rb', line 9

def __prepare_recording(recording)
  method = recording.name
  define_singleton_method(method) do
    if @__retvals_recorded[method]
      @__retvals[method]
    else
      @__retvals_recorded[method] = true
      @__retvals[method] = __record(recording)
    end
  end
end

#__record(recording) ⇒ Object



21
22
23
# File 'lib/onceler/blank_tape.rb', line 21

def __record(recording)
  instance_eval(&recording.block)
end

#copy(mixins) ⇒ Object



38
39
40
41
42
# File 'lib/onceler/blank_tape.rb', line 38

def copy(mixins)
  copy = self.class.new(mixins)
  copy.copy_from(self)
  copy
end

#copy_from(other) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/onceler/blank_tape.rb', line 44

def copy_from(other)
  ivars, @__retvals = Marshal.load(other.__data)
  ivars.each do |key, value|
    instance_variable_set(key, value)
  end
  @__retvals.each do |key, value|
    define_singleton_method(key) { value }
  end
end