Class: SDL::Field Abstract

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

Overview

This class is abstract.

A base class for all fields of a model

Direct Known Subclasses

Association, Attachment, Attribute, Enum

Defined Under Namespace

Modules: ColumnOptions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, **options) ⇒ Field

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Field.


17
18
19
20
# File 'lib/sdl/field.rb', line 17

def initialize(name, **options)
  @name = Name.new(name.to_s)
  @options = options
end

Instance Attribute Details

#nameName (readonly)

The name of the field

Returns:


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

def name
  @name
end

#optionsHash (readonly)

All options that were passed to the field

Returns:

  • (Hash)

14
15
16
# File 'lib/sdl/field.rb', line 14

def options
  @options
end

Instance Method Details

#association?Boolean

Indicates that this is an Association

Returns:

  • (Boolean)

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

def association?
  has_one? || has_many? || belongs_to?
end

#attachment?Boolean

Indicates that this is an Attachment

Returns:

  • (Boolean)

30
31
32
# File 'lib/sdl/field.rb', line 30

def attachment?
  has_one_attached? || has_many_attached?
end

#attribute?Boolean

Indicates that this is an Attribute

Returns:

  • (Boolean)

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

def attribute?
  !enum? && !association? && !attachment?
end

#belongs_to?Boolean

Indicates that this is an Association::BelongsTo

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#binary?Boolean

Indicates that this is an Attribute whose type is :binary

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#boolean?Boolean

Indicates that this is an Attribute whose type is :boolean

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#date?Boolean

Indicates that this is an Attribute whose type is :date

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#datetime?Boolean

Indicates that this is an Attribute whose type is :datetime

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#decimal?Boolean

Indicates that this is an Attribute whose type is :decimal

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#enum?Boolean

Indicates that this is an Enum

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#float?Boolean

Indicates that this is an Attribute whose type is :float

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#has_many?Boolean

Indicates that this is an Association::HasMany

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#has_many_attached?Boolean

Indicates that this is an Attachment::HasMany

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#has_one?Boolean

Indicates that this is an Association::HasOne

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#has_one_attached?Boolean

Indicates that this is an Attachment::HasOne

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#id?Boolean

Indicates that this is an Attribute whose type is :id

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#integer?Boolean

Indicates that this is an Attribute whose type is :integer

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#nullable?Boolean

Can this field be null? By default, this is false. But, it can be overridden by passing nullable: true to a field.

Returns:

  • (Boolean)

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

def nullable?
  options.fetch(:nullable, false)
end

#required?Boolean

The opposite of #nullable?. All fields are required by default

Returns:

  • (Boolean)

49
50
51
# File 'lib/sdl/field.rb', line 49

def required?
  !nullable?
end

#string?Boolean

Indicates that this is an Attribute whose type is :string

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#text?Boolean

Indicates that this is an Attribute whose type is :text

Returns:

  • (Boolean)

114
115
116
117
118
# File 'lib/sdl/field.rb', line 114

TYPES.each do |meth|
  define_method "#{meth}?" do
    type == meth
  end
end

#typeSymbol

This method is abstract.

The type of the field

Returns:

  • (Symbol)

Raises:

  • (NotImplementedError)

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

def type
  raise NotImplementedError, __method__
end

#type_nameName

The name of the type

Returns:


62
63
64
# File 'lib/sdl/field.rb', line 62

def type_name
  Name.new(type.to_s)
end