Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

+ Namespaces

  • Loading branch information...
commit 59fb4fc7c8fff07e79f19d06c439387efbaacfe6 1 parent 42ead9d
Kaspar Schiess authored
2  Gemfile.lock
View
@@ -17,7 +17,7 @@ GEM
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
sanitize (2.0.3)
- nokogiri (< 1.6, >= 1.4.4)
+ nokogiri (>= 1.4.4, < 1.6)
sdoc (0.3.11)
json (>= 1.1.3)
rdoc (~> 3)
4 HISTORY.txt
View
@@ -1,3 +1,7 @@
+== 0.1.3 / 2012-05-21
+
+ + Allows the argument :namespace in FloorManager.define. All classes are
+ constructed from constants in that namespace, instead of Object.
== 0.1.2 / 2011-09-28
2  floor_manager.gemspec
View
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = "floor_manager"
- s.version = "0.1.2"
+ s.version = "0.1.3"
s.authors = ["Kaspar Schiess"]
s.email = "kaspar.schiess@absurd.li"
4 lib/floor_manager.rb
View
@@ -5,10 +5,10 @@ class <<self
# Defines a new environment under the supervision of the floor manager.
#
- def define(environment_name, &block)
+ def define(environment_name, options={}, &block)
@floors ||= {}
- @floors[environment_name] = FloorManager::Floor.from_dsl(&block)
+ @floors[environment_name] = FloorManager::Floor.from_dsl(options, &block)
end
# Returns an instance of the environment.
9 lib/floor_manager/employee.rb
View
@@ -3,12 +3,13 @@ module FloorManager::Employee
# Base class for employees. No instances of this should be created.
class Template
- def self.from_dsl(klass_name, &block)
- new(klass_name).tap { |emp| DSL.new(emp, &block) }
+ def self.from_dsl(klass_name, namespace=nil, &block)
+ new(klass_name, namespace).tap { |emp| DSL.new(emp, &block) }
end
- def initialize(klass_name)
+ def initialize(klass_name, namespace=nil)
@klass_name = klass_name
+ @namespace = namespace
@attributes = Hash.new { |h,k| h[k] = Array.new }
end
@@ -56,7 +57,7 @@ def add_attribute filter, action
def produce_instance
name = camelcase(@klass_name.to_s)
- Object.const_get(name).new
+ (@namespace || Object).const_get(name).new
end
def camelcase(str)
str.gsub(%r((^|_)\w)) { |match| match[-1].upcase }
13 lib/floor_manager/floor.rb
View
@@ -5,27 +5,30 @@
#
class FloorManager::Floor
class DSL
- def initialize(&block)
+ def initialize(options, &block)
+ @namespace = options[:namespace] || nil
@floor = FloorManager::Floor.new
instance_eval(&block)
end
def one(name, opts={}, &block)
klass_name = opts[:class] || name
- @floor.employees[name.to_sym] = FloorManager::Employee::Unique.from_dsl(klass_name, &block)
+ @floor.employees[name.to_sym] =
+ FloorManager::Employee::Unique.from_dsl(klass_name, @namespace, &block)
end
def any(name, opts={}, &block)
klass_name = opts[:class] || name
- @floor.employees[name.to_sym] = FloorManager::Employee::Template.from_dsl(klass_name, &block)
+ @floor.employees[name.to_sym] =
+ FloorManager::Employee::Template.from_dsl(klass_name, @namespace, &block)
end
def object
@floor
end
end
- def self.from_dsl(&block)
- DSL.new(&block).object
+ def self.from_dsl(options, &block)
+ DSL.new(options, &block).object
end
attr_reader :employees
21 spec/acceptance/floor_manager_spec.rb
View
@@ -199,4 +199,25 @@
it { should < 10 }
end
end
+ describe 'namespaces' do
+ module Foo
+ class Bar < Spy
+ end
+ end
+
+ let(:env) {
+ FloorManager.define :any, :namespace => Foo do |m|
+ any :bar do
+ name 'bar'
+ end
+ end
+
+ FloorManager.get(:any)
+ }
+ let(:bar) { env.bar }
+
+ it "should construct an instance of Foo::Bar" do
+ bar.should be_a(Foo::Bar)
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.