Class: Dry::Logic::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/logic/result.rb

Constant Summary collapse

SUCCESS =
Class.new {
  def success?
    true
  end

  def failure?
    false
  end
}.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success, id = nil, &block) ⇒ Result

Returns a new instance of Result.



22
23
24
25
26
# File 'lib/dry/logic/result.rb', line 22

def initialize(success, id = nil, &block)
  @success = success
  @id = id
  @serializer = block
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/dry/logic/result.rb', line 18

def id
  @id
end

#serializerObject (readonly)

Returns the value of attribute serializer.



20
21
22
# File 'lib/dry/logic/result.rb', line 20

def serializer
  @serializer
end

#successObject (readonly)

Returns the value of attribute success.



16
17
18
# File 'lib/dry/logic/result.rb', line 16

def success
  @success
end

Instance Method Details

#ast(input = Undefined) ⇒ Object



40
41
42
# File 'lib/dry/logic/result.rb', line 40

def ast(input = Undefined)
  serializer.(input)
end

#failure?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/dry/logic/result.rb', line 32

def failure?
  !success?
end

#success?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/dry/logic/result.rb', line 28

def success?
  success
end

#to_astObject



44
45
46
47
48
49
50
# File 'lib/dry/logic/result.rb', line 44

def to_ast
  if id
    [type, [id, ast]]
  else
    ast
  end
end

#to_sObject



52
53
54
# File 'lib/dry/logic/result.rb', line 52

def to_s
  visit(to_ast)
end

#typeObject



36
37
38
# File 'lib/dry/logic/result.rb', line 36

def type
  success? ? :success : :failure
end