Sha256: de3586007ae268f32370a6e0ad5370b862b72b6d1d7256a05b89f078ab5d9bca

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Dcr
  class History

    class << self
      def of obj
        obj.instance_variable_set(ivar, new(obj)) unless obj.instance_variable_defined? ivar
        obj.instance_variable_get ivar
      end
    end

    attr_reader :track

    def initialize obj
      @object = obj
      @track = Hash.new{ |hash, key| hash[key] = [] }
    end

    def list method_name
      track[method_name].
        reverse_each.
        map do |method, file, line|
          [file, line]
        end
    end

    def add_to_track method, name=nil
      file, line, _ =  caller_not_from_dcr.split(':')
      track[name || method.name] << [method, file, line.to_i]
    end

    def caller_not_from_dcr
      caller.find do |file|
        file !~ /dcr-.*\/lib\/dcr/
      end
    end

    def pop_last_track method_name
      warn_if_no_org_methods method_name
      track[method_name].pop[0]
    end

    def pop_all_track method_name
      warn_if_no_org_methods method_name
      oldest_method = track[method_name].shift[0]
      track[method_name].clear
      oldest_method
    end

    def warn_if_no_org_methods method_name
      methods = track[method_name]
      raise NoMethodError,
        "no more history for method: #{method_name}" if methods.empty?
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dcr-0.1.2 lib/dcr/history.rb