Class: Speck
- Inherits:
-
Object
- Object
- Speck
- 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
-
.stack ⇒ Object
The current
Speckexecution stack. -
.unbound ⇒ Object
All specks not bound to an environment.
Instance Attribute Summary collapse
-
#block ⇒ Object
The block to be executed.
-
#checks ⇒ Object
The checks involved in the current
Speck. -
#children ⇒ Object
Specks which consider thisSpeckto be their environment. -
#environment ⇒ Object
The
environmentof aSpeckis anotherSpeck, describing some sort of parent. -
#target ⇒ Object
The
targetof a speck is usually the object which it is intended to describe (and test) the functionality of (Usually, this will be an instance ofClass,Module,Methodfor “class” methods, orUnboundMethodfor instance methods).
Class Method Summary collapse
-
.current ⇒ Object
Returns the top
Speckon the execution stack (the one currently in the process of executing). -
.for(object) ⇒ Object
Retreives the
Specks defiend for a given object, or, if none are defined, creates a new (empty)Speckfor it. -
.on(object) ⇒ Object
Functions like
Speck::for, without creating a newSpeckif none are defined.
Instance Method Summary collapse
-
#execute ⇒ Object
Executes the
Speck. -
#initialize(*environment, &block) ⇒ Speck
constructor
Creates a new
Speck.
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
.stack ⇒ Object
The current Speck execution stack
17 18 19 |
# File 'lib/speck.rb', line 17 def stack @stack end |
.unbound ⇒ Object
All specks not bound to an environment
11 12 13 |
# File 'lib/speck.rb', line 11 def unbound @unbound end |
Instance Attribute Details
#block ⇒ Object
The block to be executed
63 64 65 |
# File 'lib/speck.rb', line 63 def block @block end |
#checks ⇒ Object
The checks involved in the current Speck
88 89 90 |
# File 'lib/speck.rb', line 88 def checks @checks end |
#children ⇒ Object
Specks which consider this Speck to be their environment
67 68 69 |
# File 'lib/speck.rb', line 67 def children @children end |
#environment ⇒ Object
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 |
#target ⇒ Object
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
.current ⇒ Object
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.
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
#execute ⇒ Object
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 |