Class: SequelMapper::Configurations::ConventionalAssociationConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel_mapper/configurations/conventional_association_configuration.rb

Constant Summary collapse

DEFAULT =
Module.new

Instance Method Summary collapse

Constructor Details

#initialize(mapping_name, mappings, datastore) ⇒ ConventionalAssociationConfiguration



13
14
15
16
17
18
# File 'lib/sequel_mapper/configurations/conventional_association_configuration.rb', line 13

def initialize(mapping_name, mappings, datastore)
  @local_mapping_name = mapping_name
  @mappings = mappings
  @local_mapping = mappings.fetch(local_mapping_name)
  @datastore = datastore
end

Instance Method Details

#belongs_to(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sequel_mapper/configurations/conventional_association_configuration.rb', line 54

def belongs_to(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT)
  defaults = {
    key: :id,
    foreign_key: [association_name, "_id"].join.to_sym,
    mapping_name: INFLECTOR.pluralize(association_name).to_sym,
  }

  specified = {
    mapping_name: mapping_name,
    foreign_key: foreign_key,
    key: key,
  }.reject { |_k,v|
    v == DEFAULT
  }

  config = defaults.merge(specified)

  associated_mapping_name = config.fetch(:mapping_name)
  associated_mapping = mappings.fetch(associated_mapping_name)

  local_mapping.add_association(
    association_name,
    belongs_to_mapper(**config)
  )
end

#has_many(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sequel_mapper/configurations/conventional_association_configuration.rb', line 25

def has_many(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT)
  defaults = {
    mapping_name: association_name,
    foreign_key: [INFLECTOR.singularize(local_mapping_name), "_id"].join.to_sym,
    key: :id,
    order_fields: [],
    order_direction: "ASC",
  }

  specified = {
    mapping_name: mapping_name,
    foreign_key: foreign_key,
    key: key,
    order_fields: order_fields,
    order_direction: order_direction,
  }.reject { |_k,v|
    v == DEFAULT
  }

  config = defaults.merge(specified)
  associated_mapping_name = config.fetch(:mapping_name)
  associated_mapping = mappings.fetch(associated_mapping_name)

  local_mapping.add_association(
    association_name,
    has_many_mapper(**config)
  )
end

#has_many_through(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, through_mapping_name: DEFAULT, association_key: DEFAULT, association_foreign_key: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sequel_mapper/configurations/conventional_association_configuration.rb', line 80

def has_many_through(association_name, key: DEFAULT, foreign_key: DEFAULT, mapping_name: DEFAULT, through_mapping_name: DEFAULT, association_key: DEFAULT, association_foreign_key: DEFAULT, order_fields: DEFAULT, order_direction: DEFAULT)
  defaults = {
    mapping_name: association_name,
    key: :id,
    association_key: :id,
    foreign_key: [INFLECTOR.singularize(local_mapping_name), "_id"].join.to_sym,
    association_foreign_key: [INFLECTOR.singularize(association_name), "_id"].join.to_sym,
    order_fields: [],
    order_direction: "ASC",
  }

  specified = {
    mapping_name: mapping_name,
    key: key,
    association_key: association_key,
    foreign_key: foreign_key,
    association_foreign_key: association_foreign_key,
    order_fields: order_fields,
    order_direction: order_direction,
  }.reject { |_k,v|
    v == DEFAULT
  }

  config = defaults.merge(specified)
  associated_mapping = mappings.fetch(config.fetch(:mapping_name))

  if through_mapping_name == DEFAULT
    through_mapping_name = [
      associated_mapping.name,
      local_mapping.name,
    ].sort.join("_to_").to_sym
  end

  join_table_name = mappings.fetch(through_mapping_name).namespace
  config = config
    .merge(
      through_mapping_name: through_mapping_name,
      through_dataset: datastore[join_table_name.to_sym],
    )

  local_mapping.add_association(
    association_name,
    has_many_through_mapper(**config)
  )
end