Class: Dbee::Base
- Inherits:
-
Object
- Object
- Dbee::Base
- Defined in:
- lib/dbee/base.rb
Overview
Instead of using the configuration-first approach, you could use this super class for Model declaration.
Class Method Summary collapse
- .association(name, opts = {}) ⇒ Object
- .associations_by_name ⇒ Object
- .inherited_associations_by_name ⇒ Object
- .inherited_table_name ⇒ Object
- .table(name) ⇒ Object
- .table_name ⇒ Object
- .table_name? ⇒ Boolean
-
.to_model(key_chain, name = nil, constraints = [], path_parts = []) ⇒ Object
This method is cycle-resistant due to the fact that it is a requirement to send in a key_chain.
Class Method Details
.association(name, opts = {}) ⇒ Object
21 22 23 24 25 |
# File 'lib/dbee/base.rb', line 21 def association(name, opts = {}) associations_by_name[name.to_s] = opts.merge(name: name) self end |
.associations_by_name ⇒ Object
50 51 52 |
# File 'lib/dbee/base.rb', line 50 def associations_by_name @associations_by_name ||= {} end |
.inherited_associations_by_name ⇒ Object
62 63 64 65 66 |
# File 'lib/dbee/base.rb', line 62 def inherited_associations_by_name reversed_subclasses.each_with_object({}) do |subclass, memo| memo.merge!(subclass.associations_by_name) end end |
.inherited_table_name ⇒ Object
58 59 60 |
# File 'lib/dbee/base.rb', line 58 def inherited_table_name subclasses.find(&:table_name?)&.table_name || '' end |
.table(name) ⇒ Object
15 16 17 18 19 |
# File 'lib/dbee/base.rb', line 15 def table(name) @table_name = name.to_s self end |
.table_name ⇒ Object
46 47 48 |
# File 'lib/dbee/base.rb', line 46 def table_name @table_name || '' end |
.table_name? ⇒ Boolean
54 55 56 |
# File 'lib/dbee/base.rb', line 54 def table_name? !table_name.empty? end |
.to_model(key_chain, name = nil, constraints = [], path_parts = []) ⇒ Object
This method is cycle-resistant due to the fact that it is a requirement to send in a key_chain. That means each model produced using to_model is specific to a set of desired fields. Basically, you cannot derive a Model from a Base subclass without the context of a Query. This is not true for configuration-first Model definitions because, in that case, cycles do not exist since the nature of the configuration is flat.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dbee/base.rb', line 32 def to_model(key_chain, name = nil, constraints = [], path_parts = []) derived_name = derive_name(name) key = [key_chain, derived_name, constraints, path_parts] to_models[key] ||= Model.make( model_config( key_chain, derived_name, constraints, path_parts + [name] ) ) end |