Class: Field

Inherits:
Object
  • Object
show all
Defined in:
lib/yodel/models/core/fields/field.rb

Constant Summary collapse

TYPES =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Field

options is a hash of string keys to values as described. Type is the only required option. All others are optional.

type => field type default => value (value for newly created records) display => true/false searchable => true/false protected => true/false (unable to mass assign to this field if true) section => string (nil or a section name used in admin)

Some field types may have other options, such as embedded fields.



24
25
26
27
# File 'lib/yodel/models/core/fields/field.rb', line 24

def initialize(name, options={})
  @name = name
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



69
70
71
# File 'lib/yodel/models/core/fields/field.rb', line 69

def method_missing(name, *args, &block)
  @options[name.to_s]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/yodel/models/core/fields/field.rb', line 2

def name
  @name
end

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/yodel/models/core/fields/field.rb', line 2

def options
  @options
end

Class Method Details

.field_from_type(type) ⇒ Object



9
10
11
# File 'lib/yodel/models/core/fields/field.rb', line 9

def self.field_from_type(type)
  TYPES[type]
end

.from_options(name, options) ⇒ Object



5
6
7
# File 'lib/yodel/models/core/fields/field.rb', line 5

def self.from_options(name, options)
  field_from_type(options['type']).new(name, options)
end

Instance Method Details

#default_input_typeObject



82
83
84
# File 'lib/yodel/models/core/fields/field.rb', line 82

def default_input_type
  :text
end

#display?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/yodel/models/core/fields/field.rb', line 33

def display?
  @options['display'] != false
end

#from_json(value, record) ⇒ Object

Take a raw JSON value and encode it in an untypecast (raw) value ready for storage in a mongo record. Non mongo records can still use this method since it performs complex->simple type conversion.



109
110
111
# File 'lib/yodel/models/core/fields/field.rb', line 109

def from_json(value, record)
  value
end

#include_in_search_keywords?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/yodel/models/core/fields/field.rb', line 65

def include_in_search_keywords?
  @options.key?('include_in_search_keywords')
end

#index?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/yodel/models/core/fields/field.rb', line 53

def index?
  @options['index'] == true
end

#inherited?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/yodel/models/core/fields/field.rb', line 57

def inherited?
  @options['inherited'] == true
end

#numeric?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/yodel/models/core/fields/field.rb', line 61

def numeric?
  false
end

#required?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/yodel/models/core/fields/field.rb', line 41

def required?
  @options['required'] == true
end

#searchable?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/yodel/models/core/fields/field.rb', line 37

def searchable?
  @options['searchable'] != false
end

#strip_nil?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/yodel/models/core/fields/field.rb', line 49

def strip_nil?
  @options['strip_nil'] == true
end

#to_json(*a) ⇒ Object

JSON converter for the field object itself; this method does not encode a field’s value. Fields convert values to and from mongo (untypecast/typecast) and from json (from_json). When converting a record to JSON, the raw (untypecast) value is used. If this value needs special encoding for JSON, specify a to_json(*a) method on the class the value is an instance of. For instance, Time fields store values as Time objects in mongo. For conversion to JSON, the Time class is extended with a to_json(*a) method to perform the necessary conversion. This is done so fields can be ignored by the JSON converter, and a simple conversion done between untypecast values and JSON. Since mongo stores values in BSON (which is similar to JSON), this makes conversion much faster.



125
126
127
# File 'lib/yodel/models/core/fields/field.rb', line 125

def to_json(*a)
  @options.to_json(*a)
end

#to_strObject



29
30
31
# File 'lib/yodel/models/core/fields/field.rb', line 29

def to_str
  "#<#{self.class.name} #{name}>"
end

#typecast(value, record) ⇒ Object

Convert from an untypecast (raw) representation of a value to a more complex version of the same value. For instance, BigDecimals are stored as strings, but are BigDecimal objects when typecast.



89
90
91
# File 'lib/yodel/models/core/fields/field.rb', line 89

def typecast(value, record)
  value
end

#unique?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/yodel/models/core/fields/field.rb', line 45

def unique?
  @options['unique'] == true
end

#untypecast(value, record) ⇒ Object

Convert from a complex (typecast) representation of a value to a simpler version of the same value. For instance, BigDecimals are BigDecimal objects when typecast, but are strings when untypecast.



96
97
98
# File 'lib/yodel/models/core/fields/field.rb', line 96

def untypecast(value, record)
  value
end

#validate(record, errors) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/yodel/models/core/fields/field.rb', line 73

def validate(record, errors)
  return if @options['validations'].blank?
  value = record.get(name)
  field_name = name.humanize
  @options['validations'].each do |type, params|
    Validation.validate(type, params, self, field_name, value, record, errors)
  end
end