Class: FieldMapper::Standard::Field
- Inherits:
-
Object
- Object
- FieldMapper::Standard::Field
- Includes:
- Marshaller
- Defined in:
- lib/field_mapper/standard/field.rb
Direct Known Subclasses
Constant Summary
Constants included from Marshaller
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#desc ⇒ Object
readonly
Returns the value of attribute desc.
-
#name ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute name.
-
#placeholder ⇒ Object
Returns the value of attribute placeholder.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #cast(value, as_single_value: false) ⇒ Object
- #find_value(value) ⇒ Object
- #has_values? ⇒ Boolean
-
#initialize(name, type: nil, desc: nil, default: nil, placeholder: nil) ⇒ Field
constructor
A new instance of Field.
- #list? ⇒ Boolean
- #list_with_emtpy_default? ⇒ Boolean
-
#load_values(path_to_csv) ⇒ Object
Adds values to a Field instance that are defined in a CSV file.
- #plat? ⇒ Boolean
- #plat_field? ⇒ Boolean
- #plat_list? ⇒ Boolean
- #raw_values ⇒ Object
-
#value(val) ⇒ Object
Adds a value to a Field instance.
Methods included from Marshaller
Constructor Details
#initialize(name, type: nil, desc: nil, default: nil, placeholder: nil) ⇒ Field
Returns a new instance of Field.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/field_mapper/standard/field.rb', line 19 def initialize( name, type: nil, desc: nil, default: nil, placeholder: nil ) raise TypeNotSpecified.new("type not specified for: #{name}") if type.nil? @name = name.to_sym @type = type @desc= desc @default = default @placeholder = placeholder end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
9 10 11 |
# File 'lib/field_mapper/standard/field.rb', line 9 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
9 10 11 |
# File 'lib/field_mapper/standard/field.rb', line 9 def desc @desc end |
#name ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute name.
9 10 11 |
# File 'lib/field_mapper/standard/field.rb', line 9 def name @name end |
#placeholder ⇒ Object
Returns the value of attribute placeholder.
17 18 19 |
# File 'lib/field_mapper/standard/field.rb', line 17 def placeholder @placeholder end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/field_mapper/standard/field.rb', line 9 def type @type end |
Instance Method Details
#cast(value, as_single_value: false) ⇒ Object
105 106 107 108 109 110 |
# File 'lib/field_mapper/standard/field.rb', line 105 def cast(value, as_single_value: false) value = cast_value(type, value, as_single_value: as_single_value) return nil if value.nil? || value.to_s.blank? value = clean_value(value) unless as_single_value value end |
#find_value(value) ⇒ Object
100 101 102 103 |
# File 'lib/field_mapper/standard/field.rb', line 100 def find_value(value) return nil unless has_values? values.find { |val| val == value || val.value == value } end |
#has_values? ⇒ Boolean
96 97 98 |
# File 'lib/field_mapper/standard/field.rb', line 96 def has_values? !values.nil? end |
#list? ⇒ Boolean
34 35 36 |
# File 'lib/field_mapper/standard/field.rb', line 34 def list? type.name == "FieldMapper::Types::List" end |
#list_with_emtpy_default? ⇒ Boolean
38 39 40 |
# File 'lib/field_mapper/standard/field.rb', line 38 def list_with_emtpy_default? list? && default == [] 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 single column with a header row.
90 91 92 93 94 |
# File 'lib/field_mapper/standard/field.rb', line 90 def load_values(path_to_csv) CSV.foreach(path_to_csv, :headers => true) do |row| value row["standard_value"].to_s.strip end end |
#plat? ⇒ Boolean
42 43 44 |
# File 'lib/field_mapper/standard/field.rb', line 42 def plat? type.name == "FieldMapper::Types::Plat" end |
#plat_field? ⇒ Boolean
46 47 48 |
# File 'lib/field_mapper/standard/field.rb', line 46 def plat_field? plat? || plat_list? end |
#plat_list? ⇒ Boolean
50 51 52 |
# File 'lib/field_mapper/standard/field.rb', line 50 def plat_list? list? && type.plat_list? end |
#raw_values ⇒ Object
54 55 56 57 |
# File 'lib/field_mapper/standard/field.rb', line 54 def raw_values return nil unless has_values? values.map { |v| v.value } end |
#value(val) ⇒ Object
Adds a value to a Field instance. Intended use is from within a Plat class declaration.
68 69 70 71 72 |
# File 'lib/field_mapper/standard/field.rb', line 68 def value(val) @values ||= [] @values << FieldMapper::Standard::Value.new(val, field: self) @values.last end |