Module: Onceler::Recordable
- Defined in:
- lib/onceler/recordable.rb
Class Method Summary collapse
Instance Method Summary collapse
- #__data ⇒ Object
- #__inherited_ivar?(key, val) ⇒ Boolean
- #__ivars ⇒ Object
- #__prepare_recording(recording) ⇒ Object
- #__record(recording) ⇒ Object
- #__retvals ⇒ Object
- #copy_from(other) ⇒ Object
Class Method Details
.extended(instance) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/onceler/recordable.rb', line 3 def self.extended(instance) instance.instance_eval do @__retvals = {} @__retvals_recorded = {} # we might override an inherited one, so we need to differentiate @__ignore_ivars = instance_variables end end |
Instance Method Details
#__data ⇒ Object
49 50 51 |
# File 'lib/onceler/recordable.rb', line 49 def __data @__data ||= Marshal.dump([__ivars, __retvals]) end |
#__inherited_ivar?(key, val) ⇒ Boolean
42 43 44 45 46 47 |
# File 'lib/onceler/recordable.rb', line 42 def __inherited_ivar?(key, val) return false unless @__inherited_ivar_cache # need to do both types of comparison, i.e. it's the same object in # memory, and nothing about it has been changed @__inherited_ivar_cache[key] == val && @__inherited_ivars.equal?(val) end |
#__ivars ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/onceler/recordable.rb', line 31 def __ivars ivars = instance_variables - @__ignore_ivars ivars.inject({}) do |hash, key| if key.to_s !~ /\A@__/ val = instance_variable_get(key) hash[key] = val unless __inherited_ivar?(key, val) end hash end end |
#__prepare_recording(recording) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/onceler/recordable.rb', line 11 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
23 24 25 |
# File 'lib/onceler/recordable.rb', line 23 def __record(recording) instance_eval(&recording.block) end |
#__retvals ⇒ Object
27 28 29 |
# File 'lib/onceler/recordable.rb', line 27 def __retvals @__retvals.slice(*@__retvals_recorded.keys) end |
#copy_from(other) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/onceler/recordable.rb', line 53 def copy_from(other) @__inherited_ivar_cache = Marshal.load(other.__data).first @__inherited_ivars, @__retvals = Marshal.load(other.__data) @__inherited_ivars.each do |key, value| instance_variable_set(key, value) end @__retvals.each do |key, value| define_singleton_method(key) { value } end end |