Class: RSpecSystem::Helper Abstract
- Inherits:
-
Object
- Object
- RSpecSystem::Helper
- Defined in:
- lib/rspec-system/helper.rb
Overview
Subclass and override methods to create a helper object
This class represents an abstract ‘helper’ object.
It provides some DSL and convenience helpers to create rspec-system compatible helper objects so others can create their own helper methods with the same syntactical sugar.
Start by sub-classing this feature and providing your own #execute method, name & properties declaration. See other sub-classes for examples on proper usage.
Direct Known Subclasses
Class Attribute Summary collapse
-
.name_value ⇒ Object
Returns the value of attribute name_value.
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Options when called.
-
#rd ⇒ Object
readonly
private
Cache of previous result data.
Class Method Summary collapse
-
.name(name) ⇒ Object
DSL method for setting the helper name.
-
.properties(*props) ⇒ Object
Accepts a list of properties to automatically create.
Instance Method Summary collapse
-
#[](key) ⇒ Object
This allows the data to be treated as a hash.
-
#default_node ⇒ Object
Return default node.
-
#execute ⇒ Hash <Symbol, Any>
abstract
This method is executed to retrieve the data for this helper.
-
#get_node_by_name(name) ⇒ RSpecSystem::Node
Returns a node by its name.
-
#initialize(opts, clr, &block) ⇒ Helper
constructor
abstract
Setup the helper object.
-
#name ⇒ String
Return the helper name of this helper object.
-
#refresh ⇒ void
(also: #run)
Refresh the data, re-running the action associated with this helper.
-
#result_data ⇒ RSpecSystem::Result
private
Internal method to return any result data from resource execution time.
-
#to_hash ⇒ Hash
Retrieve the data from this helper object as a hash.
-
#to_s ⇒ String
String representation of helper.
Constructor Details
#initialize(opts, clr, &block) ⇒ Helper
Override, but make sure you call super(opts, clr, &block)
Setup the helper object.
Here we establish laziness detection, provide the default :node setting and handle been called as a block automatically for the consumer. This is the main setup for the magic that is behind these helper objects.
This initialize method is usually not overridden for simple cases, but can be overridden for the purposes of munging options and providing defaults.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rspec-system/helper.rb', line 55 def initialize(opts, clr, &block) dn = default_node # This is a test for the context of how this command was called # # If clr is Class or Object then it could be called as a subject, and it # should lazy execute its action. lazy = nil if [Class, Object].include?(clr.class) # presumes being used as a subject lazy = true elsif clr.is_a? RSpec::Core::ExampleGroup # anything inside an 'it' lazy = false else # We presume everything else wants results immediately lazy = false end # Merge defaults and such @opts = { :node => opts[:n] || dn, :n => opts[:node] || dn, :timeout => opts[:timeout] || 0, :lazy => lazy, }.merge(opts) # Try to figure out :node using the node helper if a string is passed if @opts[:node].is_a? String @opts[:n] = @opts[:node] = get_node_by_name(@opts[:node]) end # Try to lookup result_data now, unless we are lazy result_data unless @opts[:lazy] # If called as a block, yield the result as a block if block_given? yield(self) end end |
Class Attribute Details
.name_value ⇒ Object
Returns the value of attribute name_value.
26 27 28 |
# File 'lib/rspec-system/helper.rb', line 26 def name_value @name_value end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Options when called
23 24 25 |
# File 'lib/rspec-system/helper.rb', line 23 def opts @opts end |
#rd ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Cache of previous result data
20 21 22 |
# File 'lib/rspec-system/helper.rb', line 20 def rd @rd end |
Class Method Details
.name(name) ⇒ Object
DSL method for setting the helper name
31 32 33 |
# File 'lib/rspec-system/helper.rb', line 31 def name(name) @name_value = name end |
.properties(*props) ⇒ Object
Accepts a list of properties to automatically create
38 39 40 41 42 |
# File 'lib/rspec-system/helper.rb', line 38 def properties(*props) props.each do |prop| define_method(prop) { result_data.send(prop) } end end |
Instance Method Details
#[](key) ⇒ Object
This allows the data to be treated as a hash
142 143 144 |
# File 'lib/rspec-system/helper.rb', line 142 def [](key) result_data[key] end |
#default_node ⇒ Object
Return default node
168 169 170 |
# File 'lib/rspec-system/helper.rb', line 168 def default_node RSpecSystem::NodeSet.create.default_node end |
#execute ⇒ Hash <Symbol, Any>
Always override this method with your own execution routine.
This method is executed to retrieve the data for this helper. It is always overridden by sub-classes.
Here we perform the actual step to retrieve the helper data, returning the result as a basic hash which gets stored for later retrieval via the helper object.
103 104 105 |
# File 'lib/rspec-system/helper.rb', line 103 def execute raise "The #execute method has not be overridden in this class" end |
#get_node_by_name(name) ⇒ RSpecSystem::Node
Returns a node by its name.
To be used by helpers that wish to retrieve a node by its name.
178 179 180 |
# File 'lib/rspec-system/helper.rb', line 178 def get_node_by_name(name) RSpecSystem::NodeSet.create.nodes[name] end |
#name ⇒ String
Return the helper name of this helper object
156 157 158 |
# File 'lib/rspec-system/helper.rb', line 156 def name self.class.name_value end |
#refresh ⇒ void Also known as: run
This method returns an undefined value.
Refresh the data, re-running the action associated with this helper.
130 131 132 133 134 |
# File 'lib/rspec-system/helper.rb', line 130 def refresh @rd = nil result_data nil end |
#result_data ⇒ RSpecSystem::Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Internal method to return any result data from resource execution time
If there are no previous results, it will execute the resource action and return those result. The action only runs once, so subsequent requests return the last result.
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rspec-system/helper.rb', line 115 def result_data return rd unless rd.nil? begin Timeout::timeout(opts[:timeout]) do @rd = RSpecSystem::Result.new(execute) end rescue Timeout::Error => e raise RSpecSystem::Exception::TimeoutError, e. end end |
#to_hash ⇒ Hash
Retrieve the data from this helper object as a hash
149 150 151 |
# File 'lib/rspec-system/helper.rb', line 149 def to_hash result_data.to_hash end |
#to_s ⇒ String
String representation of helper
163 164 165 |
# File 'lib/rspec-system/helper.rb', line 163 def to_s name + "(" + opts.inspect + ")" end |