Class: Rooibos::Transition

Inherits:
Object
  • Object
show all
Defined in:
lib/rooibos/transition.rb

Overview

Transition represents a normalized [model, command] tuple from Update. Use Transition.from to convert DWIM return values.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(update_return, previous_model) ⇒ Object

Creates a Transition from an Update return value.

Handles:

nil

preserve previous model, no command

model

new model, no command

command

previous model, command

[model, command]

as-is



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rooibos/transition.rb', line 19

def self.from(update_return, previous_model)
  case update_return
  when nil
    new(model: previous_model, command: nil)
  when -> (r) { r.respond_to?(:rooibos_command?) && r.rooibos_command? }
    new(model: previous_model, command: update_return)
  when Array
    case update_return
    in [model, command] if command.nil? || (command.respond_to?(:rooibos_command?) && command.rooibos_command?)
      new(model:, command:)
    else
      warn_suspicious_callable(update_return)
      new(model: update_return, command: nil)
    end
  else
    new(model: update_return, command: nil)
  end
end

.initial(model) ⇒ Object

Creates an initial Transition with no command.



39
40
41
# File 'lib/rooibos/transition.rb', line 39

def self.initial(model)
  new(model:, command: nil)
end

Instance Method Details

#to_aObject Also known as: to_ary, deconstruct

Converts to [model, command] tuple.



74
# File 'lib/rooibos/transition.rb', line 74

def to_a = [model, command]

#with_added_command(new_command) ⇒ Object

Returns new Transition with command added (batched if existing).



58
59
60
61
62
63
# File 'lib/rooibos/transition.rb', line 58

def with_added_command(new_command)
  return self unless new_command
  return with_command(new_command) unless command

  with_command(Command.batch(command, new_command))
end

#with_command(new_command) ⇒ Object

Returns new Transition with an updated command, or self if nil.



51
52
53
54
55
# File 'lib/rooibos/transition.rb', line 51

def with_command(new_command)
  return self unless new_command

  Transition.new(model:, command: new_command)
end

#with_model(new_model) ⇒ Object

Returns new Transition with an updated model, or self if nil.



44
45
46
47
48
# File 'lib/rooibos/transition.rb', line 44

def with_model(new_model)
  return self unless new_model

  Transition.new(model: new_model, command:)
end

#with_separate_command(new_command) ⇒ Object

Returns new Transition with command added via Separate (for observe).



66
67
68
69
70
71
# File 'lib/rooibos/transition.rb', line 66

def with_separate_command(new_command)
  return self unless new_command
  return with_command(new_command) unless command

  with_command(Command.const_get(:Separate).new(commands: [command, new_command]))
end