Class: Dbee::Base

Inherits:
Object
  • Object
show all
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

Class Method Details

.association(name, opts = {}) ⇒ Object



25
26
27
28
29
# File 'lib/dbee/base.rb', line 25

def association(name, opts = {})
  associations_by_name[name.to_s] = opts.merge(name: name)

  self
end

.associations_by_nameObject



54
55
56
# File 'lib/dbee/base.rb', line 54

def associations_by_name
  @associations_by_name ||= {}
end

.inherited_associations_by_nameObject



70
71
72
73
74
# File 'lib/dbee/base.rb', line 70

def inherited_associations_by_name
  reversed_subclasses.each_with_object({}) do |subclass, memo|
    memo.merge!(subclass.associations_by_name)
  end
end

.inherited_partitionersObject



76
77
78
79
80
# File 'lib/dbee/base.rb', line 76

def inherited_partitioners
  reversed_subclasses.inject([]) do |memo, subclass|
    memo + subclass.partitioners
  end
end

.inherited_table_nameObject



66
67
68
# File 'lib/dbee/base.rb', line 66

def inherited_table_name
  subclasses.find(&:table_name?)&.table_name || ''
end

.partitioner(name, value) ⇒ Object



15
16
17
# File 'lib/dbee/base.rb', line 15

def partitioner(name, value)
  partitioners << { name: name, value: value }
end

.partitionersObject



58
59
60
# File 'lib/dbee/base.rb', line 58

def partitioners
  @partitioners ||= []
end

.table(name) ⇒ Object



19
20
21
22
23
# File 'lib/dbee/base.rb', line 19

def table(name)
  @table_name = name.to_s

  self
end

.table_nameObject



50
51
52
# File 'lib/dbee/base.rb', line 50

def table_name
  @table_name || ''
end

.table_name?Boolean

Returns:



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

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.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dbee/base.rb', line 36

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