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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/field_mapper/standard/field.rb', line 20 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.
10 11 12 |
# File 'lib/field_mapper/standard/field.rb', line 10 def default @default end |
#desc ⇒ Object (readonly)
Returns the value of attribute desc.
10 11 12 |
# File 'lib/field_mapper/standard/field.rb', line 10 def desc @desc end |
#name ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute name.
10 11 12 |
# File 'lib/field_mapper/standard/field.rb', line 10 def name @name end |
#placeholder ⇒ Object
Returns the value of attribute placeholder.
18 19 20 |
# File 'lib/field_mapper/standard/field.rb', line 18 def placeholder @placeholder end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
10 11 12 |
# File 'lib/field_mapper/standard/field.rb', line 10 def type @type end |
Instance Method Details
#cast(value, as_single_value: false) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/field_mapper/standard/field.rb', line 106 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
101 102 103 104 |
# File 'lib/field_mapper/standard/field.rb', line 101 def find_value(value) return nil unless has_values? values.find { |val| val == value || val.value == value } end |
#has_values? ⇒ Boolean
97 98 99 |
# File 'lib/field_mapper/standard/field.rb', line 97 def has_values? !values.nil? end |
#list? ⇒ Boolean
35 36 37 |
# File 'lib/field_mapper/standard/field.rb', line 35 def list? type.name == "FieldMapper::Types::List" end |
#list_with_emtpy_default? ⇒ Boolean
39 40 41 |
# File 'lib/field_mapper/standard/field.rb', line 39 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.
91 92 93 94 95 |
# File 'lib/field_mapper/standard/field.rb', line 91 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
43 44 45 |
# File 'lib/field_mapper/standard/field.rb', line 43 def plat? type.name == "FieldMapper::Types::Plat" end |
#plat_field? ⇒ Boolean
47 48 49 |
# File 'lib/field_mapper/standard/field.rb', line 47 def plat_field? plat? || plat_list? end |
#plat_list? ⇒ Boolean
51 52 53 |
# File 'lib/field_mapper/standard/field.rb', line 51 def plat_list? list? && type.plat_list? end |
#raw_values ⇒ Object
55 56 57 58 |
# File 'lib/field_mapper/standard/field.rb', line 55 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.
69 70 71 72 73 |
# File 'lib/field_mapper/standard/field.rb', line 69 def value(val) @values ||= [] @values << FieldMapper::Standard::Value.new(val, field: self) @values.last end |