Class: Lemon::Snapshot

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lemon/coverage/snapshot.rb

Overview

Snapshot is used to record the “unit picture” of the system at a given moment.

Defined Under Namespace

Classes: Unit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(units = []) ⇒ Snapshot

Returns a new instance of Snapshot.



20
21
22
# File 'lib/lemon/coverage/snapshot.rb', line 20

def initialize(units=[])
  @units = units
end

Instance Attribute Details

#unitsObject (readonly)

Returns the value of attribute units.



17
18
19
# File 'lib/lemon/coverage/snapshot.rb', line 17

def units
  @units
end

Class Method Details

.capture(namespaces = nil) ⇒ Object



10
11
12
13
14
# File 'lib/lemon/coverage/snapshot.rb', line 10

def self.capture(namespaces=nil)
  o = new
  o.capture(namespaces)
  o
end

Instance Method Details

#+(other) ⇒ Object



89
90
91
# File 'lib/lemon/coverage/snapshot.rb', line 89

def +(other)
  Snapshot.new(units + other.units)
end

#-(other) ⇒ Object



84
85
86
# File 'lib/lemon/coverage/snapshot.rb', line 84

def -(other)
  Snapshot.new(units - other.units)
end

#<<(other) ⇒ Object



94
95
96
# File 'lib/lemon/coverage/snapshot.rb', line 94

def <<(other)
  @units.concat(other.units)
end

#capture(namespaces = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/lemon/coverage/snapshot.rb', line 45

def capture(namespaces=nil)
  @units = []
  ObjectSpace.each_object(Module) do |mod|
    next if mod.nil? or mod.name.nil? or mod.name.empty?
    #next if namespaces and !namespaces.any?{ |ns| /^#{ns}(::|$)/ =~ mod.to_s }
    next if namespaces and !namespaces.any?{ |ns| ns.to_s == mod.to_s }
    capture_namespace(mod)
  end
end

#capture_namespace(mod) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/lemon/coverage/snapshot.rb', line 56

def capture_namespace(mod)
  ['', 'protected_', 'private_'].each do |access|
    methods  = mod.__send__("#{access}instance_methods", false)
    #methods -= Object.__send__("#{access}instance_methods", true)
    methods.each do |method|
      @units << Unit.new(mod, method, :access=>access)
    end

    methods  = mod.__send__("#{access}methods", false)
    #methods -= Object.__send__("#{access}methods", true)
    methods.each do |method|
      @units << Unit.new(mod, method, :access=>access, :singleton=>true)
    end
  end
  return @units
end

#each(&block) ⇒ Object



25
26
27
# File 'lib/lemon/coverage/snapshot.rb', line 25

def each(&block)
  units.each(&block)
end

#public_unitsObject



79
80
81
# File 'lib/lemon/coverage/snapshot.rb', line 79

def public_units
  @units.select{ |u| u.public? }
end

#resetObject



35
36
37
# File 'lib/lemon/coverage/snapshot.rb', line 35

def reset
  each{ |u| u.covered = false }
end

#sizeObject



30
31
32
# File 'lib/lemon/coverage/snapshot.rb', line 30

def size
  @units.size
end

#to_aObject



74
75
76
# File 'lib/lemon/coverage/snapshot.rb', line 74

def to_a
  @units
end