Class: Lemon::Snapshot::Unit

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

Overview

Snapshot Unit encapsulates a method and it’s various characteristics.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, method, props = {}) ⇒ Unit

Returns a new instance of Unit.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/lemon/coverage/snapshot.rb', line 110

def initialize(target, method, props={})
  @target    = target
  @method    = method.to_sym
  @singleton = props[:singleton] ? true : false
  @covered   = props[:covered]

  if @singleton
    @private   = @target.private_methods.find{ |m| m.to_sym == @method }
    @protected = @target.protected_methods.find{ |m| m.to_sym == @method }
  else
    @private   = @target.private_instance_methods.find{ |m| m.to_sym == @method }
    @protected = @target.protected_instance_methods.find{ |m| m.to_sym == @method }
  end
end

Instance Attribute Details

#coveredObject

Can be used to flag a unit as covered.



126
127
128
# File 'lib/lemon/coverage/snapshot.rb', line 126

def covered
  @covered
end

#methodObject (readonly)

Method name.



105
106
107
# File 'lib/lemon/coverage/snapshot.rb', line 105

def method
  @method
end

#singletonObject (readonly)

Is the method a “class method”, rather than an instance method.



108
109
110
# File 'lib/lemon/coverage/snapshot.rb', line 108

def singleton
  @singleton
end

#targetObject (readonly)

Clsss or module.



102
103
104
# File 'lib/lemon/coverage/snapshot.rb', line 102

def target
  @target
end

Instance Method Details

#<=>(other) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/lemon/coverage/snapshot.rb', line 192

def <=>(other)
  c = (target.name <=> other.target.name)
  return c unless c == 0
  return -1 if singleton  && !other.singleton
  return  1 if !singleton && other.singleton
  method.to_s <=> other.method.to_s
end

#accessObject



154
155
156
157
158
# File 'lib/lemon/coverage/snapshot.rb', line 154

def access
  return :private if private?
  return :protected if protected?
  :public
end

#covered?Boolean

Marked as covered?

Returns:

  • (Boolean)


161
162
163
# File 'lib/lemon/coverage/snapshot.rb', line 161

def covered?
  @covered
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


180
181
182
183
184
185
186
# File 'lib/lemon/coverage/snapshot.rb', line 180

def eql?(other)
  return false unless Unit === other
  return false unless target == other.target
  return false unless method == other.method
  return false unless singleton == other.singleton
  return true
end

#hashObject



166
167
168
# File 'lib/lemon/coverage/snapshot.rb', line 166

def hash
  @target.hash ^ @method.hash ^ @singleton.hash
end

#inspectObject



188
189
190
# File 'lib/lemon/coverage/snapshot.rb', line 188

def inspect
  "#{target}#{singleton ? '.' : '#'}#{method}"
end

#namespaceObject

Alternate name for target.



129
130
131
# File 'lib/lemon/coverage/snapshot.rb', line 129

def namespace
  @target
end

#private?Boolean

Method access is public?

Returns:

  • (Boolean)


144
145
146
# File 'lib/lemon/coverage/snapshot.rb', line 144

def private?
  @private
end

#protected?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/lemon/coverage/snapshot.rb', line 149

def protected?
  @protected
end

#public?Boolean

Method access is public?

Returns:

  • (Boolean)


139
140
141
# File 'lib/lemon/coverage/snapshot.rb', line 139

def public?
  !(@private or @protected)
end

#singleton?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/lemon/coverage/snapshot.rb', line 134

def singleton?
  @singleton
end

#to_sObject Also known as: to_str



171
172
173
174
175
176
177
# File 'lib/lemon/coverage/snapshot.rb', line 171

def to_s
  if @singleton
    "#{@target}.#{@method}"
  else
    "#{@target}##{@method}"
  end
end