Class: Fosm::Lifecycle::GuardDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/fosm/lifecycle/guard_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, &block) ⇒ GuardDefinition

Returns a new instance of GuardDefinition.



6
7
8
9
# File 'lib/fosm/lifecycle/guard_definition.rb', line 6

def initialize(name:, &block)
  @name = name
  @block = block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/fosm/lifecycle/guard_definition.rb', line 4

def name
  @name
end

Instance Method Details

#call(record) ⇒ Object



11
12
13
# File 'lib/fosm/lifecycle/guard_definition.rb', line 11

def call(record)
  @block.call(record)
end

#evaluate(record) ⇒ Object

🆕 Evaluate guard and return [allowed, reason] tuple Supports: true/false (legacy), String (failure reason), [:fail, reason]



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fosm/lifecycle/guard_definition.rb', line 17

def evaluate(record)
  result = call(record)

  case result
  when true
    [ true, nil ]
  when false
    [ false, nil ]
  when String
    # String is treated as failure reason
    [ false, result ]
  when Array
    result[0] == :fail ? [ false, result[1] ] : [ true, nil ]
  else
    # 🆕 Any other truthy value is treated as passing
    # Only false/nil fails; everything else passes
    result ? [ true, nil ] : [ false, nil ]
  end
end