Permalink
Browse files

Update support constants to work like toystore.

  • Loading branch information...
jnunemaker committed May 20, 2012
1 parent 13c813f commit 995057fa373d0c94021931f97dbb4c621c1e28ff
Showing with 22 additions and 20 deletions.
  1. +22 −20 spec/support/constants.rb
View
@@ -1,42 +1,44 @@
module Support
module Constants
- def self.included(base)
- base.extend(ClassMethods)
- end
+ extend ActiveSupport::Concern
module ClassMethods
def uses_constants(*constants)
- before { create_constants(*constants) }
+ before { create_constants *constants }
+ after { remove_constants *constants }
end
end
def create_constants(*constants)
- constants.each { |constant| create_constant(constant) }
+ constants.each { |constant| create_constant constant }
end
def remove_constants(*constants)
- constants.each { |constant| remove_constant(constant) }
+ constants.each { |constant| remove_constant constant }
end
- def create_constant(constant)
- remove_constant(constant)
- Kernel.const_set(constant, Model(constant))
+ def create_constant(constant, superclass=nil)
+ Object.const_set constant, Model(superclass)
end
def remove_constant(constant)
- Kernel.send(:remove_const, constant) if Kernel.const_defined?(constant)
+ if Object.const_defined?(constant)
+ Object.send :remove_const, constant
+ end
end
- def Model(name=nil)
- Class.new.tap do |model|
- model.class_eval """
- def self.name; '#{name}' end
- def self.to_s; '#{name}' end
- """ if name
-
- model.send :include, Toy::Mongo
- model.adapter :mongo, STORE
+ def Model(superclass=nil)
+ if superclass.nil?
+ Class.new {
+ include Toy::Mongo
+ adapter :mongo, STORE
+ }
+ else
+ Class.new(superclass) {
+ include Toy::Mongo
+ adapter :mongo, STORE
+ }
end
end
end
-end
+end

0 comments on commit 995057f

Please sign in to comment.