Class: ActiveRecord::TypedStore::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/typed_store/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Field.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/active_record/typed_store/field.rb', line 7

def initialize(name, type, options={})
  type_options = options.slice(:scale, :limit, :precision)
  @type = lookup_type(type, type_options)
  @type_sym = type

  @accessor = options.fetch(:accessor, true)
  @name = name
  if options.key?(:default)
    @default = extract_default(options[:default])
  end
  @null = options.fetch(:null, true)
  @blank = options.fetch(:blank, true)
  @array = options.fetch(:array, false)
end

Instance Attribute Details

#accessorObject (readonly)

Returns the value of attribute accessor.



5
6
7
# File 'lib/active_record/typed_store/field.rb', line 5

def accessor
  @accessor
end

#arrayObject (readonly)

Returns the value of attribute array.



5
6
7
# File 'lib/active_record/typed_store/field.rb', line 5

def array
  @array
end

#blankObject (readonly)

Returns the value of attribute blank.



5
6
7
# File 'lib/active_record/typed_store/field.rb', line 5

def blank
  @blank
end

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/active_record/typed_store/field.rb', line 5

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/active_record/typed_store/field.rb', line 5

def name
  @name
end

#nullObject (readonly)

Returns the value of attribute null.



5
6
7
# File 'lib/active_record/typed_store/field.rb', line 5

def null
  @null
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/active_record/typed_store/field.rb', line 5

def type
  @type
end

#type_symObject (readonly)

Returns the value of attribute type_sym.



5
6
7
# File 'lib/active_record/typed_store/field.rb', line 5

def type_sym
  @type_sym
end

Instance Method Details

#cast(value) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/active_record/typed_store/field.rb', line 26

def cast(value)
  casted_value = type_cast(value)
  if !blank
    casted_value = default if casted_value.blank?
  elsif !null
    casted_value = default if casted_value.nil?
  end
  casted_value
end

#has_default?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/active_record/typed_store/field.rb', line 22

def has_default?
  defined?(@default)
end