Class: FieldMapper::Custom::Field
- Inherits:
-
Standard::Field
- Object
- Standard::Field
- FieldMapper::Custom::Field
- Defined in:
- lib/field_mapper/custom/field.rb
Constant Summary collapse
- DefaultFlipper =
-> (value, standard_instance: nil) { value }
Constants included from Marshaller
Instance Attribute Summary collapse
-
#custom_to_standard ⇒ Object
readonly
Returns the value of attribute custom_to_standard.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
- #placeholder ⇒ Object
-
#standard_field ⇒ Object
readonly
Returns the value of attribute standard_field.
-
#standard_to_custom ⇒ Object
readonly
Returns the value of attribute standard_to_custom.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
- #custom_flipper?(direction) ⇒ Boolean
- #find_priority_value_mapped_to_standard(standard_value) ⇒ Object
- #find_values_mapped_to_standard(standard_value) ⇒ Object
- #flip_strategy(direction) ⇒ Object
-
#initialize(name, type: nil, desc: nil, default: nil, placeholder: nil, standard_field: nil, custom_to_standard: DefaultFlipper, standard_to_custom: DefaultFlipper, &block) ⇒ Field
constructor
A new instance of Field.
-
#load_values(path_to_csv) ⇒ Object
Adds values to a Field instance that are defined in a CSV file.
- #value(value, standard: nil, priority: nil) ⇒ Object
Methods inherited from Standard::Field
#cast, #find_value, #has_values?, #list?, #list_with_emtpy_default?, #plat?, #plat_field?, #plat_list?, #raw_values
Methods included from Marshaller
Constructor Details
#initialize(name, type: nil, desc: nil, default: nil, placeholder: nil, standard_field: nil, custom_to_standard: DefaultFlipper, standard_to_custom: DefaultFlipper, &block) ⇒ Field
Returns a new instance of Field.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/field_mapper/custom/field.rb', line 21 def initialize( name, type: nil, desc: nil, default: nil, placeholder: nil, standard_field: nil, custom_to_standard: DefaultFlipper, standard_to_custom: DefaultFlipper, &block ) type ||= standard_field.type unless standard_field.nil? super name, type: type, desc: desc, default: default, placeholder: placeholder @standard_field = standard_field @custom_to_standard = custom_to_standard @standard_to_custom = standard_to_custom eigen = class << self; self; end eigen.instance_eval do define_method :custom_to_standard1, &custom_to_standard define_method :standard_to_custom2, &standard_to_custom end end |
Instance Attribute Details
#custom_to_standard ⇒ Object (readonly)
Returns the value of attribute custom_to_standard.
10 11 12 |
# File 'lib/field_mapper/custom/field.rb', line 10 def custom_to_standard @custom_to_standard end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
10 11 12 |
# File 'lib/field_mapper/custom/field.rb', line 10 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
10 11 12 |
# File 'lib/field_mapper/custom/field.rb', line 10 def desc @desc end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/field_mapper/custom/field.rb', line 10 def name @name end |
#placeholder ⇒ Object
48 49 50 |
# File 'lib/field_mapper/custom/field.rb', line 48 def placeholder @placeholder || (standard_field && standard_field.placeholder) end |
#standard_field ⇒ Object (readonly)
Returns the value of attribute standard_field.
10 11 12 |
# File 'lib/field_mapper/custom/field.rb', line 10 def standard_field @standard_field end |
#standard_to_custom ⇒ Object (readonly)
Returns the value of attribute standard_to_custom.
10 11 12 |
# File 'lib/field_mapper/custom/field.rb', line 10 def standard_to_custom @standard_to_custom end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/field_mapper/custom/field.rb', line 10 def type @type end |
#values ⇒ Object (readonly)
Returns the value of attribute values.
10 11 12 |
# File 'lib/field_mapper/custom/field.rb', line 10 def values @values end |
Instance Method Details
#custom_flipper?(direction) ⇒ Boolean
111 112 113 |
# File 'lib/field_mapper/custom/field.rb', line 111 def custom_flipper?(direction) instance_variable_get("@#{direction}") != DefaultFlipper end |
#find_priority_value_mapped_to_standard(standard_value) ⇒ Object
98 99 100 101 102 |
# File 'lib/field_mapper/custom/field.rb', line 98 def find_priority_value_mapped_to_standard(standard_value) matches = find_values_mapped_to_standard(standard_value) match = matches.find { |val| val.priority } match ||= matches.first end |
#find_values_mapped_to_standard(standard_value) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/field_mapper/custom/field.rb', line 91 def find_values_mapped_to_standard(standard_value) values.select do |val| val.standard_value == standard_value || val.standard_value.try(:value) == standard_value end end |
#flip_strategy(direction) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/field_mapper/custom/field.rb', line 104 def flip_strategy(direction) return :compute if custom_flipper?(direction) return :find if has_values? return :find if standard_field.present? && standard_field.has_values? :compute end |
#load_values(path_to_csv) ⇒ Object
Adds values to a Field instance that are defined in a CSV file.
Intended use is from within a Plat class declaration. The format of the CSV file should contain a two columns with a header row. NOTE: An optional priority column can also be included.
81 82 83 84 85 86 87 88 89 |
# File 'lib/field_mapper/custom/field.rb', line 81 def load_values(path_to_csv) CSV.foreach(path_to_csv, :headers => true) do |row| value( row["custom_value"], standard: row["standard_value"], priority: FieldMapper::Types::Boolean.parse(row["priority"]) ) end end |
#value(value, standard: nil, priority: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/field_mapper/custom/field.rb', line 52 def value(value, standard: nil, priority: nil) @values ||= [] @values << FieldMapper::Custom::Value.new( value, field: self, standard_value: standard, priority: priority ) @values.last end |