Class: Exwiw::MongoidSchemaGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/exwiw/mongoid_schema_generator.rb

Overview

Generates exwiw ‘MongodbCollectionConfig` files by introspecting Mongoid document models. This is the MongoDB/Mongoid counterpart of `SchemaGenerator` (which targets ActiveRecord); it is intentionally a separate class and rake task because the two ORMs expose entirely different metadata APIs.

Introspection relies only on class-level Mongoid metadata (‘fields`, `relations`, `collection_name`), so it does not require a live MongoDB connection.

Defined Under Namespace

Classes: UnsupportedEmbedding

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(models:, output_dir:, skip_unsupported: false) ⇒ MongoidSchemaGenerator

Returns a new instance of MongoidSchemaGenerator.



45
46
47
48
49
# File 'lib/exwiw/mongoid_schema_generator.rb', line 45

def initialize(models:, output_dir:, skip_unsupported: false)
  @models = models
  @output_dir = output_dir
  @skip_unsupported = skip_unsupported
end

Class Method Details

.from_rails_application(output_dir:, skip_unsupported: false) ⇒ Object

‘skip_unsupported`: when true, the generator does not abort on a construct it cannot represent. It skips an unresolvable `belongs_to` (keeping the foreign-key field) and emits an unrepresentable embedded collection as an `ignore: true` top-level config annotated with a `comment`, warning to stderr in both cases. Off by default, so the historical fail-loud behavior is unchanged unless a caller opts in.



40
41
42
43
# File 'lib/exwiw/mongoid_schema_generator.rb', line 40

def self.from_rails_application(output_dir:, skip_unsupported: false)
  Rails.application.eager_load!
  new(models: ::Mongoid.models, output_dir: output_dir, skip_unsupported: skip_unsupported)
end

Instance Method Details

#build_collectionsObject

Returns an array of ‘MongodbCollectionConfig`, one per collection (top-level collections and embedded subdocument configs alike).

Models are grouped by ‘collection_name` so an inheritance hierarchy whose subclasses share the base’s collection (Mongoid STI, discriminated by the auto-added ‘_type` field) collapses into a single config that aggregates every class’s fields and associations. See ‘expand_with_descendants`.



64
65
66
67
68
69
# File 'lib/exwiw/mongoid_schema_generator.rb', line 64

def build_collections
  models = expand_with_descendants(concrete_models)
  models
    .group_by { |model| model.collection_name.to_s }
    .map { |collection_name, group| build_collection_for(collection_name, group) }
end

#generate!Object



51
52
53
54
55
# File 'lib/exwiw/mongoid_schema_generator.rb', line 51

def generate!
  collections = build_collections
  write_files(@output_dir, collections)
  collections
end

#write_files(dir, collections) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/exwiw/mongoid_schema_generator.rb', line 71

def write_files(dir, collections)
  FileUtils.mkdir_p(dir)

  collections.each do |collection|
    path = File.join(dir, "#{collection.name}.json")
    config_to_write =
      if File.exist?(path)
        MongodbCollectionConfig.from(JSON.parse(File.read(path))).merge(collection)
      else
        collection
      end
    File.write(path, JSON.pretty_generate(config_to_write.to_hash) + "\n")
  end
end