Class: Dbee::Model

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Model.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dbee/model.rb', line 29

def initialize(name:, constraints: [], models: [], partitioners: [], table: '')
  raise ArgumentError, 'name is required' if name.to_s.empty?

  @name           = name.to_s
  @constraints    = Constraints.array(constraints).uniq
  @models_by_name = name_hash(Model.array(models))
  @partitioners   = Partitioner.array(partitioners).uniq
  @table          = table.to_s.empty? ? @name : table.to_s

  freeze
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



22
23
24
# File 'lib/dbee/model.rb', line 22

def constraints
  @constraints
end

#filtersObject (readonly)

Returns the value of attribute filters.



22
23
24
# File 'lib/dbee/model.rb', line 22

def filters
  @filters
end

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/dbee/model.rb', line 22

def name
  @name
end

#partitionersObject (readonly)

Returns the value of attribute partitioners.



22
23
24
# File 'lib/dbee/model.rb', line 22

def partitioners
  @partitioners
end

#tableObject (readonly)

Returns the value of attribute table.



22
23
24
# File 'lib/dbee/model.rb', line 22

def table
  @table
end

Instance Method Details

#<=>(other) ⇒ Object



74
75
76
# File 'lib/dbee/model.rb', line 74

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

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



64
65
66
67
68
69
70
71
# File 'lib/dbee/model.rb', line 64

def ==(other)
  other.instance_of?(self.class) &&
    other.name == name &&
    other.table == table &&
    other.sorted_constraints == sorted_constraints &&
    other.sorted_partitioners == sorted_partitioners &&
    other.sorted_models == sorted_models
end

#ancestors!(parts = [], visited_parts = [], found = {}) ⇒ Object

This recursive method will walk a path of model names (parts) and return back a flattened hash instead of a nested object structure. The hash key will be an array of strings (model names) and the value will be the identified model.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dbee/model.rb', line 45

def ancestors!(parts = [], visited_parts = [], found = {})
  return found if Array(parts).empty?

  # Take the first entry in parts
  model_name = parts.first.to_s

  # Ensure we have it registered as a child, or raise error
  model = assert_model(model_name, visited_parts)

  # Push onto visited list
  visited_parts += [model_name]

  # Add found model to flattened structure
  found[visited_parts] = model

  # Recursively call for next parts in the chain
  model.ancestors!(parts[1..-1], visited_parts, found)
end