Class: Dbee::Base

Inherits:
Object
  • Object
show all
Extended by:
Dsl::Inflectable, Dsl::Reflectable
Defined in:
lib/dbee/base.rb

Overview

Instead of using the configuration-first approach, you could use this super class for Model declaration.

Constant Summary collapse

BASE_CLASS_CONSTANT =
Dbee::Base

Class Method Summary collapse

Methods included from Dsl::Inflectable

constantize, tableize

Methods included from Dsl::Reflectable

reversed_subclasses, subclasses

Class Method Details

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



31
32
33
# File 'lib/dbee/base.rb', line 31

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

.associations_by_nameObject



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

def associations_by_name
  @associations_by_name ||= {}
end

.inherited_associationsObject



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

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

.inherited_partitionersObject



81
82
83
84
85
# File 'lib/dbee/base.rb', line 81

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

.inherited_table_nameObject



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

def inherited_table_name
  subclasses(BASE_CLASS_CONSTANT).find(&:table_name?)&.table_name ||
    tableize(reversed_subclasses(BASE_CLASS_CONSTANT).first.name)
end

.partitioner(name, value) ⇒ Object



23
24
25
# File 'lib/dbee/base.rb', line 23

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

.partitionersObject



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

def partitioners
  @partitioners ||= []
end

.table(name) ⇒ Object



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

def table(name)
  tap { @table_name = name.to_s }
end

.table_nameObject



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

def table_name
  @table_name || ''
end

.table_name?Boolean

Returns:

  • (Boolean)


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

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.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dbee/base.rb', line 40

def to_model(key_chain, name = nil, constraints = [], path_parts = [])
  derived_name  = name.to_s.empty? ? tableize(self.name) : name.to_s
  key           = [key_chain, derived_name, constraints, path_parts]

  to_models[key] ||= Model.make(
    model_config(
      key_chain,
      derived_name,
      constraints,
      path_parts + [name]
    )
  )
end