Class: Datacaster::Checker

Inherits:
Base
  • Object
show all
Defined in:
lib/datacaster/checker.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(error_key = nil, &block) ⇒ Checker

Returns a new instance of Checker.



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

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

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

  @check = block
end

Instance Method Details

#cast(object, runtime:) ⇒ Object



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

def cast(object, runtime:)
  if Runtimes::Base.(runtime, @check, object)
    Datacaster.ValidResult(object)
  else
    Datacaster.ErrorResult(I18nValues::Key.new(@error_keys, value: object))
  end
end

#inspectObject



20
21
22
# File 'lib/datacaster/checker.rb', line 20

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