Class: Philiprehberger::DataMapper::FieldDefinition
- Inherits:
-
Object
- Object
- Philiprehberger::DataMapper::FieldDefinition
- Defined in:
- lib/philiprehberger/data_mapper/field_definition.rb
Constant Summary collapse
- BOOLEAN_TRUE_VALUES =
%w[true 1 yes].freeze
- BOOLEAN_FALSE_VALUES =
%w[false 0 no].freeze
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#split_delimiter ⇒ Object
readonly
Returns the value of attribute split_delimiter.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Method Summary collapse
- #apply(value) ⇒ Object
- #conditional? ⇒ Boolean
- #include?(record) ⇒ Boolean
-
#initialize(target, **opts, &transform) ⇒ FieldDefinition
constructor
A new instance of FieldDefinition.
- #valid?(value) ⇒ Boolean
Constructor Details
#initialize(target, **opts, &transform) ⇒ FieldDefinition
Returns a new instance of FieldDefinition.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 11 def initialize(target, **opts, &transform) @target = target @source = opts[:from] || target @default = opts[:default] @type = opts[:type] @condition = opts[:if] @validator = opts[:validate] @split_delimiter = opts[:split] @transform = transform end |
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
6 7 8 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 6 def condition @condition end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
6 7 8 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 6 def default @default end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
6 7 8 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 6 def source @source end |
#split_delimiter ⇒ Object (readonly)
Returns the value of attribute split_delimiter.
6 7 8 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 6 def split_delimiter @split_delimiter end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
6 7 8 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 6 def target @target end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
6 7 8 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 6 def type @type end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
6 7 8 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 6 def validator @validator end |
Instance Method Details
#apply(value) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 22 def apply(value) value = @default if value.nil? value = value.to_s.split(@split_delimiter) if @split_delimiter && value.is_a?(String) value = @transform.call(value) if @transform value = coerce(value) if @type value end |
#conditional? ⇒ Boolean
30 31 32 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 30 def conditional? !@condition.nil? end |
#include?(record) ⇒ Boolean
34 35 36 37 38 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 34 def include?(record) return true unless conditional? @condition.call(record) end |
#valid?(value) ⇒ Boolean
40 41 42 43 44 |
# File 'lib/philiprehberger/data_mapper/field_definition.rb', line 40 def valid?(value) return true unless @validator @validator.call(value) end |