Class: HCast::Casters::SymbolCaster

Inherits:
Object
  • Object
show all
Defined in:
lib/hcast/casters/symbol_caster.rb

Constant Summary collapse

MAX_SYMBOL_LENGTH =
1000

Class Method Summary collapse

Class Method Details

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



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/hcast/casters/symbol_caster.rb', line 4

def self.cast(value, attr_name, options = {})
  if value.is_a?(Symbol)
    value
  elsif value.is_a?(String)
    if value.length > MAX_SYMBOL_LENGTH
      raise HCast::Errors::CastingError, "is too long to be a symbol"
    else
      value.to_sym
    end
  else
    raise HCast::Errors::CastingError, "should be a symbol"
  end
end