class SDL::Model
Attributes
Name
of the model @return [Name]
Any additional options @return [Hash]
Public Class Methods
@api private
# 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
Public Instance Methods
Get all {Association} fields @deprecated Use {#fields} instead @return [Array<Association>]
# File lib/sdl/model.rb, line 167 def association_fields fields(:association) end
Get all {Attachment} fields @deprecated Use {#fields} instead @return [Array<Attachment>]
# File lib/sdl/model.rb, line 174 def attachment_fields fields(:attachment) end
Adds an {Attribute} to the model @param name [Symbol] @param type [Symbol] @option options [Boolean] :nullable @option options [Boolean] :unique @option options [Object] :default @option options [Integer] :limit @option options [Integer] :precision @option options [Integer] :scale
@example
model :user do attribute :name, :string, required: true end
# File lib/sdl/model.rb, line 63 def attribute(name, type, **options) @fields << Attribute.new(*name, type, **options) end
Get all {Attribute} fields @deprecated Use {#fields} instead @return [Array<Attribute>]
# File lib/sdl/model.rb, line 160 def attribute_fields fields(:attribute) end
Adds an {Association::BelongsTo} to the model @param name [Symbol] @option options [Symbol] :model_name @option options [Boolean] :nullable @option options [Boolean] :unique @option options [Boolean] :foreign_key
@example
model :user do belongs_to :organization, required: true end
# File lib/sdl/model.rb, line 93 def belongs_to(name, **options) @fields << Association::BelongsTo.new(name, **options) end
Adds an {Enum} to the model @param name [Symbol] @option options [Array<Symbol>] :values @option options [Boolean] :nullable @option options [Boolean] :unique @option options [Object] :default
@example
model :user do enum :status, values: [:accepted, :rejected], required: true end
# File lib/sdl/model.rb, line 78 def enum(name, **options) @fields << Enum.new(name, **options) end
List or filter fields that have been registered @param types [Array<Symbol>] types to filter @return [Array<Field>] @example List all fields
model.fields.each { |field| puts field.name }
@example Filter fields
model.fields(:integer).each { |field| puts field.name }
# 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
Adds an {Association::HasMany} to the model @param name [Symbol] @option options [Symbol] :model_name
@example
model :user do has_many :devices end
# File lib/sdl/model.rb, line 118 def has_many(name, **options) @fields << Association::HasMany.new(name, **options) end
Adds an {Attachment::HasMany} to the model @param name [Symbol] @param options [Hash]
@example
model :product do has_many_attached :images end
# File lib/sdl/model.rb, line 142 def has_many_attached(name, **options) @fields << Attachment::HasMany.new(name, **options) end
Adds an {Association::HasOne} to the model @param name [Symbol] @option options [Symbol] :model_name @option options [Boolean] :nullable
@example
model :user do has_one :profile end
# File lib/sdl/model.rb, line 106 def has_one(name, **options) @fields << Association::HasOne.new(name, **options) end
Adds an {Attachment::HasOne} to the model @param name [Symbol] @option options [Symbol] :nullable
@example
model :user do has_one_attached :avatar end
# File lib/sdl/model.rb, line 130 def has_one_attached(name, **options) @fields << Attachment::HasOne.new(name, **options) end
Adds attributes for :created_at
and :updated_at
to the model
@example
model :user do timestamps end
# File lib/sdl/model.rb, line 152 def timestamps attribute :created_at, :datetime, required: true attribute :updated_at, :datetime, required: true end