Sha256: b5638f4e658acbed058feaf1792fd3959237b89977c1320aec6f60fa6114ff3d

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require 'dcr/history'

module Dcr
  class SingletonMethodHistory < History
    def self.ivar
      :@_dcr_sm
    end

    attr_reader :object

    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

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

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

    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dcr-0.1.2 lib/dcr/singleton_method_history.rb
dcr-0.1.0 lib/dcr/singleton_method_history.rb