Class: Exwiw::MongodbCollectionConfig

Inherits:
Object
  • Object
show all
Includes:
Serdes
Defined in:
lib/exwiw/mongodb_collection_config.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(obj) ⇒ Object



24
25
26
27
28
# File 'lib/exwiw/mongodb_collection_config.rb', line 24

def self.from(obj)
  instance = super
  instance.__send__(:validate_embedded!)
  instance
end

.from_symbol_keys(hash) ⇒ Object



30
31
32
# File 'lib/exwiw/mongodb_collection_config.rb', line 30

def self.from_symbol_keys(hash)
  from(JSON.parse(hash.to_json))
end

Instance Method Details

#embedded?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/exwiw/mongodb_collection_config.rb', line 34

def embedded?
  !embedded_in.nil?
end

#merge(passed) ⇒ Object

Merge an auto-generated config (‘passed`) into this user-maintained one so that `MongoidSchemaGenerator` regenerations preserve hand-edited values.

  • structural facts come from the freshly generated config: primary_key, belongs_tos, embedded_in.

  • user customizations are kept from the receiver: filter, ignore, bulk_insert_chunk_size, and each field’s ‘replace_with` masking rule.

  • generated fields drive the field list (so added/removed fields track the model), but a matching receiver field wins to retain its masking.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/exwiw/mongodb_collection_config.rb', line 56

def merge(passed)
  return passed if passed.to_hash == to_hash

  MongodbCollectionConfig.new.tap do |merged|
    merged.name = name
    merged.primary_key = passed.primary_key
    merged.filter = filter
    merged.bulk_insert_chunk_size = bulk_insert_chunk_size
    merged.ignore = ignore
    merged.embedded_in = passed.embedded_in

    # Structural facts of each belongs_to come from the freshly generated
    # config, but the user-owned `comment`/`ignore` carry over when the same
    # relation still exists.
    receiver_belongs_to_by_identity = belongs_tos.each_with_object({}) { |bt, h| h[bt.identity] = bt }
    merged.belongs_tos = passed.belongs_tos.map do |pbt|
      receiver_bt = receiver_belongs_to_by_identity[pbt.identity]
      if receiver_bt
        pbt.comment = receiver_bt.comment if receiver_bt.comment
        pbt.ignore = receiver_bt.ignore unless receiver_bt.ignore.nil?
      end
      pbt
    end

    # Take each field from the freshly generated config (so structural facts
    # like `mongoid_field_name` track the model) but carry over the user's
    # hand-edited `replace_with`/`comment`/`ignore` when the field still exists.
    receiver_field_by_name = fields.each_with_object({}) { |f, h| h[f.name] = f }
    merged.fields = passed.fields.map do |pf|
      receiver = receiver_field_by_name[pf.name]
      if receiver
        pf.replace_with = receiver.replace_with if receiver.replace_with
        pf.comment = receiver.comment if receiver.comment
        pf.ignore = receiver.ignore unless receiver.ignore.nil?
      end
      pf
    end
  end
end

#reject_ignored_members!Object

Drop the belongs_tos/fields flagged ‘ignore:true` so they are excluded from extraction. The config files on disk keep these entries; this is applied to the runtime config right after it is loaded (see Runner#load_table_config).



41
42
43
44
45
# File 'lib/exwiw/mongodb_collection_config.rb', line 41

def reject_ignored_members!
  self.belongs_tos = belongs_tos.reject(&:ignore)
  self.fields = fields.reject(&:ignore)
  self
end