Class: Dbee::Model
- Inherits:
-
Object
- Object
- Dbee::Model
- Defined in:
- lib/dbee/model.rb,
lib/dbee/model/constraints.rb,
lib/dbee/model/constraints/base.rb,
lib/dbee/model/constraints/static.rb,
lib/dbee/model/constraints/reference.rb
Overview
In DB terms, a Model is usually a table, but it does not have to be. You can also re-model your DB schema using Dbee::Models.
Defined Under Namespace
Classes: Constraints, ModelNotFoundError
Instance Attribute Summary collapse
-
#constraints ⇒ Object
readonly
Returns the value of attribute constraints.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #ancestors(parts = [], alias_chain = [], found = {}) ⇒ Object
-
#initialize(name:, constraints: [], models: [], table: '') ⇒ Model
constructor
A new instance of Model.
- #models ⇒ Object
- #name_hash(array) ⇒ Object
Constructor Details
#initialize(name:, constraints: [], models: [], table: '') ⇒ Model
Returns a new instance of Model.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dbee/model.rb', line 26 def initialize(name:, constraints: [], models: [], table: '') raise ArgumentError, 'name is required' if name.to_s.empty? @name = name.to_s @constraints = Constraints.array(constraints) @models_by_name = name_hash(Model.array(models)) @table = table.to_s.empty? ? @name : table.to_s freeze end |
Instance Attribute Details
#constraints ⇒ Object (readonly)
Returns the value of attribute constraints.
24 25 26 |
# File 'lib/dbee/model.rb', line 24 def constraints @constraints end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/dbee/model.rb', line 24 def name @name end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
24 25 26 |
# File 'lib/dbee/model.rb', line 24 def table @table end |
Instance Method Details
#<=>(other) ⇒ Object
73 74 75 |
# File 'lib/dbee/model.rb', line 73 def <=>(other) name <=> other.name end |
#==(other) ⇒ Object Also known as: eql?
65 66 67 68 69 70 |
# File 'lib/dbee/model.rb', line 65 def ==(other) other.name == name && other.table == table && other.models.sort == models.sort && other.constraints.sort == constraints.sort end |
#ancestors(parts = [], alias_chain = [], found = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/dbee/model.rb', line 45 def ancestors(parts = [], alias_chain = [], found = {}) return found if Array(parts).empty? alias_chain = [] if Array(alias_chain).empty? model_name = parts.first model = models_by_name[model_name.to_s] raise ModelNotFoundError, "Cannot traverse: #{model_name}" unless model new_alias_chain = alias_chain + [model_name] new_alias = new_alias_chain.join(JOIN_CHAR) found[new_alias] = model model.ancestors(parts[1..-1], new_alias_chain, found) end |
#models ⇒ Object
41 42 43 |
# File 'lib/dbee/model.rb', line 41 def models models_by_name.values end |
#name_hash(array) ⇒ Object
37 38 39 |
# File 'lib/dbee/model.rb', line 37 def name_hash(array) array.map { |a| [a.name, a] }.to_h end |