Class: SDL::Model

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, fields: [], **options, &block) ⇒ Model

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 Model.


18
19
20
21
22
23
# File 'lib/sdl/model.rb', line 18

def initialize(name, fields: [], **options, &block)
  @name = Name.new(name.to_s)
  @fields = fields
  @options = options
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#nameName (readonly)

Name of the model

Returns:


11
12
13
# File 'lib/sdl/model.rb', line 11

def name
  @name
end

#optionsHash (readonly)

Any additional options

Returns:

  • (Hash)

15
16
17
# File 'lib/sdl/model.rb', line 15

def options
  @options
end

Instance Method Details

#association_fieldsArray<Association>

Deprecated.

Use #fields instead

Get all Association fields

Returns:


167
168
169
# File 'lib/sdl/model.rb', line 167

def association_fields
  fields(:association)
end

#attachment_fieldsArray<Attachment>

Deprecated.

Use #fields instead

Get all Attachment fields

Returns:


174
175
176
# File 'lib/sdl/model.rb', line 174

def attachment_fields
  fields(:attachment)
end

#attribute(name, type, **options) ⇒ Object

Adds an Attribute to the model

Examples:

model :user do
  attribute :name, :string, required: true
end

Parameters:

  • name (Symbol)
  • type (Symbol)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :nullable (Boolean)
  • :unique (Boolean)
  • :default (Object)
  • :limit (Integer)
  • :precision (Integer)
  • :scale (Integer)

63
64
65
# File 'lib/sdl/model.rb', line 63

def attribute(name, type, **options)
  @fields << Attribute.new(*name, type, **options)
end

#attribute_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields

Returns:


160
161
162
# File 'lib/sdl/model.rb', line 160

def attribute_fields
  fields(:attribute)
end

#belongs_to(name, **options) ⇒ Object

Adds an Association::BelongsTo to the model

Examples:

model :user do
  belongs_to :organization, required: true
end

Parameters:

  • name (Symbol)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :model_name (Symbol)
  • :nullable (Boolean)
  • :unique (Boolean)
  • :foreign_key (Boolean)

93
94
95
# File 'lib/sdl/model.rb', line 93

def belongs_to(name, **options)
  @fields << Association::BelongsTo.new(name, **options)
end

#belongs_to_fieldsArray<Association::BelongsTo>

Deprecated.

Use #fields instead

Get all Association::BelongsTo fields

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#binary_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :binary

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#boolean_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :boolean

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#date_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :date

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#datetime_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :datetime

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#decimal_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :decimal

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#enum(name, **options) ⇒ Object

Adds an Enum to the model

Examples:

model :user do
  enum :status, values: [:accepted, :rejected], required: true
end

Parameters:

  • name (Symbol)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :values (Array<Symbol>)
  • :nullable (Boolean)
  • :unique (Boolean)
  • :default (Object)

78
79
80
# File 'lib/sdl/model.rb', line 78

def enum(name, **options)
  @fields << Enum.new(name, **options)
end

#enum_fieldsArray<Enum>

Deprecated.

Use #fields instead

Get all Enum fields

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#fields(*types) ⇒ Array<Field>

List or filter fields that have been registered

Examples:

List all fields

model.fields.each { |field| puts field.name }

Filter fields

model.fields(:integer).each { |field| puts field.name }

Parameters:

  • types (Array<Symbol>)

    types to filter

Returns:


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sdl/model.rb', line 32

def fields(*types)
  return @fields if types.empty?

  types.flat_map { |type|
    case type
    when :attribute
      @fields.grep Attribute
    when :association
      @fields.grep Association
    when :attachment
      @fields.grep Attachment
    else
      @fields.select { |field| field.type == type }
    end
  }
end

#float_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :float

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#has_many(name, **options) ⇒ Object

Adds an Association::HasMany to the model

Examples:

model :user do
  has_many :devices
end

Parameters:

  • name (Symbol)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :model_name (Symbol)

118
119
120
# File 'lib/sdl/model.rb', line 118

def has_many(name, **options)
  @fields << Association::HasMany.new(name, **options)
end

#has_many_attached(name, **options) ⇒ Object

Adds an Attachment::HasMany to the model

Examples:

model :product do
  has_many_attached :images
end

Parameters:

  • name (Symbol)
  • options (Hash)

142
143
144
# File 'lib/sdl/model.rb', line 142

def has_many_attached(name, **options)
  @fields << Attachment::HasMany.new(name, **options)
end

#has_many_attached_fieldsArray<Attachment::HasMany>

Deprecated.

Use #fields instead

Get all Attachment::HasMany fields

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#has_many_fieldsArray<Association::HasMany>

Deprecated.

Use #fields instead

Get all Association::HasMany fields

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#has_one(name, **options) ⇒ Object

Adds an Association::HasOne to the model

Examples:

model :user do
  has_one :profile
end

Parameters:

  • name (Symbol)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :model_name (Symbol)
  • :nullable (Boolean)

106
107
108
# File 'lib/sdl/model.rb', line 106

def has_one(name, **options)
  @fields << Association::HasOne.new(name, **options)
end

#has_one_attached(name, **options) ⇒ Object

Adds an Attachment::HasOne to the model

Examples:

model :user do
  has_one_attached :avatar
end

Parameters:

  • name (Symbol)
  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :nullable (Symbol)

130
131
132
# File 'lib/sdl/model.rb', line 130

def has_one_attached(name, **options)
  @fields << Attachment::HasOne.new(name, **options)
end

#has_one_attached_fieldsArray<Attachment::HasOne>

Deprecated.

Use #fields instead

Get all Attachment::HasOne fields

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#has_one_fieldsArray<Assocation::HasOne>

Deprecated.

Use #fields instead

Get all Association::HasOne fields

Returns:

  • (Array<Assocation::HasOne>)

242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#id_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :id

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#integer_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :integer

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#string_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :string

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#text_fieldsArray<Attribute>

Deprecated.

Use #fields instead

Get all Attribute fields whose type is :text

Returns:


242
243
244
# File 'lib/sdl/model.rb', line 242

TYPES.each do |type|
  define_method("#{type}_fields") { fields(type) }
end

#timestampsObject

Adds attributes for +:created_at+ and +:updated_at+ to the model

Examples:

model :user do
  timestamps
end

152
153
154
155
# File 'lib/sdl/model.rb', line 152

def timestamps
  attribute :created_at, :datetime, required: true
  attribute :updated_at, :datetime, required: true
end