Class: Dcr::History

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ History

Returns a new instance of History.



13
14
15
16
# File 'lib/dcr/history.rb', line 13

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

Instance Attribute Details

#trackObject (readonly)

Returns the value of attribute track.



11
12
13
# File 'lib/dcr/history.rb', line 11

def track
  @track
end

Class Method Details

.of(obj) ⇒ Object



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

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

Instance Method Details

#add_to_track(method, name = nil) ⇒ Object



26
27
28
29
# File 'lib/dcr/history.rb', line 26

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

#caller_not_from_dcrObject



31
32
33
34
35
# File 'lib/dcr/history.rb', line 31

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

#list(method_name) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/dcr/history.rb', line 18

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

#pop_all_track(method_name) ⇒ Object



42
43
44
45
46
47
# File 'lib/dcr/history.rb', line 42

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

#pop_last_track(method_name) ⇒ Object



37
38
39
40
# File 'lib/dcr/history.rb', line 37

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

#warn_if_no_org_methods(method_name) ⇒ Object

Raises:

  • (NoMethodError)


49
50
51
52
53
# File 'lib/dcr/history.rb', line 49

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