Class: Less::Interaction

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

Constant Summary collapse

VERSION =
"0.1.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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/less_interactions/interaction.rb', line 7

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

  ex = self.class.expectations.dup
  n = ex.keep_if {|name, allow_nil| allow_nil.has_key?(:allow_nil) && allow_nil[:allow_nil]}
  nils = {}
  n.each do |name, val|
    nils[name] = nil
  end
  self.class.any_expectations.flatten.each {|e| nils[e] = nil}

  nils.merge(options).each do |name, value|
    instance_variable_set "@#{name}", value
    if respond_to?( "#{name}=" ) && !value.nil?
      send "#{name}=", value
    end
  end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

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



63
64
65
66
67
68
69
70
71
72
# File 'lib/less_interactions/interaction.rb', line 63

def self.expects(*parameters)
  if parameters.last.is_a?(Hash)
    options = parameters.pop
  else
    options = {}
  end
  __setup_expecations parameters do |parameter|
    add_expectation(parameter, options)
  end
end

.expects_any(*parameters) ⇒ Object



74
75
76
77
78
# File 'lib/less_interactions/interaction.rb', line 74

def self.expects_any *parameters
  __setup_expecations parameters do |parameter|
  end
  add_any_expectation(parameters)
end

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

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

Parameters:

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


48
49
50
51
52
53
# File 'lib/less_interactions/interaction.rb', line 48

def self.run(context = {}, options = {})
  me = new(context, options)
  me.send :expectations_met?
  me.init
  me.run
end

Instance Method Details

#initObject



40
41
# File 'lib/less_interactions/interaction.rb', line 40

def init
end

#runObject

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

The default implementation raises an Less::InvalidInteractionError



36
37
38
# File 'lib/less_interactions/interaction.rb', line 36

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