Method: Preferable::Field#initialize
- Defined in:
- lib/preferable/field.rb
#initialize(name, type, options = {}) ⇒ Field
Create a new Field.
Params:
name: The name symbol
type: The type symbol, see Preferable::Field::TYPES
options: The hash (keys as symbols)
Options:
default: The default value.
if: Value assignment proc. Value is only assigned if true is yielded.
unless: Value assignment proc. Value is only assigned if false is yielded.
cast: Only relevant for :array type. Specify the type of the array contents.
Examples:
Field.new :color, :string, :if => lambda {|v| v =~ /^[A-F0-9]{6}$/ }
Field.new :holidays, :array, :cast => :date
Field.new :newsletter, :boolean, :default => false
Field.new :age, :integer, :unless => &:zero?
26 27 28 29 30 31 32 33 |
# File 'lib/preferable/field.rb', line 26 def initialize(name, type, = {}) raise ArgumentError, "Unknown type '#{type}', available types are: #{TYPES.map(&:to_s).join(', ')}" unless TYPES.include?(type.to_sym) @name = name.to_s @type = type.to_sym = .dup @default = type_cast .delete(:default) end |