Skip to content

Commit

Permalink
Added DataMapper::Property::Lookup module.
Browse files Browse the repository at this point in the history
The module provides transparent access to Property and Type classes via
a const_missing method.
  • Loading branch information
postmodern authored and Piotr Solnica committed Jul 8, 2010
1 parent 9538824 commit 0780ac1
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/dm-core.rb
Expand Up @@ -102,6 +102,7 @@ module ActiveSupport
require 'dm-core/property/time'
require 'dm-core/property/class'
require 'dm-core/property/discriminator'
require 'dm-core/property/lookup'
require 'dm-core/property_set'

require 'dm-core/adapters'
Expand Down
42 changes: 42 additions & 0 deletions lib/dm-core/property/lookup.rb
@@ -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
26 changes: 26 additions & 0 deletions spec/semipublic/property/lookup_spec.rb
@@ -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

0 comments on commit 0780ac1

Please sign in to comment.