Class: SDL::Model
- Inherits:
-
Object
- Object
- SDL::Model
- Defined in:
- lib/sdl/model.rb
Instance Attribute Summary collapse
-
#name ⇒ Name
readonly
Name of the model.
-
#options ⇒ Hash
readonly
Any additional options.
Instance Method Summary collapse
-
#association_fields ⇒ Array<Association>
deprecated
Deprecated.
Use #fields instead
-
#attachment_fields ⇒ Array<Attachment>
deprecated
Deprecated.
Use #fields instead
-
#attribute(name, type, **options) ⇒ Object
Adds an Attribute to the model.
-
#attribute_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#belongs_to(name, **options) ⇒ Object
Adds an Association::BelongsTo to the model.
-
#belongs_to_fields ⇒ Array<Association::BelongsTo>
deprecated
Deprecated.
Use #fields instead
-
#binary_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#boolean_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#date_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#datetime_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#decimal_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#enum(name, **options) ⇒ Object
Adds an Enum to the model.
-
#enum_fields ⇒ Array<Enum>
deprecated
Deprecated.
Use #fields instead
-
#fields(*types) ⇒ Array<Field>
List or filter fields that have been registered.
-
#float_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#has_many(name, **options) ⇒ Object
Adds an Association::HasMany to the model.
-
#has_many_attached(name, **options) ⇒ Object
Adds an Attachment::HasMany to the model.
-
#has_many_attached_fields ⇒ Array<Attachment::HasMany>
deprecated
Deprecated.
Use #fields instead
-
#has_many_fields ⇒ Array<Association::HasMany>
deprecated
Deprecated.
Use #fields instead
-
#has_one(name, **options) ⇒ Object
Adds an Association::HasOne to the model.
-
#has_one_attached(name, **options) ⇒ Object
Adds an Attachment::HasOne to the model.
-
#has_one_attached_fields ⇒ Array<Attachment::HasOne>
deprecated
Deprecated.
Use #fields instead
-
#has_one_fields ⇒ Array<Assocation::HasOne>
deprecated
Deprecated.
Use #fields instead
-
#id_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#initialize(name, fields: [], **options, &block) ⇒ Model
constructor
private
A new instance of Model.
-
#integer_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#string_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#text_fields ⇒ Array<Attribute>
deprecated
Deprecated.
Use #fields instead
-
#timestamps ⇒ Object
Adds attributes for +:created_at+ and +:updated_at+ to the model.
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: [], **, &block) @name = Name.new(name.to_s) @fields = fields @options = instance_eval(&block) if block_given? end |
Instance Attribute Details
#name ⇒ Name (readonly)
Name of the model
11 12 13 |
# File 'lib/sdl/model.rb', line 11 def name @name end |
#options ⇒ Hash (readonly)
Any additional options
15 16 17 |
# File 'lib/sdl/model.rb', line 15 def @options end |
Instance Method Details
#association_fields ⇒ Array<Association>
Use #fields instead
Get all Association fields
167 168 169 |
# File 'lib/sdl/model.rb', line 167 def association_fields fields(:association) end |
#attachment_fields ⇒ Array<Attachment>
Use #fields instead
Get all Attachment fields
174 175 176 |
# File 'lib/sdl/model.rb', line 174 def fields(:attachment) end |
#attribute(name, type, **options) ⇒ Object
Adds an Attribute to the model
63 64 65 |
# File 'lib/sdl/model.rb', line 63 def attribute(name, type, **) @fields << Attribute.new(*name, type, **) end |
#attribute_fields ⇒ Array<Attribute>
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
93 94 95 |
# File 'lib/sdl/model.rb', line 93 def belongs_to(name, **) @fields << Association::BelongsTo.new(name, **) end |
#belongs_to_fields ⇒ Array<Association::BelongsTo>
Use #fields instead
Get all Association::BelongsTo fields
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#binary_fields ⇒ Array<Attribute>
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#boolean_fields ⇒ Array<Attribute>
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#date_fields ⇒ Array<Attribute>
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#datetime_fields ⇒ Array<Attribute>
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#decimal_fields ⇒ Array<Attribute>
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
78 79 80 |
# File 'lib/sdl/model.rb', line 78 def enum(name, **) @fields << Enum.new(name, **) end |
#enum_fields ⇒ Array<Enum>
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
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_fields ⇒ Array<Attribute>
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
118 119 120 |
# File 'lib/sdl/model.rb', line 118 def has_many(name, **) @fields << Association::HasMany.new(name, **) end |
#has_many_attached(name, **options) ⇒ Object
Adds an Attachment::HasMany to the model
142 143 144 |
# File 'lib/sdl/model.rb', line 142 def has_many_attached(name, **) @fields << Attachment::HasMany.new(name, **) end |
#has_many_attached_fields ⇒ Array<Attachment::HasMany>
Use #fields instead
Get all Attachment::HasMany fields
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#has_many_fields ⇒ Array<Association::HasMany>
Use #fields instead
Get all Association::HasMany fields
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
106 107 108 |
# File 'lib/sdl/model.rb', line 106 def has_one(name, **) @fields << Association::HasOne.new(name, **) end |
#has_one_attached(name, **options) ⇒ Object
Adds an Attachment::HasOne to the model
130 131 132 |
# File 'lib/sdl/model.rb', line 130 def has_one_attached(name, **) @fields << Attachment::HasOne.new(name, **) end |
#has_one_attached_fields ⇒ Array<Attachment::HasOne>
Use #fields instead
Get all Attachment::HasOne fields
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#has_one_fields ⇒ Array<Assocation::HasOne>
Use #fields instead
Get all Association::HasOne fields
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#id_fields ⇒ Array<Attribute>
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#integer_fields ⇒ Array<Attribute>
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#string_fields ⇒ Array<Attribute>
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#text_fields ⇒ Array<Attribute>
242 243 244 |
# File 'lib/sdl/model.rb', line 242 TYPES.each do |type| define_method("#{type}_fields") { fields(type) } end |
#timestamps ⇒ Object
Adds attributes for +:created_at+ and +:updated_at+ to the model
152 153 154 155 |
# File 'lib/sdl/model.rb', line 152 def attribute :created_at, :datetime, required: true attribute :updated_at, :datetime, required: true end |