Class: Datacaster::Trier

Inherits:
Base
  • Object
show all
Defined in:
lib/datacaster/trier.rb

Instance Method Summary collapse

Methods included from Mixin

#&, #*, #call, #call_with_runtime, #cast_errors, #i18n_key, #i18n_map_keys, #i18n_scope, #i18n_vars, #then, #with_context, #with_object_context, #with_runtime, #|

Constructor Details

#initialize(catched_exception, error_key = nil, &block) ⇒ Trier

Returns a new instance of Trier.



3
4
5
6
7
8
9
10
11
# File 'lib/datacaster/trier.rb', line 3

def initialize(catched_exception, error_key = nil, &block)
  raise "Expected block" unless block_given?

  @catched_exception = Array(catched_exception)
  @try = block

  @error_keys = ['.try', 'datacaster.errors.try']
  @error_keys.unshift(error_key) if error_key
end

Instance Method Details

#cast(object, runtime:) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/datacaster/trier.rb', line 13

def cast(object, runtime:)
  begin
    Datacaster.ValidResult(Runtimes::Base.(runtime, @try, object))
  rescue *@catched_exception
    Datacaster.ErrorResult(I18nValues::Key.new(@error_keys, value: object))
  end
end

#inspectObject



21
22
23
# File 'lib/datacaster/trier.rb', line 21

def inspect
  "#<Datacaster::Trier>"
end