Class: Less::Interaction

Inherits:
Object
  • Object
show all
Defined in:
lib/version.rb,
lib/less_interactions/interaction.rb

Constant Summary collapse

VERSION =
"0.2.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = {}, options = {}) ⇒ Interaction

Initialize the objects for an interaction.

Parameters:

  • context (Object) (defaults to: {})

    The context for running an interaction. Optional param.

  • options (Hash) (defaults to: {})

    The options are passed when running an interaction. Optional param.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



23
24
25
# File 'lib/less_interactions/interaction.rb', line 23

def context
  @context
end

Class Method Details

.expects(*parameters) ⇒ Object .expects(*parameters, options) ⇒ Object

Expect certain parameters to be present. If any parameter can’t be found, a MissingParameterError will be raised.

Overloads:

  • .expects(*parameters) ⇒ Object

    Parameters:

    • *parameters

      A list of parameters that your interaction expects to find.

  • .expects(*parameters, options) ⇒ Object

    Parameters:

    • *parameters

      A list of parameters that your interaction expects to find.

    • options

      A list of options for the exclusion

    Options Hash (options):

    • :allow_nil (Object)

      Allow nil values to be passed to the interaction, only check to see whether the key has been set



55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



68
69
70
71
72
73
# 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

.returns(*args) ⇒ Object

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



77
78
79
# File 'lib/less_interactions/interaction.rb', line 77

def self.returns(*args)
  attr_accessor(*args)
end

.run(context = {}, params = {}) ⇒ Object

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

Parameters:

  • context (Object) (defaults to: {})
  • params (Hash) (defaults to: {})


40
41
42
43
44
45
# 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

Instance Method Details

#initObject



32
33
# File 'lib/less_interactions/interaction.rb', line 32

def init
end

#runObject

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

The default implementation raises an Less::InvalidInteractionError



28
29
30
# File 'lib/less_interactions/interaction.rb', line 28

def run
  raise InvalidInteractionError, "You must override the run instance method in #{self.class}"
end