Sha256: a212efb79c14bc533f17faafca3663558280d12104da4d2b574a2738a85217df

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/class/attribute_accessors'

module Rails
  module DataMapper

    mattr_accessor :configuration

    class Configuration

      attr_accessor :raw
      attr_accessor :root

      def self.create
        Rails::DataMapper.configuration ||= new
      end

      def environments
        raw.keys
      end

      def repositories
        @repositories ||= raw.reject { |k,v| k =~ /defaults/ }.inject({}) do |repositories, pair|
          environment, config = pair.first, pair.last
          repositories[environment] = begin
            c = config['repositories'] || {}
            c['default'] = config.except('repositories') if config.except('repositories')
            normalize_repository_config(c)
          end
          repositories
        end
      end

      def resource_naming_convention
        @resource_naming_convention ||= {}
      end

    private

      def normalize_repository_config(hash)
        config = {}
        hash.each do |key, value|

          config[key] = if value.kind_of?(Hash)
            normalize_repository_config(value)
          elsif key == 'port'
            value.to_i
          elsif key == 'adapter' && value == 'postgresql'
            'postgres'
          elsif (key == 'database' || key == 'path') && hash['adapter'] =~ /sqlite/
            value == ':memory:' ? value : File.expand_path(hash[key], root)
          else
            value
          end

          # FIXME Rely on a new dm-sqlite-adapter to do the right thing
          # For now, we need to make sure that both 'path' and 'database'
          # point to the same thing, since dm-sqlite-adapter always passes
          # both to the underlying do_sqlite3 adapter and there's no
          # guarantee which one will be used

          config['path']     = config[key] if key == 'database'
          config['database'] = config[key] if key == 'path'

        end
        config
      end

    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dm-rails-1.1.0 lib/dm-rails/configuration.rb
dm-rails-1.1.0.rc3 lib/dm-rails/configuration.rb
dm-rails-1.1.0.rc2 lib/dm-rails/configuration.rb
dm-rails-1.1.0.rc1 lib/dm-rails/configuration.rb
dm-rails-1.0.4 lib/dm-rails/configuration.rb