lib/with_model/model.rb in with_model-1.2.1 vs lib/with_model/model.rb in with_model-1.2.2
- old
+ new
@@ -4,33 +4,33 @@
require 'with_model/methods'
require 'with_model/table'
module WithModel
class Model
+ OPTIONS = [:superclass].freeze
+ private_constant :OPTIONS
+
attr_writer :model_block, :table_block, :table_options
def initialize name, options = {}
+ validate_options!(options)
@name = name.to_sym
- @options = options
@model_block = nil
@table_block = nil
@table_options = {}
+ @superclass = options.fetch(:superclass, ActiveRecord::Base)
end
def create
table.create
- @model = Class.new(superclass) do
+ @model = Class.new(@superclass) do
extend WithModel::Methods
end
stubber.stub_const @model
setup_model
end
- def superclass
- @options.fetch(:superclass, ActiveRecord::Base)
- end
-
def destroy
stubber.unstub_const
remove_from_superclass_descendants
reset_dependencies_cache
table.destroy
@@ -66,9 +66,17 @@
def table
@table ||= Table.new table_name, @table_options, &@table_block
end
def table_name
- "with_model_#{@name.to_s.tableize}_#{$$}".freeze
+ uid = "#$$_#{Thread.current.object_id}"
+ "with_model_#{@name.to_s.tableize}_#{uid}".freeze
+ end
+
+ def validate_options!(options)
+ unknown_options = options.keys - OPTIONS
+ unless unknown_options.empty?
+ raise ArgumentError, "unknown options: #{unknown_options.inspect}"
+ end
end
end
end