Sha256: d97fc5ec5b6b1fe9587f9c8c87ef8f8b6c603d0a1b9e0c4c7eae94485b80113e

Contents?: true

Size: 1.72 KB

Versions: 2

Compression:

Stored size: 1.72 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 =~ /\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.to_s =~ accept) if accept
      modules.reject!(&it.to_s =~ reject) if reject
      modules
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
schema_monkey-2.1.4 lib/schema_monkey/client.rb
schema_monkey-2.1.3 lib/schema_monkey/client.rb