class Less::Interaction

Constants

VERSION

Attributes

context[R]

Public Class Methods

expects(*parameters) click to toggle source

Expect certain parameters to be present. If any parameter can't be found, a {MissingParameterError} will be raised. @overload expects(*parameters)

@param *parameters A list of parameters that your interaction expects to find.

@overload expects(*parameters, options)

@param *parameters A list of parameters that your interaction expects to find.
@param options A list of options for the exclusion
@option options :allow_nil Allow nil values to be passed to the interaction, only check to see whether the key has been set
# File lib/less_interactions/interaction.rb, line 55
def self.expects(*parameters)
  if parameters.last.is_a?(Hash)
    options = parameters.pop
  else
    options = {}
  end

  parameters.each do |parameter|
    add_reader(parameter)
    add_expectation(parameter, options)
  end
end
expects_any(*parameters) click to toggle source
# File lib/less_interactions/interaction.rb, line 68
def self.expects_any *parameters
  parameters.each do |parameter|
    add_reader(parameter)
  end
  add_any_expectation(parameters)
end
new(context = {}, options = {}) click to toggle source

Initialize the objects for an interaction. @param [Object] context The context for running an interaction. Optional param. @param [Hash] options The options are passed when running an interaction. Optional param.

# File lib/less_interactions/interaction.rb, line 6
def initialize(context = {}, options = {})
  #the param context = {} is to allow for interactions with no context
  if context.is_a? Hash
    options.merge! context #context is not a Context so merge it in
  else
    options[:context] = context # add context to the options so will get the ivar and getter
  end

  self.all_params = options
  set_instance_variables
  options.each do |name, value|
    if respond_to?( "#{name}=" ) 
      send "#{name}=", value   
    end
  end
end
returns(*args) click to toggle source

Make an attr_accessor alias for what you are expecting to be returned Need to return self in the run method for to use this

# File lib/less_interactions/interaction.rb, line 77
def self.returns(*args)
  attr_accessor(*args)
end
run(context = {}, params = {}) click to toggle source

Run your interaction. @param [Object] context @param [Hash] params

This will initialize your interaction with the params you pass to it and then call its {#run} method.

# File lib/less_interactions/interaction.rb, line 40
def self.run(context = {}, params = {})
  me = new(context, params)
  me.send :expectations_met?
  me.init
  me.run
end

Private Class Methods

add_any_expectation(parameters) click to toggle source
# File lib/less_interactions/interaction.rb, line 118
def self.add_any_expectation(parameters)
  new_ex = MultipleChoiceExpectation.new(parameters)
  if any_expectations.none? {|ex| ex.parameters == new_ex.parameters }
    any_expectations << new_ex
  end
end
add_expectation(parameter, options) click to toggle source
# File lib/less_interactions/interaction.rb, line 111
def self.add_expectation(parameter, options)
  ex = Expectation.new(parameter, options)
  if expectations.none? { |e| e.parameter == parameter }
    expectations << ex
  end
end
add_reader(param) click to toggle source
# File lib/less_interactions/interaction.rb, line 106
def self.add_reader param
  methods = (self.instance_methods + self.private_instance_methods)
  self.send(:attr_reader, param.to_sym) unless methods.member?(param.to_sym)
end
any_expectations() click to toggle source
# File lib/less_interactions/interaction.rb, line 129
def self.any_expectations
  @any_expectations ||= ExpectationArray.new()
end
expectations() click to toggle source
# File lib/less_interactions/interaction.rb, line 133
def self.expectations
  @expectations ||= ExpectationArray.new()
end

Public Instance Methods

init() click to toggle source
# File lib/less_interactions/interaction.rb, line 32
def init
end
run() click to toggle source

Definition of the interaction itself. You should override this in your interactions

The default implementation raises an {InvalidInteractionError}

# File lib/less_interactions/interaction.rb, line 28
def run
  raise InvalidInteractionError, "You must override the run instance method in #{self.class}"
end

Private Instance Methods

all_params() click to toggle source
# File lib/less_interactions/interaction.rb, line 83
def all_params
  @__all_params
end
all_params=(p) click to toggle source
# File lib/less_interactions/interaction.rb, line 87
def all_params=(p)
  @__all_params = p
end
expectations_met?() click to toggle source
# File lib/less_interactions/interaction.rb, line 125
def expectations_met?
  self.class.any_expectations.verify!(all_params) && self.class.expectations.verify!(all_params)
end
set_instance_variables() click to toggle source
# File lib/less_interactions/interaction.rb, line 91
def set_instance_variables
  all_params.each do |name, value|
    instance_variable_set "@#{name}", value
  end

  self.class.expectations.each do |expectation|
    name = expectation.parameter
    if all_params.has_key?(name)
      instance_variable_set "@#{name}", all_params[name]
    elsif expectation.allows_nil?
      instance_variable_set "@#{name}", nil
    end
  end
end