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:



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

#defaultObject (readonly)

Returns the value of attribute default.



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

def default
  @default
end

#descObject (readonly)

Returns the value of attribute desc.



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

def desc
  @desc
end

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

#placeholderObject

Returns the value of attribute placeholder.



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

def placeholder
  @placeholder
end

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

Returns:

  • (Boolean)


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

def has_values?
  !values.nil?
end

#list?Boolean

Returns:

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

Returns:

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

Examples:

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


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

Returns:

  • (Boolean)


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

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

#plat_field?Boolean

Returns:

  • (Boolean)


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

def plat_field?
  plat? || plat_list?
end

#plat_list?Boolean

Returns:

  • (Boolean)


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

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

#raw_valuesObject



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.

Examples:

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


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