Class: Less::Interaction
- Inherits:
-
Object
- Object
- Less::Interaction
- Defined in:
- lib/version.rb,
lib/less_interactions/interaction.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Class Method Summary collapse
-
.expects(*parameters) ⇒ Object
Expect certain parameters to be present.
- .expects_any(*parameters) ⇒ Object
-
.run(context = {}, options = {}) ⇒ Object
Run your interaction.
Instance Method Summary collapse
- #init ⇒ Object
-
#initialize(context = {}, options = {}) ⇒ Interaction
constructor
Initialize the objects for an interaction.
-
#run ⇒ Object
Definition of the interaction itself.
Constructor Details
#initialize(context = {}, options = {}) ⇒ Interaction
Initialize the objects for an interaction.
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 = {}, = {}) #the param context = {} is to allow for interactions with no context if context.is_a? Hash .merge! context #context is not a Context so merge it in else [: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().each do |name, value| instance_variable_set "@#{name}", value if respond_to?( "#{name}=" ) && !value.nil? send "#{name}=", value end end end |
Instance Attribute Details
#context ⇒ Object (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.
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) = parameters.pop else = {} end __setup_expecations parameters do |parameter| add_expectation(parameter, ) 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.
48 49 50 51 52 53 |
# File 'lib/less_interactions/interaction.rb', line 48 def self.run(context = {}, = {}) me = new(context, ) me.send :expectations_met? me.init me.run end |
Instance Method Details
#init ⇒ Object
40 41 |
# File 'lib/less_interactions/interaction.rb', line 40 def init end |
#run ⇒ Object
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 |