Class: Datacaster::Caster
Instance Method Summary collapse
- #cast(object, runtime:) ⇒ Object
-
#initialize(&block) ⇒ Caster
constructor
A new instance of Caster.
- #inspect ⇒ Object
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(&block) ⇒ Caster
Returns a new instance of Caster.
3 4 5 6 7 |
# File 'lib/datacaster/caster.rb', line 3 def initialize(&block) raise "Expected block" unless block_given? @cast = block end |
Instance Method Details
#cast(object, runtime:) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/datacaster/caster.rb', line 9 def cast(object, runtime:) result = Runtimes::Base.(runtime, @cast, object) if defined?(Dry::Monads::Result) && result.is_a?(Dry::Monads::Result) result = result.success? ? Datacaster.ValidResult(result.value!) : Datacaster.ErrorResult(result.failure) end raise TypeError.new("Either Datacaster::Result or Dry::Monads::Result " \ "should be returned from cast block, instead got #{result.inspect}") unless result.is_a?(Datacaster::Result) result end |
#inspect ⇒ Object
22 23 24 |
# File 'lib/datacaster/caster.rb', line 22 def inspect "#<Datacaster::Caster>" end |