forked from datamapper/dm-core
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DataMapper::Property::Lookup module.
The module provides transparent access to Property and Type classes via a const_missing method.
- Loading branch information
1 parent
9538824
commit 0780ac1
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module DataMapper | ||
class Property | ||
module Lookup | ||
|
||
protected | ||
|
||
# | ||
# Provides transparent access to the Properties defined in | ||
# {Property}. It also provides access to the legacy {Types} namespace. | ||
# | ||
# @param [Symbol] name | ||
# The name of the property to lookup. | ||
# | ||
# @return [Property, Type] | ||
# The property with the given name. | ||
# | ||
# @raise [NameError] | ||
# The property could not be found. | ||
# | ||
# @api private | ||
# | ||
# @since 1.0.1 | ||
# | ||
def const_missing(name) | ||
if const = Property.find_class(name) | ||
return const | ||
end | ||
|
||
# only check within DataMapper::Types, if it was loaded. | ||
if DataMapper.const_defined?(:Types) | ||
if DataMapper::Types.const_defined?(name) | ||
type = DataMapper::Types.const_get(name) | ||
|
||
return type if type < DataMapper::Type | ||
end | ||
end | ||
|
||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '../..', 'spec_helper')) | ||
require 'dm-core/property/lookup' | ||
|
||
describe DataMapper::Property::Lookup do | ||
supported_by :all do | ||
before(:all) do | ||
@klass = Class.new { extend DataMapper::Model } | ||
|
||
DataMapper::Types::LegacyType = Class.new(DataMapper::Types::Text) | ||
end | ||
|
||
it "should provide access to Property classes" do | ||
@klass::Serial.should == DataMapper::Property::Serial | ||
end | ||
|
||
it "should provide access to legacy Types" do | ||
@klass::LegacyType.should be(DataMapper::Types::LegacyType) | ||
end | ||
|
||
it "should not provide access to unknown Property classes" do | ||
lambda { | ||
@klass::Bla | ||
}.should raise_error(NameError) | ||
end | ||
end | ||
end |