Class: Speck

Inherits:
Object
  • Object
show all
Defined in:
lib/speck.rb,
lib/speck/check.rb,
lib/speck/battery.rb

Overview

All library files are required at the bottom, because in this unique case we need Speck defined before we can use it to Speck anything.

Defined Under Namespace

Classes: Battery, Check, Exception

Constant Summary collapse

VERSION =
1
NinjaVar =

This instance variable will be set on target objects to point to the specks for that object

:@_specks_

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*environment, &block) ⇒ Speck

Creates a new Speck.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/speck.rb', line 107

def initialize *environment, &block
  self.target = environment.pop
  
  environment = environment.inject do |prev, curr|
    raise Exception::EnvironmentConflict if Speck::for(curr).first.environment and Speck::for(curr).first.environment != Speck::for(prev).first
    Speck::for(curr).first.environment = Speck::for(prev).first
    curr
  end
  
  self.environment = environment ? Speck::for(environment).first : Speck.current
  @block = block || lambda {}
end

Class Attribute Details

.stackObject

The current Speck execution stack

See Also:

  • #current


17
18
19
# File 'lib/speck.rb', line 17

def stack
  @stack
end

.unboundObject

All specks not bound to an environment



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

def unbound
  @unbound
end

Instance Attribute Details

#blockObject

The block to be executed



63
64
65
# File 'lib/speck.rb', line 63

def block
  @block
end

#checksObject

The checks involved in the current Speck



88
89
90
# File 'lib/speck.rb', line 88

def checks
  @checks
end

#childrenObject

Specks which consider this Speck to be their environment



67
68
69
# File 'lib/speck.rb', line 67

def children
  @children
end

#environmentObject

The environment of a Speck is another Speck, describing some sort of parent. The environment of a Speck describing an UnboundMethod, for instance, would most likely be a Speck describing a Module or Class on which that method is defined



75
76
77
# File 'lib/speck.rb', line 75

def environment
  @environment
end

#targetObject

The target of a speck is usually the object which it is intended to describe (and test) the functionality of (Usually, this will be an instance of Class, Module, Method for “class” methods, or UnboundMethod for instance methods)



96
97
98
# File 'lib/speck.rb', line 96

def target
  @target
end

Class Method Details

.currentObject

Returns the top Speck on the execution stack (the one currently in the process of executing)

When your Specks are being run, there is a stack of Speck objects, consisting of the current nesting list of Specks being run.



26
27
28
# File 'lib/speck.rb', line 26

def current
  stack.last
end

.for(object) ⇒ Object

Retreives the Specks defiend for a given object, or, if none are defined, creates a new (empty) Speck for it.

It’s worth noting that Module#instance_method returns a new UnboundMethod object every time you call it, even for the same method… so you can’t retreive Specks assigned to UnboundMethods via Module#instance_method with this method.



38
39
40
41
42
# File 'lib/speck.rb', line 38

def for object
  specks = Speck::on object
  specks << Speck.new(object) if specks.empty?
  return specks
end

.on(object) ⇒ Object

Functions like Speck::for, without creating a new Speck if none are defined.

See Also:

  • `Speck`Speck::for`


49
50
51
52
# File 'lib/speck.rb', line 49

def on object
  object.instance_variable_get(NinjaVar) ||
    object.instance_variable_set(NinjaVar, Array.new)
end

Instance Method Details

#executeObject

Executes the Speck.



122
123
124
125
126
# File 'lib/speck.rb', line 122

def execute
  Speck.stack << self
  @block.call
  Speck.stack.pop
end