Class: FieldMapper::Standard::Field

Inherits:
Object
  • Object
show all
Includes:
Marshaller
Defined in:
lib/field_mapper/standard/field.rb

Direct Known Subclasses

Custom::Field

Constant Summary

Constants included from Marshaller

Marshaller::OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Marshaller

#marshal, #unmarshal

Constructor Details

#initialize(name, type: nil, desc: nil, default: nil, placeholder: nil) ⇒ Field

Returns a new instance of Field.

Raises:



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

#defaultObject (readonly)

Returns the value of attribute default.



9
10
11
# File 'lib/field_mapper/standard/field.rb', line 9

def default
  @default
end

#descObject (readonly)

Returns the value of attribute desc.



9
10
11
# File 'lib/field_mapper/standard/field.rb', line 9

def desc
  @desc
end

#nameObject (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

#placeholderObject

Returns the value of attribute placeholder.



17
18
19
# File 'lib/field_mapper/standard/field.rb', line 17

def placeholder
  @placeholder
end

#typeObject (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

Returns:

  • (Boolean)


96
97
98
# File 'lib/field_mapper/standard/field.rb', line 96

def has_values?
  !values.nil?
end

#list?Boolean

Returns:

  • (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

Returns:

  • (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.

Examples:

class ExamplePlat < FieldMapper::Standard::Plat
  field :example do
    load_values "/path/to/file.csv"
  end
end
Name of Field
1
2


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

Returns:

  • (Boolean)


42
43
44
# File 'lib/field_mapper/standard/field.rb', line 42

def plat?
  type.name == "FieldMapper::Types::Plat"
end

#plat_field?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/field_mapper/standard/field.rb', line 46

def plat_field?
  plat? || plat_list?
end

#plat_list?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/field_mapper/standard/field.rb', line 50

def plat_list?
  list? && type.plat_list?
end

#raw_valuesObject



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.

Examples:

class ExamplePlat < FieldMapper::Standard::Plat
  field :example do
    value 1
  end
end


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