Sha256: afe4aa8285a10e6fc98bdd31aee876616f0c2093407a9adccac4982c75d5ffa1

Contents?: true

Size: 1.74 KB

Versions: 4

Compression:

Stored size: 1.74 KB

Contents

module SchemaMonkey
  class Client

    def initialize(mod)
      @root = mod
      @inserted = {}
    end

    def insert(dbm: nil)
      insert_active_record(dbm: dbm)
      insert_middleware(dbm: dbm)
    end

    private

    def insert_active_record(dbm: nil)
      # Kernel.warn "--- inserting active_record for #{@root}, dbm=#{dbm.inspect}"
      find_modules(:ActiveRecord, dbm: dbm).each do |mod|
        next if @inserted[mod]
        relative_path = canonicalize_path(mod, :ActiveRecord, dbm)
        ActiveRecord.insert(relative_path, mod)
      end
    end

    def insert_middleware(dbm: nil)
      find_modules(:Middleware, dbm: dbm).each do |mod|
        next if @inserted[mod]
        relative_path = canonicalize_path(mod, :Middleware, dbm)
        Stack.insert(relative_path, mod) unless relative_path.empty?
        @inserted[mod] = true
      end
    end

    def canonicalize_path(mod, base, dbm)
      path = mod.to_s.sub(/^#{@root}::#{base}::/, '')
      if dbm
        path = path.split('::')
        if (i = path.find_index { |it| it =~ /\b#{dbm}\b/i }) # delete first occurence
          path.delete_at i
        end
        path = path.join('::').gsub(/#{dbm}/i, dbm.to_s) # canonicalize case for things like PostgreSQLAdapter
      end
      path
    end

    def find_modules(container, dbm: nil)
      return [] unless (container = Module.const_lookup @root, container)

      if dbm
        accept = /#{dbm}/i
        reject = nil
      else
        accept = nil
        reject = /\b(#{DBMS.join('|')})/i
      end

      modules = []
      modules += Module.descendants(container, can_load: accept)
      modules.select! { |it| it.to_s =~ accept } if accept
      modules.reject! { |it| it.to_s =~ reject } if reject
      modules
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
schema_monkey-3.0.1 lib/schema_monkey/client.rb
schema_monkey-3.0.0 lib/schema_monkey/client.rb
schema_monkey-2.1.6 lib/schema_monkey/client.rb
schema_monkey-2.1.5 lib/schema_monkey/client.rb