Skip to content
Browse files

Renamed Resource::ClassMethods#resource_name as #storage_name

* Added Repository.default_name
* Updated Resource::ClassMethods methods that accepted repository_name
  as an argument to use default_repository_name if no argument passed
  • Loading branch information...
1 parent bfc1bee commit 179820ab9e044b23dcbf6306b5f752b03eb5a232 Dan Kubb committed Apr 11, 2008
View
18 lib/data_mapper/adapters/data_objects_adapter.rb
@@ -236,7 +236,7 @@ def query(sql, *args)
module SQL
def create_statement(model, properties)
<<-EOS.compress_lines
- INSERT INTO #{quote_table_name(model.resource_name(name))}
+ INSERT INTO #{quote_table_name(model.storage_name(name))}
(#{properties.map { |property| quote_column_name(property.field) }.join(', ')})
VALUES
(#{(['?'] * properties.size).join(', ')})
@@ -245,7 +245,7 @@ def create_statement(model, properties)
def create_statement_with_returning(model, properties)
<<-EOS.compress_lines
- INSERT INTO #{quote_table_name(model.resource_name(name))}
+ INSERT INTO #{quote_table_name(model.storage_name(name))}
(#{properties.map { |property| quote_column_name(property.field) }.join(', ')})
VALUES
(#{(['?'] * properties.size).join(', ')})
@@ -257,22 +257,22 @@ def read_statement(model, key)
properties = model.properties(name).defaults
<<-EOS.compress_lines
SELECT #{properties.map { |property| quote_column_name(property.field) }.join(', ')}
- FROM #{quote_table_name(model.resource_name(name))}
+ FROM #{quote_table_name(model.storage_name(name))}
WHERE #{model.key(name).map { |key| "#{quote_column_name(key.field)} = ?" }.join(' AND ')}
EOS
end
def update_statement(model, properties)
<<-EOS.compress_lines
- UPDATE #{quote_table_name(model.resource_name(name))}
+ UPDATE #{quote_table_name(model.storage_name(name))}
SET #{properties.map {|attribute| "#{quote_column_name(attribute.field)} = ?" }.join(', ')}
WHERE #{model.key(name).map { |key| "#{quote_column_name(key.field)} = ?" }.join(' AND ')}
EOS
end
def delete_statement(model)
<<-EOS.compress_lines
- DELETE FROM #{quote_table_name(model.resource_name(name))}
+ DELETE FROM #{quote_table_name(model.storage_name(name))}
WHERE #{model.key(name).map { |key| "#{quote_column_name(key.field)} = ?" }.join(' AND ')}
EOS
end
@@ -285,7 +285,7 @@ def query_read_statement(query)
sql << query.fields.map do |property|
# deriving the model name from the property and not the query
# allows for "foreign" properties to be qualified correctly
- model_name = property.model.resource_name(property.model.repository.name)
+ model_name = property.model.storage_name(property.model.repository.name)
property_to_column_name(model_name, property, qualify)
end.join(', ')
@@ -296,8 +296,8 @@ def query_read_statement(query)
query.links.each do |relationship|
child_model = relationship.child_model
parent_model = relationship.parent_model
- child_model_name = child_model.resource_name(child_model.repository.name)
- parent_model_name = parent_model.resource_name(parent_model.repository.name)
+ child_model_name = child_model.storage_name(child_model.repository.name)
+ parent_model_name = parent_model.storage_name(parent_model.repository.name)
child_keys = relationship.child_key.to_a
# We only do LEFT OUTER JOIN for now
@@ -324,7 +324,7 @@ def query_read_statement(query)
sql << "(" << query.conditions.map do |operator, property, value|
# deriving the model name from the property and not the query
# allows for "foreign" properties to be qualified correctly
- model_name = property.model.resource_name(property.model.repository.name)
+ model_name = property.model.storage_name(property.model.repository.name)
case operator
when :eql, :in then equality_operator(query,model_name,operator, property, qualify, value)
when :not then inequality_operator(query,model_name,operator, property, qualify, value)
View
2 lib/data_mapper/query.rb
@@ -171,7 +171,7 @@ def initialize(model, options = {})
@repository = model.repository
repository_name = @repository.name
- @model_name = model.resource_name(repository_name)
+ @model_name = model.storage_name(repository_name)
@properties = model.properties(repository_name)
@model = model # must be Class that includes DM::Resource
View
4 lib/data_mapper/repository.rb
@@ -14,6 +14,10 @@ def self.context
Thread.current[:dm_repository_contexts] ||= []
end
+ def self.default_name
+ :default
+ end
+
attr_reader :name, :adapter
def identity_map_get(model, key)
View
29 lib/data_mapper/resource.rb
@@ -207,27 +207,24 @@ def private_attributes=(values_hash)
module ClassMethods
def self.extended(base)
- base.instance_variable_set(:@resource_names, Hash.new { |h,k| h[k] = repository(k).adapter.resource_naming_convention.call(base.name) })
- base.instance_variable_set(:@properties, Hash.new { |h,k| h[k] = k == :default ? PropertySet.new : h[:default].dup })
+ base.instance_variable_set(:@storage_names, Hash.new { |h,k| h[k] = repository(k).adapter.resource_naming_convention.call(base.name) })
+ base.instance_variable_set(:@properties, Hash.new { |h,k| h[k] = k == :default ? PropertySet.new : h[:default].dup })
end
- def repository(name = default_repository_name)
- DataMapper.repository(name)
+ def default_repository_name
+ Repository.default_name
end
- def default_repository_name
- :default
+ def repository(repository_name = default_repository_name)
+ DataMapper.repository(repository_name)
end
- # FIXME: should this be renamed, since it returns the name of
- # physical storage location, and resource_name is a confusing
- # name when there is already a resource object.
- def resource_name(repository_name)
- @resource_names[repository_name]
+ def storage_name(repository_name = default_repository_name)
+ @storage_names[repository_name]
end
- def resource_names
- @resource_names
+ def storage_names
+ @storage_names
end
def property(name, type, options = {})
@@ -258,15 +255,15 @@ def property(name, type, options = {})
property
end
- def properties(repository_name)
+ def properties(repository_name = default_repository_name)
@properties[repository_name]
end
- def key(repository_name)
+ def key(repository_name = default_repository_name)
@properties[repository_name].key
end
- def inheritance_property(repository_name)
+ def inheritance_property(repository_name = default_repository_name)
@properties[repository_name].inheritance_property
end
View
2 spec/integration/association_spec.rb
@@ -6,7 +6,7 @@
begin
require 'do_sqlite3'
- DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db")
+ DataMapper.setup(:sqlite3, "sqlite3://#{INTEGRATION_DB_PATH}")
class Engine
include DataMapper::Resource
View
8 spec/integration/property_spec.rb
@@ -5,10 +5,14 @@
begin
require 'do_sqlite3'
+ DataMapper.setup(:sqlite3, "sqlite3://#{INTEGRATION_DB_PATH}")
describe DataMapper::Property do
- describe" tracking strategies" do
+ before do
+ @adapter = repository(:sqlite3).adapter
+ end
+ describe" tracking strategies" do
before do
class Actor
include DataMapper::Resource
@@ -24,7 +28,6 @@ class Actor
# Potentially faster, but less safe, so use judiciously, when the odds of a hash-collision are low.
end
- @adapter = DataMapper::Repository.adapters[:sqlite3] || DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db")
@adapter.execute <<-EOS.compress_lines
CREATE TABLE actors (
id INTEGER PRIMARY KEY,
@@ -86,7 +89,6 @@ class Actor
describe "lazy loading" do
before do
- @adapter = DataMapper::Repository.adapters[:sqlite3] || DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db")
@adapter.execute(<<-EOS.compress_lines) rescue nil
CREATE TABLE "sail_boats" (
"id" INTEGER PRIMARY KEY,
View
2 spec/integration/query_spec.rb
@@ -6,7 +6,7 @@
begin
require 'do_sqlite3'
- DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db")
+ DataMapper.setup(:sqlite3, "sqlite3://#{INTEGRATION_DB_PATH}")
describe DataMapper::Query do
describe 'when ordering' do
View
3 spec/integration/repository_spec.rb
@@ -3,11 +3,10 @@
require ROOT_DIR + 'lib/data_mapper'
-
begin
require 'do_sqlite3'
- DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db")
+ DataMapper.setup(:sqlite3, "sqlite3://#{INTEGRATION_DB_PATH}")
describe DataMapper::Repository do
describe "finders" do
View
6 spec/integration/sqlite3_adapter_spec.rb
@@ -3,14 +3,10 @@
require ROOT_DIR + 'lib/data_mapper'
-DB_PATH = __DIR__ + 'integration_test.db'
-
begin
require 'do_sqlite3'
- FileUtils.touch DB_PATH
-
- DataMapper.setup(:sqlite3, "sqlite3://#{DB_PATH}")
+ DataMapper.setup(:sqlite3, "sqlite3://#{INTEGRATION_DB_PATH}")
describe DataMapper::Adapters::DataObjectsAdapter do
View
4 spec/integration/type_spec.rb
@@ -5,7 +5,7 @@
begin
require 'do_sqlite3'
- DataMapper.setup(:sqlite3, "sqlite3://#{__DIR__}/integration_test.db") unless DataMapper::Repository.adapters[:sqlite3]
+ DataMapper.setup(:sqlite3, "sqlite3://#{INTEGRATION_DB_PATH}")
describe DataMapper::Type do
@@ -22,7 +22,7 @@ class Impostor < DataMapper::Type
class Coconut
include DataMapper::Resource
- resource_names[:sqlite3] = 'coconuts'
+ storage_names[:sqlite3] = 'coconuts'
property :id, Fixnum, :serial => true
property :faked, Impostor
View
5 spec/spec_helper.rb
@@ -6,12 +6,15 @@
# for __DIR__
require Pathname(__FILE__).dirname.expand_path.parent + 'lib/data_mapper/support/kernel'
-ROOT_DIR = __DIR__.parent
+ROOT_DIR = __DIR__.parent
+INTEGRATION_DB_PATH = __DIR__ + 'integration/integration_test.db'
+FileUtils.touch INTEGRATION_DB_PATH
ENV['LOG_NAME'] = 'spec'
require ROOT_DIR + 'environment'
require __DIR__ + 'lib/mock_adapter'
+
class Article
include DataMapper::Resource
View
7 spec/unit/repository_spec.rb
@@ -42,4 +42,11 @@ class Vegetable
repository.save(instance)
end
+ it 'should provide default_name' do
+ DataMapper::Repository.should respond_to(:default_name)
+ end
+
+ it 'should return :default for default_name' do
+ DataMapper::Repository.default_name.should == :default
+ end
end
View
137 spec/unit/resource_spec.rb
@@ -6,7 +6,7 @@
# DO NOT CHANGE THIS!
describe "DataMapper::Resource" do
- before(:all) do
+ before :all do
DataMapper.setup(:default, "mock://localhost/mock") unless DataMapper::Repository.adapters[:default]
DataMapper.setup(:legacy, "mock://localhost/mock") unless DataMapper::Repository.adapters[:legacy]
@@ -20,12 +20,13 @@ class Planet
include DataMapper::Resource
- resource_names[:legacy] = "dying_planets"
+ storage_names[:legacy] = "dying_planets"
property :id, Fixnum, :key => true
property :name, String, :lock => true
property :age, Fixnum
property :core, String, :private => true
+ property :type, Class
# An example of how to scope a property to a specific repository.
# Un-specced currently.
@@ -39,41 +40,21 @@ class Planet
Planet.create(:name => 'Venus', :age => 1_000_000, :core => nil, :id => 42).should be_a_kind_of(Planet)
end
- it "should provide persistance methods" do
- Planet.should respond_to(:get)
- Planet.should respond_to(:first)
- Planet.should respond_to(:all)
- Planet.should respond_to(:[])
-
+ it 'should provide persistance methods' do
planet = Planet.new
planet.should respond_to(:new_record?)
planet.should respond_to(:save)
planet.should respond_to(:destroy)
end
- it "should provide a resource_name" do
- Planet.should respond_to(:resource_name)
- Planet.resource_name(:default).should == 'planets'
- Planet.resource_name(:legacy).should == 'dying_planets'
- Planet.resource_name(:yet_another_repository).should == 'planet'
- end
-
- it "should provide properties" do
- Planet.properties(:default).should have(4).entries
- end
-
- it "should provide mapping defaults" do
- Planet.properties(:yet_another_repository).should have(4).entries
- end
-
it "should have attributes" do
- attributes = { :name => 'Jupiter', :age => 1_000_000, :core => nil, :id => 42 }
+ attributes = { :name => 'Jupiter', :age => 1_000_000, :core => nil, :id => 42, :type => nil }
jupiter = Planet.new(attributes)
jupiter.attributes.should == attributes
end
it "should be able to set attributes (including private attributes)" do
- attributes = { :name => 'Jupiter', :age => 1_000_000, :core => nil, :id => 42 }
+ attributes = { :name => 'Jupiter', :age => 1_000_000, :core => nil, :id => 42, :type => nil }
jupiter = Planet.new(attributes)
jupiter.attributes.should == attributes
jupiter.attributes = attributes.merge({ :core => 'Magma' })
@@ -82,10 +63,6 @@ class Planet
jupiter.attributes.should == attributes.merge({ :core => 'Magma' })
end
- it "should provide a repository" do
- Planet.repository.name.should == :default
- end
-
it "should track attributes" do
# So attribute tracking is a feature of the Resource,
@@ -153,9 +130,105 @@ class Planet
mars.shadow_attribute_get(:name).should == 'Mars'
end
- it 'should add hook functionality to including class' do
- Planet.should respond_to(:before)
- Planet.should respond_to(:after)
+ describe 'ClassMethods' do
+ it 'should add hook functionality to including class' do
+ Planet.should respond_to(:before)
+ Planet.should respond_to(:after)
+ end
+
+ it 'should provide default_repository_name' do
+ Planet.should respond_to(:default_repository_name)
+ end
+
+ it '.default_repository_name should delegate to DataMapper::Repository.default_name' do
+ DataMapper::Repository.should_receive(:default_name).with(no_args).once.and_return(:default)
+ Planet.default_repository_name.should == :default
+ end
+
+ it 'should provide a repository' do
+ Planet.should respond_to(:repository)
+ end
+
+ it '.repository should delegate to DataMapper.repository' do
+ repository = mock('repository')
+ DataMapper.should_receive(:repository).with(:legacy).once.and_return(repository)
+ Planet.repository(:legacy).should == repository
+ end
+
+ it '.repository should use default repository when not passed any arguments' do
+ Planet.repository.name.should == Planet.repository(:default).name
+ end
+
+ it 'should provide storage_name' do
+ Planet.should respond_to(:storage_name)
+ end
+
+ it '.storage_name should map a repository to the storage location' do
+ Planet.storage_name(:legacy).should == 'dying_planets'
+ end
+
+ it '.storage_name should use default repository when not passed any arguments' do
+ Planet.storage_name.should == Planet.storage_name(:default)
+ end
+
+ it 'should provide storage_names' do
+ Planet.should respond_to(:storage_names)
+ end
+
+ it '.storage_names should return a Hash mapping each repository to a storage location' do
+ Planet.storage_names.should be_kind_of(Hash)
+ Planet.storage_names.should == { :default => 'planets', :legacy => 'dying_planets' }
+ end
+
+ it 'should provide property' do
+ Planet.should respond_to(:property)
+ end
+
+ it 'should specify property'
+
+ it 'should provide properties' do
+ Planet.should respond_to(:properties)
+ end
+
+ it '.properties should return an PropertySet' do
+ Planet.properties(:legacy).should be_kind_of(DataMapper::PropertySet)
+ Planet.properties(:legacy).should have(5).entries
+ end
+
+ it 'should provide key' do
+ Planet.should respond_to(:key)
+ end
+
+ it '.key should return an Array of Property objects' do
+ Planet.key(:legacy).should be_kind_of(Array)
+ Planet.key(:legacy).should have(1).entries
+ Planet.key(:legacy).first.should be_kind_of(DataMapper::Property)
+ end
+
+ it '.key should use default repository when not passed any arguments' do
+ Planet.key.should == Planet.key(:default)
+ end
+
+ it 'should provide inheritance_property' do
+ Planet.should respond_to(:inheritance_property)
+ end
+
+ it '.inheritance_property should return a Property object' do
+ Planet.inheritance_property(:legacy).should be_kind_of(DataMapper::Property)
+ Planet.inheritance_property(:legacy).name.should == :type
+ Planet.inheritance_property(:legacy).type.should == Class
+ end
+
+ it '.inheritance_property should use default repository when not passed any arguments' do
+ Planet.inheritance_property.should == Planet.inheritance_property(:default)
+ end
+
+ it 'should provide finder methods' do
+ Planet.should respond_to(:get)
+ Planet.should respond_to(:first)
+ Planet.should respond_to(:all)
+ Planet.should respond_to(:[])
+ end
end
describe 'when retrieving by key' do

0 comments on commit 179820a

Please sign in to comment.
Something went wrong with that request. Please try again.