Module: Screengem::Actor

Defined in:
lib/screengem/actor.rb

Overview

Mixin that allows an actor to perform tasks, ask questions, and to remember and recall tagged values.

Return self for those methods that may be chained in the step definition DSL.

The ability to remember and recall values is used to carry state forward from one step definition to another (as the preferred alternative to instance variables).

Question and task instances (aka primitives) are configured with:

(1) a reference to the actor that is interacting with the primitive
(2) a reference to the screen instance that hosts accessors to the screen elements.

Instance Method Summary collapse

Instance Method Details

#asks(*questions) ⇒ Object

Used by an actor to ask one or more questions in a step definition.



19
20
21
22
23
24
25
26
27
# File 'lib/screengem/actor.rb', line 19

def asks(*questions)
  before_actor_asks(*questions)

  questions.each do |question|
    question.configure(self, screen).answer
  end

  self
end

#before_actor_asks(*_questions) ⇒ Object



29
30
31
# File 'lib/screengem/actor.rb', line 29

def before_actor_asks(*_questions)
  # Hook to customize actor behaviour.
end

#before_actor_performs(*_tasks) ⇒ Object



46
47
48
# File 'lib/screengem/actor.rb', line 46

def before_actor_performs(*_tasks)
  # Hook to customize actor behaviour.
end

#performs(*tasks) ⇒ Object

Used by an actor to perform one or more tasks in a step definition.



36
37
38
39
40
41
42
43
44
# File 'lib/screengem/actor.rb', line 36

def performs(*tasks)
  before_actor_performs(*tasks)

  tasks.each do |task|
    task.configure(self, screen).perform
  end

  self
end

#recall(tag, default: nil, reload: true) ⇒ Object

Used by an actor to recall a value for the specified tag.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/screengem/actor.rb', line 53

def recall(tag, default: nil, reload: true)
  unless recollections.key?(tag) || default.present?
    raise <<~MSG
      #{name} does not recall #{tag}
      #{name} recalls: #{recollections.keys.to_sentence}
    MSG
  end

  recollections.fetch(tag, default).tap do |value|
    value.reload if reload && value.respond_to?(:reload)
  end
end

#remember(facts) ⇒ Object

Used by an actor to remember one or more tagged values.



69
70
71
72
73
# File 'lib/screengem/actor.rb', line 69

def remember(facts)
  recollections.merge!(facts)

  self
end