Class: LetsDoThis::Scenario

Inherits:
Object
  • Object
show all
Defined in:
lib/lets_do_this/scenario.rb

Constant Summary collapse

ACT_SEQUENCE =
[].freeze
STAGE_ATTRS_WHITELIST =
i[].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Scenario

Returns a new instance of Scenario.



11
12
13
14
15
16
# File 'lib/lets_do_this/scenario.rb', line 11

def initialize(input)
  @input = input
  @errors = Errors.new
  @act_sequence = self.class::ACT_SEQUENCE.map(&:new)
  @stage_attrs_whitelist = self.class::STAGE_ATTRS_WHITELIST
end

Instance Attribute Details

#act_sequenceObject (readonly)

Returns the value of attribute act_sequence.



6
7
8
# File 'lib/lets_do_this/scenario.rb', line 6

def act_sequence
  @act_sequence
end

Instance Method Details

#act_out!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lets_do_this/scenario.rb', line 18

def act_out!
  raise LetsDoThis::EmptyActSequence if @act_sequence.empty?

  raise LetsDoThis::EmptyStageAttrsWhitelist if @stage_attrs_whitelist.empty?

  go_on_stage!(@input)
  @act_sequence.each do
    act = _1.follow_instructions!(stage)
    unless act.success?
      @errors.add(act.class.name, act.errors)
      break
    end

    go_on_stage!(act.result)
  end
  self
end

#failure?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/lets_do_this/scenario.rb', line 53

def failure?
  !success?
end

#go_on_stage!(hash) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
# File 'lib/lets_do_this/scenario.rb', line 40

def go_on_stage!(hash)
  raise(ArgumentError) unless hash.is_a?(Hash)

  w_symbolized_keys = hash.transform_keys { |key| key.to_sym rescue key }
  w_symbolized_keys.slice(*@stage_attrs_whitelist).each_pair do |key, value|
    stage.public_send("#{key}=", value)
  end
end

#stageObject



36
37
38
# File 'lib/lets_do_this/scenario.rb', line 36

def stage
  @stage ||= OpenStruct.new
end

#success?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/lets_do_this/scenario.rb', line 49

def success?
  @errors.empty?
end