Skip to content

Commit

Permalink
Merge pull request #45 from tguo/move-configuration-out-of-model
Browse files Browse the repository at this point in the history
[WIP] Move lotus model adapter into Configuration
  • Loading branch information
jodosha committed Jul 29, 2014
2 parents 1b0ee81 + 9c19106 commit 7f912c0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 35 deletions.
32 changes: 1 addition & 31 deletions lib/lotus/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,7 @@ class NonPersistedEntityError < ::StandardError
# adapter :sql, 'postgres://localhost/database', default: true
# end
def self.configure(&block)
instance_eval(&block)
end

# Register adapter
#
# If `default` params is set to `true`, the adapter will be used as default one
#
# @param name [Symbol] Derive adapter class name
# @param uri [String] The adapter uri
# @param default [TrueClass,FalseClass] Decide if adapter is used by default
#
# @since 0.2.0
#
# @see Lotus::Model#adapter
# @see Lotus::Model#configure
#
# @example Register SQL Adapter as default adapter
# require 'lotus/model'
#
# Lotus::Model.adapters # => {}
#
# @example Register an adapter
# require 'lotus/model'
#
# Lotus::Model.configure do
# adapter :sql, 'postgres://localhost/database', default: true
# end
def self.adapter(name, uri, default: false)
adapter = Lotus::Model::Config::Adapter.new(name, uri)
configuration.adapters[name] = adapter
configuration.adapters[:default] = adapter if default
configuration.instance_eval(&block)
end
end
end
44 changes: 40 additions & 4 deletions lib/lotus/model/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ module Model
# @since 0.2.0
class Configuration

# @attr_accessor adapters [Hash] a hash of Lotus::Model::Config::Adapter
# @attr_reader adapters [Hash] a hash of Lotus::Model::Config::Adapter
#
# @since 0.2.0
#
# @see Lotus::Controller::Configuration#adapters
attr_accessor :adapters
# @see Lotus::Model::Configuration#adapters
attr_reader :adapters

# Initialize a configuration instance
#
Expand All @@ -22,7 +22,7 @@ class Configuration
#
# @since 0.2.0
def initialize
@adapters = {}
reset!
end

# Reset all the values to the defaults
Expand All @@ -32,6 +32,42 @@ def initialize
def reset!
@adapters = {}
end

# Register adapter
#
# If `default` params is set to `true`, the adapter will be used as default one
#
# @param name [Symbol] Derive adapter class name
# @param uri [String] The adapter uri
# @param default [TrueClass, FalseClass] Decide if adapter is used by default
#
# @since x.x.x
#
# @see Lotus::Model#configure
#
# @example Register SQL Adapter as default adapter
# require 'lotus/model'
#
# Lotus::Model.configure do
# adapter :sql, 'postgres://localhost/database', default: true
# end
#
# Lotus::Model.adapters.fetch(:default)
# Lotus::Model.adapters.fetch(:sql)
#
# @example Register an adapter
# require 'lotus/model'
#
# Lotus::Model.configure do
# adapter :sql, 'postgres://localhost/database'
# end
#
# Lotus::Model.adapters.fetch(:sql)
def adapter(name, uri, default: false)
adapter = Lotus::Model::Config::Adapter.new(name, uri)
@adapters[name] = adapter
@adapters[:default] = adapter if default
end
end
end
end
37 changes: 37 additions & 0 deletions test/model/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'test_helper'

describe Lotus::Model::Configuration do
before do
@configuration = Lotus::Model::Configuration.new
end

describe '#adapter' do
it 'defaults to an empty set' do
@configuration.adapters.must_be_empty
end

it 'allows to register adapter' do
@configuration.adapter(:sql, 'postgres://localhost/database')

adapter = @configuration.adapters.fetch(:sql)
adapter.must_be_kind_of Lotus::Model::Config::Adapter
adapter.uri.must_equal 'postgres://localhost/database'
end

it 'allows to register default adapter' do
@configuration.adapter(:sql, 'postgres://localhost/database', default: true)

default_adapter = @configuration.adapters.fetch(:default)
default_adapter.must_be_kind_of Lotus::Model::Config::Adapter
default_adapter.uri.must_equal 'postgres://localhost/database'
end

it 'eliminates duplications' do
@configuration.adapter(:sql, 'postgres://localhost/database')
@configuration.adapter(:sql, 'postgres://localhost/database')

@configuration.adapters.size.must_equal(1)
end
end

end
6 changes: 6 additions & 0 deletions test/model_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
require 'test_helper'

describe Lotus::Model do
describe '.configuration' do
it 'exposes class configuration' do
Lotus::Model.configuration.must_be_kind_of(Lotus::Model::Configuration)
end
end

describe '.configure' do
describe '.adapter' do
before do
Expand Down

0 comments on commit 7f912c0

Please sign in to comment.