Class: Dbee::Model

Inherits:
Object
  • Object
show all
Extended by:
Util::MakeKeyedBy, Forwardable
Defined in:
lib/dbee/model.rb,
lib/dbee/model/constraints.rb,
lib/dbee/model/partitioner.rb,
lib/dbee/model/relationships.rb,
lib/dbee/model/constraints/base.rb,
lib/dbee/model/constraints/static.rb,
lib/dbee/model/relationships/basic.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, Partitioner, Relationships

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::MakeKeyedBy

make_keyed_by

Constructor Details

#initialize(name:, constraints: [], relationships: [], models: [], partitioners: [], table: '') ⇒ Model

Returns a new instance of Model.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dbee/model.rb', line 32

def initialize(
  name:,
  constraints: [], # Exists here for tree based model backward compatibility.
  relationships: [],
  models: [],      # Exists here for tree based model backward compatibility.
  partitioners: [],
  table: ''
)
  @name           = name
  @constraints    = Constraints.array(constraints || []).uniq
  @relationships  = Relationships.make_keyed_by(:name, relationships)
  @models_by_name = name_hash(Model.array(models))
  @partitioners   = Partitioner.array(partitioners).uniq
  @table          = table.to_s.empty? ? @name : table.to_s

  ensure_input_is_valid

  freeze
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



25
26
27
# File 'lib/dbee/model.rb', line 25

def constraints
  @constraints
end

#filtersObject (readonly)

Returns the value of attribute filters.



25
26
27
# File 'lib/dbee/model.rb', line 25

def filters
  @filters
end

#nameObject (readonly)

Returns the value of attribute name.



25
26
27
# File 'lib/dbee/model.rb', line 25

def name
  @name
end

#partitionersObject (readonly)

Returns the value of attribute partitioners.



25
26
27
# File 'lib/dbee/model.rb', line 25

def partitioners
  @partitioners
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



25
26
27
# File 'lib/dbee/model.rb', line 25

def relationships
  @relationships
end

#tableObject (readonly)

Returns the value of attribute table.



25
26
27
# File 'lib/dbee/model.rb', line 25

def table
  @table
end

Instance Method Details

#<=>(other) ⇒ Object



62
63
64
# File 'lib/dbee/model.rb', line 62

def <=>(other)
  name <=> other.name
end

#==(other) ⇒ Object Also known as: eql?



56
57
58
59
# File 'lib/dbee/model.rb', line 56

def ==(other)
  other.instance_of?(self.class) &&
    other.name == name && other.table == table && children_are_equal(other)
end

#hashObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/dbee/model.rb', line 66

def hash
  [
    name.hash,
    table.hash,
    relationships.hash,
    sorted_constraints.hash,
    sorted_partitioners.hash,
    sorted_models.hash
  ].hash
end

#relationship_for_name(relationship_name) ⇒ Object



52
53
54
# File 'lib/dbee/model.rb', line 52

def relationship_for_name(relationship_name)
  relationships[relationship_name]
end

#to_sObject



77
78
79
# File 'lib/dbee/model.rb', line 77

def to_s
  name
end