Class: Flowy::Success

Inherits:
Object
  • Object
show all
Includes:
Result
Defined in:
lib/flowy/success.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Result

_collect_results, _deep_merge, failure, success, wrap

Constructor Details

#initialize(data: {}, warnings: []) ⇒ Success

Returns a new instance of Success.



7
8
9
10
# File 'lib/flowy/success.rb', line 7

def initialize(data: {}, warnings: [])
  @data = data
  @warnings = warnings
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/flowy/success.rb', line 5

def data
  @data
end

#warningsObject (readonly)

Returns the value of attribute warnings.



5
6
7
# File 'lib/flowy/success.rb', line 5

def warnings
  @warnings
end

Instance Method Details

#+(other) ⇒ Object



12
13
14
# File 'lib/flowy/success.rb', line 12

def +(other)
  self.class.new(data: Flowy::Result._deep_merge(data, other.data))
end

#and_thenObject



41
42
43
44
45
46
47
48
# File 'lib/flowy/success.rb', line 41

def and_then
  result = yield self
  unless result.is_a?(Flowy::Success) || result.is_a?(Flowy::Failure)
    raise TypeError, "and_then block must return a Flowy::Success or Flowy::Failure, got #{result.class}"
  end

  result
end

#failure?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/flowy/success.rb', line 28

def failure?
  false
end

#map_failureObject



54
55
56
# File 'lib/flowy/success.rb', line 54

def map_failure(**)
  self
end

#merge_data(extra = nil) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
# File 'lib/flowy/success.rb', line 67

def merge_data(extra = nil)
  extra = block_given? ? yield(data) : extra
  raise ArgumentError, 'merge_data requires a Hash' unless extra.is_a?(Hash)

  self.class.new(data: Flowy::Result._deep_merge(data, extra), warnings: warnings)
end

#on_failureObject



37
38
39
# File 'lib/flowy/success.rb', line 37

def on_failure
  self
end

#on_success {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
# File 'lib/flowy/success.rb', line 32

def on_success
  yield self
  self
end

#or_elseObject



50
51
52
# File 'lib/flowy/success.rb', line 50

def or_else
  self
end

#raise!Object



58
59
60
# File 'lib/flowy/success.rb', line 58

def raise!
  self
end

#success?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/flowy/success.rb', line 24

def success?
  true
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



62
63
64
65
# File 'lib/flowy/success.rb', line 62

def tap
  yield self
  self
end

#to_hashObject



16
17
18
19
20
21
22
# File 'lib/flowy/success.rb', line 16

def to_hash
  {
    success: true,
    data: data,
    warnings: warnings
  }
end