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) ⇒ Field

Returns a new instance of Field.

Raises:



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/field_mapper/standard/field.rb', line 18

def initialize(
  name,
  type: nil,
  desc: nil,
  default: nil
)
  raise TypeNotSpecified.new("type not specified for: #{name}") if type.nil?
  @name = name.to_sym
  @type = type
  @desc= desc
  @default = default
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

#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

#values ⇒ Object (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#cast(value, as_single_value: false) ⇒ Object



98
99
100
101
102
103
# File 'lib/field_mapper/standard/field.rb', line 98

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



93
94
95
96
# File 'lib/field_mapper/standard/field.rb', line 93

def find_value(value)
  return nil unless has_values?
  values.find { |val| val == value || val.value == value }
end

#has_values? ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/field_mapper/standard/field.rb', line 89

def has_values?
  !values.nil?
end

#list? ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/field_mapper/standard/field.rb', line 31

def list?
  type.name == "FieldMapper::Types::List"
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


83
84
85
86
87
# File 'lib/field_mapper/standard/field.rb', line 83

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)


35
36
37
# File 'lib/field_mapper/standard/field.rb', line 35

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

#plat_field? ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/field_mapper/standard/field.rb', line 39

def plat_field?
  plat? || plat_list?
end

#plat_list? ⇒ Boolean

Returns:

  • (Boolean)


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

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

#raw_values ⇒ Object



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

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


61
62
63
64
65
# File 'lib/field_mapper/standard/field.rb', line 61

def value(val)
  @values ||= []
  @values << FieldMapper::Standard::Value.new(val, field: self)
  @values.last
end