Class: Dcr::SingletonMethodHistory

Inherits:
History
  • Object
show all
Defined in:
lib/dcr/singleton_method_history.rb

Instance Attribute Summary collapse

Attributes inherited from History

#track

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from History

#add_to_track, #caller_not_from_dcr, #initialize, #list, of, #pop_all_track, #pop_last_track, #warn_if_no_org_methods

Constructor Details

This class inherits a constructor from Dcr::History

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object.



9
10
11
# File 'lib/dcr/singleton_method_history.rb', line 9

def object
  @object
end

Class Method Details

.ivarObject



5
6
7
# File 'lib/dcr/singleton_method_history.rb', line 5

def self.ivar
  :@_dcr_sm
end

Instance Method Details

#commit(method_name, &decorator) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dcr/singleton_method_history.rb', line 11

def commit method_name, &decorator
  org_method = from_superclass_or_self object, method_name

  new_method = lambda do |*args|
    decorator.call org_method, *args
  end

  object.define_singleton_method method_name, &new_method

  add_to_track org_method, method_name
end

#from_superclass_or_self(object, method_name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/dcr/singleton_method_history.rb', line 33

def from_superclass_or_self object, method_name
  if track[method_name].empty?
    lambda do |*args|
      m = object.class.ancestors[0].instance_method method_name
      m.bind(object).call *args
    end
  else
    object.method method_name
  end
end

#rollback(method_name) ⇒ Object



23
24
25
26
# File 'lib/dcr/singleton_method_history.rb', line 23

def rollback method_name
  org_method = pop_last_track method_name
  object.define_singleton_method method_name, org_method
end

#rollback_all(method_name) ⇒ Object



28
29
30
31
# File 'lib/dcr/singleton_method_history.rb', line 28

def rollback_all method_name
  org_method = pop_all_track method_name
  object.define_singleton_method method_name, org_method
end