Module: Attributary::DSL::Castings

Included in:
ClassMethods
Defined in:
lib/attributary/dsl/castings.rb

Instance Method Summary collapse

Instance Method Details

#_attributary_cast_class(type) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/attributary/dsl/castings.rb', line 10

def _attributary_cast_class(type)
  cast_klass_name = _attributary_cast_class_name(type)
  cast_klass = cast_klass_name.safe_constantize
  if cast_klass.nil?
    raise NameError, "#{cast_klass_name} is not a valid type."
  end
  unless cast_klass.respond_to?(:cast_to)
    raise NoMethodError, "#{cast_klass} should have a class-method of cast_to"
  end
  cast_klass
end

#_attributary_cast_class_name(type) ⇒ Object



22
23
24
# File 'lib/attributary/dsl/castings.rb', line 22

def _attributary_cast_class_name(type)
  "Attributary::Types::#{type.to_s.split('_').map(&:capitalize).join}Type"
end

#_attributary_cast_to(type, value) ⇒ Object



5
6
7
8
# File 'lib/attributary/dsl/castings.rb', line 5

def _attributary_cast_to(type, value)
  cast_klass = _attributary_cast_class(type)
  cast_klass.cast_to(value)
end