Class: DomesticateMonkeys::Track

Inherits:
Object
  • Object
show all
Defined in:
lib/domesticate_monkeys/constants/track.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTrack

Returns a new instance of Track.



64
65
66
67
68
# File 'lib/domesticate_monkeys/constants/track.rb', line 64

def initialize
  @method  = nil
  @count   = 0
  @sources = []
end

Instance Attribute Details

#countObject

The Track instance keeps track of the amount of definitions, the chronological sequence of the source of the definitions, and potentially other information which we will deem valuable in the future.



14
15
16
# File 'lib/domesticate_monkeys/constants/track.rb', line 14

def count
  @count
end

#methodObject

The Track instance keeps track of the amount of definitions, the chronological sequence of the source of the definitions, and potentially other information which we will deem valuable in the future.



14
15
16
# File 'lib/domesticate_monkeys/constants/track.rb', line 14

def method
  @method
end

#sourcesObject

The Track instance keeps track of the amount of definitions, the chronological sequence of the source of the definitions, and potentially other information which we will deem valuable in the future.



14
15
16
# File 'lib/domesticate_monkeys/constants/track.rb', line 14

def sources
  @sources
end

Class Method Details

.add(unbound_method, method_type) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/domesticate_monkeys/constants/track.rb', line 18

def add(unbound_method, method_type)

  name   = format_method_name(unbound_method, method_type)
  source = read_method_source(unbound_method)
  return unless name && source

  # Find the existing track for the given method, or create a new track
  # if the given method is defined for the first time, through the 
  # default value of:
  #   $DOMESTICATE_MONKEYS_TRACKS ||= Hash.new(Track.new)

  # Also, duplicate the found / new record, in order to avoid the same
  # track being updated all the time and being assigned to all different 
  # keys.

  track = $DOMESTICATE_MONKEYS_TRACKS[name].dup
  track.add_source(name, source)
  
  rescue
    return
end

.format_instance_method(name) ⇒ Object



47
48
49
50
# File 'lib/domesticate_monkeys/constants/track.rb', line 47

def format_instance_method(name)    
  regex = /.*?<UnboundMethod: ([a-zA-Z:_]{1,}).*(?>([#]))([#.a-zA-Z_?]{1,})/
  name.scan(regex).flatten.join
end

.format_method_name(unbound_method, method_type) ⇒ Object



40
41
42
43
44
45
# File 'lib/domesticate_monkeys/constants/track.rb', line 40

def format_method_name(unbound_method, method_type)
  name = unbound_method.to_s

  return format_instance_method(name)  if method_type == :instance
  return format_singleton_method(name) if method_type == :singleton
end

.format_singleton_method(name) ⇒ Object



52
53
54
55
56
# File 'lib/domesticate_monkeys/constants/track.rb', line 52

def format_singleton_method(name)
  # regex = /.*?<Method:[^a-zA-Z:]{0,}([a-zA-Z:]{1,})[>]{0,1}([a-zA-Z_.]{1,})/
  regex = /.*?<Method:[^a-zA-Z:]{0,}([a-zA-Z:]{1,})[^\.]*([.A-Za-z_]{1,})/
  name.scan(regex).flatten.join
end

.read_method_source(unbound_method) ⇒ Object



58
59
60
# File 'lib/domesticate_monkeys/constants/track.rb', line 58

def read_method_source(unbound_method)
  unbound_method.source_location&.join(':')
end

Instance Method Details

#add_source(method_name, source) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/domesticate_monkeys/constants/track.rb', line 70

def add_source(method_name, source)
  
  $DOMESTICATE_MONKEYS_COUNT += 1
  
  if $BOOTING_MAIN_APP && !$SKIP_COUNT_PRINTING
    printf("\r   Methods defined: #{$DOMESTICATE_MONKEYS_COUNT}\r")
  end

  @method ||= method_name
  
  # Duplicate the retrieved self.sources, in order to avoid the instance variable
  # from hanging and being stacked with every new source – shared between multiple
  # instances.
  dupped_sources = @sources.dup

  # Only add a source to the sources variable if the method is being redefined, i.e.
  # being defined by source code that differs from the previously held definition.
  # If a file is being read for a second time, the method is technically defined again
  # but not redefined, since all behaviour is still the same. Therefore we should not
  # track such cases.
  dupped_sources << source unless dupped_sources.last == source

  # Update the tracker's state, and add it to our global $DOMESTICATE_MONKEYS_TRACKS dictionary afterwards. 
  @sources = dupped_sources
  @count   = dupped_sources.size

  $DOMESTICATE_MONKEYS_TRACKS[self.method] = self
end


99
100
101
102
103
104
105
106
107
108
# File 'lib/domesticate_monkeys/constants/track.rb', line 99

def print
  view = "  \n  \#{@count} definitions for: \n  \#{@method}\n  \#{@sources.map.with_index { |source, i| \"\#{i}: \#{source}\"}.join(\"\\n\")}\n  EOL\n\n  puts view\nend\n"