From b7df604dcd49be6f950d2cec48b7fb75421bcb44 Mon Sep 17 00:00:00 2001 From: Yurii Rashkovskii Date: Wed, 30 Apr 2008 11:37:43 +0300 Subject: [PATCH] Disallow nameless metas --- lib/strokedb/document/meta.rb | 6 +++- spec/lib/strokedb/document/meta_spec.rb | 6 ++-- .../lib/strokedb/document/validations_spec.rb | 36 +++++++++---------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/lib/strokedb/document/meta.rb b/lib/strokedb/document/meta.rb index a21a0989..f1e141e5 100644 --- a/lib/strokedb/document/meta.rb +++ b/lib/strokedb/document/meta.rb @@ -254,8 +254,12 @@ def make_document(store=nil) values = @args.clone.select{|a| a.is_a?(Hash) }.first values[:meta] = Meta.document(store) values[:name] ||= name.demodulize + + raise ArgumentError, "meta can't be nameless" if values[:name].blank? + values[:nsurl] ||= name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl - values[:uuid] ||= Meta.make_uuid(values[:nsurl],values[:name]) if values[:name] + values[:uuid] ||= Meta.make_uuid(values[:nsurl],values[:name]) + if meta_doc = find_meta_doc(values, store) values[:version] = meta_doc.version diff --git a/spec/lib/strokedb/document/meta_spec.rb b/spec/lib/strokedb/document/meta_spec.rb index cf5311a8..1ea36e82 100644 --- a/spec/lib/strokedb/document/meta_spec.rb +++ b/spec/lib/strokedb/document/meta_spec.rb @@ -161,8 +161,10 @@ @some_meta = Meta.new(:nsurl => "http://some/") end - it "should not have document's UUID v5 based on nsurl and name" do - @some_meta.document.uuid.should_not == Util.sha1_uuid('http://some/#SomeName') + it "should not be able to create a document" do + lambda do + @some_meta.document + end.should raise_error(ArgumentError) end end diff --git a/spec/lib/strokedb/document/validations_spec.rb b/spec/lib/strokedb/document/validations_spec.rb index 4421374a..b1e96a37 100644 --- a/spec/lib/strokedb/document/validations_spec.rb +++ b/spec/lib/strokedb/document/validations_spec.rb @@ -12,7 +12,7 @@ def validations_setup end def erroneous_stuff - Meta.new do + Meta.new(:name => "ErroneousStuff") do on_validation do |doc| doc.errors.add(:something, "123") doc.errors.add(:other, "456") @@ -383,28 +383,28 @@ def no_bang end it "should treat accepted value as valid" do - Meta.new { validates_acceptance_of :eula, :accept => "yep" }.new(:eula => "yep").should be_valid + Meta.new(:name => 'some') { validates_acceptance_of :eula, :accept => "yep" }.new(:eula => "yep").should be_valid end it "should treat not accepted value as invalid" do - Meta.new { validates_acceptance_of :eula, :accept => "yep" }.new(:eula => "nope").should_not be_valid + Meta.new(:name => 'some') { validates_acceptance_of :eula, :accept => "yep" }.new(:eula => "nope").should_not be_valid end it "should respect allow_nil" do - Meta.new { validates_acceptance_of :eula, :accept => "yep", :allow_nil => true }.new.should be_valid - Meta.new { validates_acceptance_of :eula, :accept => "yep", :allow_nil => false }.new.should_not be_valid + Meta.new(:name => 'some') { validates_acceptance_of :eula, :accept => "yep", :allow_nil => true }.new.should be_valid + Meta.new(:name => 'some') { validates_acceptance_of :eula, :accept => "yep", :allow_nil => false }.new.should_not be_valid end it "should set :allow_nil to true by default" do - Meta.new { validates_acceptance_of :eula, :accept => "yep" }.new.should be_valid + Meta.new(:name => 'some') { validates_acceptance_of :eula, :accept => "yep" }.new.should be_valid end it "should set :accept to \"1\" by default" do - Meta.new { validates_acceptance_of :eula }.new(:eula => "1").should be_valid + Meta.new(:name => 'some') { validates_acceptance_of :eula }.new(:eula => "1").should be_valid end it "should make a slot virtual" do - Foo = Meta.new { validates_acceptance_of :eula, :accept => "yep" } + Foo = Meta.new{ validates_acceptance_of :eula, :accept => "yep" } f = Foo.create!(:eula => "yep") Foo.find(f.uuid).has_slot?("eula").should_not be_true end @@ -417,23 +417,23 @@ def no_bang describe "options handling" do it "should raise ArgumentError when more than one range option is specified" do - arg_bang { Meta.new { validates_length_of :name, :is => 10, :maximum => 20 } } - arg_bang { Meta.new { validates_length_of :name, :is => 10, :within => 1..20 } } + arg_bang { Meta.new(:name => 'some') { validates_length_of :name, :is => 10, :maximum => 20 } } + arg_bang { Meta.new(:name => 'some') { validates_length_of :name, :is => 10, :within => 1..20 } } end it "should raise ArgumentError when no range option is specified" do - arg_bang { Meta.new { validates_length_of :name } } + arg_bang { Meta.new(:name => 'some') { validates_length_of :name } } end it "should raise ArgumentError when not Range given to :in or :within" do - arg_bang { Meta.new { validates_length_of :name, :in => 10 } } - arg_bang { Meta.new { validates_length_of :name, :within => "somewhere between one and a million" } } + arg_bang { Meta.new(:name => 'some') { validates_length_of :name, :in => 10 } } + arg_bang { Meta.new(:name => 'some') { validates_length_of :name, :within => "somewhere between one and a million" } } end it "should raise ArgumentError when something other than nonnegative Integer is given to :is, :minimum, :maximum" do %w(is minimum maximum).each do |arg| - arg_bang { Meta.new { validates_length_of :name, arg => "blah" } } - arg_bang { Meta.new { validates_length_of :name, arg => -1 } } + arg_bang { Meta.new(:name => 'some') { validates_length_of :name, arg => "blah" } } + arg_bang { Meta.new(:name => 'some') { validates_length_of :name, arg => -1 } } end end @@ -564,8 +564,8 @@ def arg_bang end it "should respect :allow_nil" do - Meta.new { validates_length_of :bar, :is => 10, :allow_nil => false }.new.should_not be_valid - Meta.new { validates_length_of :bar, :is => 10, :allow_nil => true }.new.should be_valid + Meta.new(:name => 'some') { validates_length_of :bar, :is => 10, :allow_nil => false }.new.should_not be_valid + Meta.new(:name => 'some') { validates_length_of :bar, :is => 10, :allow_nil => true }.new.should be_valid end it "should respect :allow_blank" do @@ -1027,7 +1027,7 @@ def arg_bang end it "should respect :allow_nil" do - Meta.new { validates_numericality_of :number, :allow_nil => true }.new.should be_valid + Meta.new(:name => 'some') { validates_numericality_of :number, :allow_nil => true }.new.should be_valid end describe "should allow for option combinations" do