Class: Preferable::Field
- Inherits:
-
Object
- Object
- Preferable::Field
- 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
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#default?(value) ⇒ Boolean
Is value equal to the default.
-
#initialize(name, type, options = {}) ⇒ Field
constructor
Create a new Field.
-
#invalid?(value) ⇒ Boolean
Opposite of #valid?.
-
#type_cast(value, to = self.type) ⇒ Object
Converts a value.
-
#valid?(value) ⇒ Boolean
Returns true if a value is assignable, else false.
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?
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 @options = .dup @default = type_cast @options.delete(:default) end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
5 6 7 |
# File 'lib/preferable/field.rb', line 5 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/preferable/field.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/preferable/field.rb', line 5 def @options end |
#type ⇒ Object (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.
49 50 51 |
# File 'lib/preferable/field.rb', line 49 def default?(value) value == default end |
#invalid?(value) ⇒ Boolean
Opposite of #valid?
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, [:cast]) } if [: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.
36 37 38 39 40 41 |
# File 'lib/preferable/field.rb', line 36 def valid?(value) result = true result = [:if].call(value) if result && [:if] result = ![:unless].call(value) if result && [:unless] result end |