Class: ActiveRecord::Embedded::Field Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/embedded/field.rb,
lib/active_record/embedded/field/hash.rb,
lib/active_record/embedded/field/time.rb,
lib/active_record/embedded/field/array.rb,
lib/active_record/embedded/field/float.rb,
lib/active_record/embedded/field/regexp.rb,
lib/active_record/embedded/field/string.rb,
lib/active_record/embedded/field/boolean.rb,
lib/active_record/embedded/field/integer.rb,
lib/active_record/embedded/field/type_error.rb,
lib/active_record/embedded/field/not_defined_error.rb

Overview

This class is abstract.

Subclass to define a custom field.

Base class for supported field types. Holds the logic for casting values and reading/writing attributes from the model.

Direct Known Subclasses

Array, Boolean, Float, Hash, Integer, Regexp, String, Time

Defined Under Namespace

Classes: Array, Boolean, Float, Hash, Integer, NotDefinedError, Regexp, String, Time, TypeError

Constant Summary collapse

PREFIX =
'ActiveRecord::Embedded::Field::'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, default) ⇒ Field


14
15
16
17
18
19
20
21
# File 'lib/active_record/embedded/field.rb', line 14

def initialize(name, default)
  @name = name
  @default = if default.respond_to? :call
               default
             else
               -> { default }
             end
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default


12
13
14
# File 'lib/active_record/embedded/field.rb', line 12

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name


12
13
14
# File 'lib/active_record/embedded/field.rb', line 12

def name
  @name
end

Class Method Details

.find(type) ⇒ ActiveRecord::Embedded::Field

Find a field object by its given type name.


43
44
45
46
47
# File 'lib/active_record/embedded/field.rb', line 43

def self.find(type)
  subclasses.find do |field|
    field.name.gsub(PREFIX, '') == type.to_s.gsub(PREFIX, '')
  end || raise(TypeError, type)
end

.typesArray<String>

All type names, which are subclasses of this object.


36
37
38
# File 'lib/active_record/embedded/field.rb', line 36

def self.types
  subclasses.map { |field| field.name.gsub(PREFIX, '') }
end

Instance Method Details

#cast(value) ⇒ Object

This method is abstract.

Override this method to implement typecasting behavior.

Cast a given value to this type. Short-circuits when value passed in is nil

Raises:

  • (NotImplementedError)

56
57
58
# File 'lib/active_record/embedded/field.rb', line 56

def cast(value)
  raise NotImplementedError, "#{self.class.name}#cast"
end

#coerce(value) ⇒ Object

Attempt to #cast this value unless it's nil.


61
62
63
64
65
# File 'lib/active_record/embedded/field.rb', line 61

def coerce(value)
  return if value.nil?

  cast(value)
end

#default?Boolean

Whether a default has been set


24
25
26
# File 'lib/active_record/embedded/field.rb', line 24

def default?
  !@default.nil?
end

#default_method_nameObject

Name of the method holding the default value.


29
30
31
# File 'lib/active_record/embedded/field.rb', line 29

def default_method_name
  "__#{name}_default__"
end