Module: Subroutine::TypeCaster

Defined in:
lib/subroutine/type_caster.rb

Defined Under Namespace

Classes: TypeCastError

Class Method Summary collapse

Class Method Details

.cast(value, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/subroutine/type_caster.rb', line 37

def self.cast(value, options = {})
  type = options[:type]
  return value if value.nil? || type.nil?

  caster = casters[type]
  return value unless caster

  caster.call(value, options)

rescue StandardError => e
  raise ::Subroutine::TypeCaster::TypeCastError, e.to_s, e.backtrace
end

.castersObject



27
28
29
# File 'lib/subroutine/type_caster.rb', line 27

def self.casters
  @casters ||= {}
end

.register(*names, &block) ⇒ Object



31
32
33
34
35
# File 'lib/subroutine/type_caster.rb', line 31

def self.register(*names, &block)
  names.each do |n|
    casters[n] = block
  end
end