Module: Entitainer::ClassMethods
- Defined in:
- lib/entitainer.rb
Overview
Methods for schema definition.
Instance Method Summary collapse
-
#attributes(*list) ⇒ Object
Used in schema block to define attributes available for the entity.
-
#available_attributes ⇒ Object
A collection of all available attributes defined in a schema block,
id
attribute, and_id
sufixed attribute for eachbelongs_to
relation. - #available_belongs_tos ⇒ Object
- #available_has_manys ⇒ Object
-
#belongs_to(*list) ⇒ Object
Used in schema block to define belongs-to type of relation.
-
#has_many(*list) ⇒ Object
Used in schema block to define has-many type of relation.
-
#schema ⇒ Object
Defines entity attributes and relations.
Instance Method Details
#attributes(*list) ⇒ Object
Used in schema block to define attributes available for the entity.
69 70 71 |
# File 'lib/entitainer.rb', line 69 def attributes(*list) list.each { |attr| @available_attributes << attr } end |
#available_attributes ⇒ Object
A collection of all available attributes defined in a schema block, id
attribute, and _id
sufixed attribute for each belongs_to
relation.
27 28 29 |
# File 'lib/entitainer.rb', line 27 def available_attributes @available_attributes ||= [] end |
#available_belongs_tos ⇒ Object
31 32 33 |
# File 'lib/entitainer.rb', line 31 def available_belongs_tos @available_belongs_tos ||= [] end |
#available_has_manys ⇒ Object
35 36 37 |
# File 'lib/entitainer.rb', line 35 def available_has_manys @available_has_manys ||= [] end |
#belongs_to(*list) ⇒ Object
Used in schema block to define belongs-to type of relation.
74 75 76 77 78 79 80 81 82 |
# File 'lib/entitainer.rb', line 74 def belongs_to(*list) @available_belongs_tos = [] list.each do |attr| @available_belongs_tos << attr relation_id_attr = "#{attr}_id".to_sym @available_attributes << relation_id_attr unless @available_attributes.include?(relation_id_attr) end end |
#has_many(*list) ⇒ Object
Used in schema block to define has-many type of relation. rubocop:disable Naming/PredicateName
86 87 88 89 |
# File 'lib/entitainer.rb', line 86 def has_many(*list) @available_has_manys = [] list.each { |attr| @available_has_manys << attr } end |
#schema ⇒ Object
Defines entity attributes and relations.
schema do
attributes # ...
belongs_to # ...
has_many # ...
end
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/entitainer.rb', line 45 def schema @available_attributes = [] yield add_id_attribute define_methods define_method(:initialize) do |**args, &block| assign_values(args) assign_belongs_to(args) assign_has_many block&.call(self) freeze_it end define_method(:==) do |other| cmp_with(other) end end |