class RSpecSystem::Helper

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.

@abstract Subclass and override methods to create a helper object

Attributes

name_value[RW]
opts[R]

Options when called

rd[R]

Cache of previous result data @api private

Public Class Methods

name(name) click to toggle source

DSL method for setting the helper name

@param name [String] unique helper method name

# File lib/rspec-system/helper.rb, line 31
def name(name)
  @name_value = name
end
new(opts, clr) { |self| ... } click to toggle source

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.

@abstract Override, but make sure you call super(opts, clr, &block)

# 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
properties(*props) click to toggle source

Accepts a list of properties to automatically create

@param props [Array <Symbol>] an array of property methods to create

# File lib/rspec-system/helper.rb, line 38
def properties(*props)
  props.each do |prop|
    define_method(prop) { result_data.send(prop) }
  end
end

Public Instance Methods

[](key) click to toggle source

This allows the data to be treated as a hash

@param key [Symbol] key of value to retrieve

# File lib/rspec-system/helper.rb, line 142
def [](key)
  result_data[key]
end
default_node() click to toggle source

Return default node

# File lib/rspec-system/helper.rb, line 168
def default_node
  RSpecSystem::NodeSet.create.default_node
end
execute() click to toggle source

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.

@return [Hash <Symbol, Any>] return a hash of results, with symbolic keys @abstract Always override this method with your own execution routine.

# 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) click to toggle source

Returns a node by its name.

To be used by helpers that wish to retrieve a node by its name.

@param name [String] name of the node @return [RSpecSystem::Node] node found

# File lib/rspec-system/helper.rb, line 178
def get_node_by_name(name)
  RSpecSystem::NodeSet.create.nodes[name]
end
name() click to toggle source

Return the helper name of this helper object

@return [String] name of helper

# File lib/rspec-system/helper.rb, line 156
def name
  self.class.name_value
end
refresh() click to toggle source

Refresh the data, re-running the action associated with this helper.

@return [void]

# File lib/rspec-system/helper.rb, line 130
def refresh
  @rd = nil
  result_data
  nil
end
Also aliased as: run
result_data() click to toggle source

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.

@return [RSpecSystem::Result] result object @api private

# 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.message
  end
end
run()

Allow run as an alias to refresh

Alias for: refresh
to_hash() click to toggle source

Retrieve the data from this helper object as a hash

@return [Hash] result data as a hash

# File lib/rspec-system/helper.rb, line 149
def to_hash
  result_data.to_hash
end
to_s() click to toggle source

String representation of helper

@return [String] helper_name(args) formatted string

# File lib/rspec-system/helper.rb, line 163
def to_s
  name + "(" + opts.inspect + ")"
end