Module: Zx::Methods

Included in:
Zx, Zx
Defined in:
lib/zx.rb

Constant Summary collapse

Success =
->(value = nil, options = {}) { Zx.Success(value, { type: :ok }.merge(options)) }
Failure =
->(value = nil, options = {}) { Zx.Failure(value, { type: :error }.merge(options)) }

Instance Method Summary collapse

Instance Method Details

#Failure(value = nil, options = {}) ⇒ Object



26
27
28
# File 'lib/zx.rb', line 26

def Failure(value = nil, options = {})
  Zx::Result.new.failure!(value, type: options.fetch(:type, :error))
end

#Given(input) ⇒ Object



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

def Given(input)
  Try { input }
end

#Success(value = nil, options = {}) ⇒ Object



22
23
24
# File 'lib/zx.rb', line 22

def Success(value = nil, options = {})
  Zx::Result.new.success!(value, type: options.fetch(:type, :ok))
end

#Try(default = nil, options = {}) ⇒ Object



30
31
32
33
34
# File 'lib/zx.rb', line 30

def Try(default = nil, options = {})
  Success[yield]
rescue StandardError => e
  Failure[default || options.fetch(:or, nil)]
end