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



35
36
37
38
39
40
# File 'lib/exwiw/mongodb_collection_config.rb', line 35

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

.from_symbol_keys(hash) ⇒ Object



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

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

Instance Method Details

#embedded?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/exwiw/mongodb_collection_config.rb', line 46

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.



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/exwiw/mongodb_collection_config.rb', line 68

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.ignore_type = ignore_type
    # A freshly generated comment (e.g. the skip_unsupported marker) wins so
    # it stays accurate; otherwise a hand-added note on a normal collection
    # is kept.
    merged.comment = passed.comment || comment
    merged.embedded_in = passed.embedded_in

    # Structural facts of each belongs_to come from the freshly generated
    # config (including a generator-derived `references`), but the user-owned
    # `comment`/`ignore` — and a hand-edited `references`, which overrides the
    # generated one — 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?
        pbt.ignore_type = receiver_bt.ignore_type if receiver_bt.ignore_type
        pbt.references = receiver_bt.references if receiver_bt.references
      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).



53
54
55
56
57
# File 'lib/exwiw/mongodb_collection_config.rb', line 53

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