Class: Preferable::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/preferable/field.rb

Constant Summary collapse

TYPES =
[:string, :integer, :float, :boolean, :date, :datetime, :array].to_set.freeze
TRUE_VALUES =
[true, 1, '1', 't', 'T', 'true', 'TRUE'].to_set.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Field

Create a new Field.

Params:

name: The name symbol
type: The type symbol, see Preferable::Field::TYPES
options: The options 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?

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
# File 'lib/preferable/field.rb', line 26

def initialize(name, type, options = {})
  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
  @options = options.dup
  @default = type_cast @options.delete(:default)
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/preferable/field.rb', line 5

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/preferable/field.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/preferable/field.rb', line 5

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/preferable/field.rb', line 5

def type
  @type
end

Instance Method Details

#default?(value) ⇒ Boolean

Is value equal to the default. . Assumes given value is already type-casted.

Returns:

  • (Boolean)


49
50
51
# File 'lib/preferable/field.rb', line 49

def default?(value)
  value == default
end

#invalid?(value) ⇒ Boolean

Opposite of #valid?

Returns:

  • (Boolean)


44
45
46
# File 'lib/preferable/field.rb', line 44

def invalid?(value)
  !valid?(value)
end

#type_cast(value, to = self.type) ⇒ Object

Converts a value.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/preferable/field.rb', line 54

def type_cast(value, to = self.type)
  return nil unless value

  case to
  when :string
    value.to_s
  when :integer
    value.to_i
  when :boolean
    TRUE_VALUES.include?(value)
  when :datetime
    to_time(value)
  when :date
    to_time(value).try(:to_date)
  when :float
    value.to_f
  when :array
    Array.wrap(value).tap do |wrap|
      wrap.map! {|item| type_cast(item, options[:cast]) } if options[:cast]
    end
  else
    value
  end
end

#valid?(value) ⇒ Boolean

Returns true if a value is assignable, else false. Assumes given value is already type-casted.

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'lib/preferable/field.rb', line 36

def valid?(value)
  result = true
  result = options[:if].call(value) if result && options[:if]
  result = !options[:unless].call(value) if result && options[:unless]
  result
end