Class: Magiq::Types::UPC

Inherits:
Type
  • Object
show all
Defined in:
lib/magiq/types.rb

Constant Summary collapse

UPC_RNG =
/[^0-9]/
UPC_MAX =
9999999999999

Instance Attribute Summary

Attributes inherited from Type

#raw

Instance Method Summary collapse

Methods inherited from Type

cast, #initialize

Constructor Details

This class inherits a constructor from Magiq::Types::Type

Instance Method Details

#cast!Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/magiq/types.rb', line 99

def cast!
  v = raw.to_s.gsub(UPC_RNG, '').to_i

  if v > 0 && v <= UPC_MAX
    v
  else
    bad! "provided value of #{raw.inspect} is not permitted, it must " \
    "be a valid UPC with a numerical value between zero and #{UPC_MAX}."
  end
end