| |   |
| 344 | 344 | collect_meta_modules(store, raw_slots['meta']).each do |meta_module| |
| 345 | 345 | unless doc.is_a? meta_module |
| 346 | 346 | doc.extend(meta_module) |
| 347 | | |
| 348 | 347 | meta_module.send!(:setup_callbacks, doc) rescue nil |
| 349 | 348 | end |
| 350 | 349 | end |
| … | … | |
| 685 | 685 | if m = store.find($1, $2) |
| 686 | 686 | mod = Module.find_by_nsurl(m[:nsurl]) |
| 687 | 687 | mod = nil if mod == Module |
| 688 | | meta_names << (mod ? mod.name : "") + "::" + m[:name] |
| 688 | if (mod.nil? && Object.constants.include?(m[:name])) || (mod && mod.constants.include?(m[:name])) |
| 689 | meta_names << (mod ? mod.name : "") + "::" + m[:name] |
| 690 | else |
| 691 | meta_names << Meta.resolve_uuid_name(m[:nsurl],m[:name]) |
| 692 | end |
| 689 | 693 | end |
| 690 | 694 | when DOCREF |
| 691 | 695 | if m = store.find($1) |
| 692 | 696 | mod = Module.find_by_nsurl(m[:nsurl]) |
| 693 | 697 | mod = nil if mod == Module |
| 694 | | meta_names << (mod ? mod.name : "") + "::" + m[:name] |
| 698 | if (mod.nil? && Object.constants.include?(m[:name])) || (mod && mod.constants.include?(m[:name])) |
| 699 | meta_names << (mod ? mod.name : "") + "::" + m[:name] |
| 700 | else |
| 701 | meta_names << Meta.resolve_uuid_name(m[:nsurl],m[:name]) |
| 702 | end |
| 695 | 703 | end |
| 696 | 704 | when Array |
| 697 | 705 | meta_names = meta.map { |m| collect_meta_modules(store, m) }.flatten |
| toggle raw diff |
--- a/lib/strokedb/document.rb
+++ b/lib/strokedb/document.rb
@@ -344,7 +344,6 @@ module StrokeDB
collect_meta_modules(store, raw_slots['meta']).each do |meta_module|
unless doc.is_a? meta_module
doc.extend(meta_module)
-
meta_module.send!(:setup_callbacks, doc) rescue nil
end
end
@@ -686,13 +685,21 @@ module StrokeDB
if m = store.find($1, $2)
mod = Module.find_by_nsurl(m[:nsurl])
mod = nil if mod == Module
- meta_names << (mod ? mod.name : "") + "::" + m[:name]
+ if (mod.nil? && Object.constants.include?(m[:name])) || (mod && mod.constants.include?(m[:name]))
+ meta_names << (mod ? mod.name : "") + "::" + m[:name]
+ else
+ meta_names << Meta.resolve_uuid_name(m[:nsurl],m[:name])
+ end
end
when DOCREF
if m = store.find($1)
mod = Module.find_by_nsurl(m[:nsurl])
mod = nil if mod == Module
- meta_names << (mod ? mod.name : "") + "::" + m[:name]
+ if (mod.nil? && Object.constants.include?(m[:name])) || (mod && mod.constants.include?(m[:name]))
+ meta_names << (mod ? mod.name : "") + "::" + m[:name]
+ else
+ meta_names << Meta.resolve_uuid_name(m[:nsurl],m[:name])
+ end
end
when Array
meta_names = meta.map { |m| collect_meta_modules(store, m) }.flatten |
| |   |
| 1 | 1 | module StrokeDB |
| 2 | |
| 3 | META_CACHE = {} |
| 4 | |
| 2 | 5 | # Meta is basically a type. Imagine the following document: |
| 3 | 6 | # |
| 4 | 7 | # some_apple: |
| … | … | |
| 23 | 23 | # |
| 24 | 24 | # Document class will be extended by modules Fruit and Product. |
| 25 | 25 | module Meta |
| 26 | | |
| 26 | |
| 27 | 27 | class << self |
| 28 | |
| 29 | def resolve_uuid_name(nsurl,name) |
| 30 | "meta:#{nsurl}##{name}" |
| 31 | end |
| 32 | |
| 33 | def make_uuid_from_fullname(full_name) |
| 34 | StrokeDB::Util.sha1_uuid(full_name) |
| 35 | end |
| 36 | |
| 37 | def make_uuid(nsurl, name) |
| 38 | StrokeDB::Util.sha1_uuid("meta:#{nsurl}##{name}") |
| 39 | end |
| 40 | |
| 28 | 41 | def new(*args, &block) |
| 29 | 42 | mod = Module.new |
| 30 | 43 | args = args.unshift(nil) if args.empty? || args.first.is_a?(Hash) |
| … | … | |
| 60 | 60 | initialize_coercions |
| 61 | 61 | initialize_virtualizations |
| 62 | 62 | end |
| 63 | | if meta_name = extract_meta_name(*args) |
| 64 | | Object.const_set(meta_name, mod) |
| 63 | if name = args.last.stringify_keys['name'] |
| 64 | META_CACHE[make_uuid(args.last.stringify_keys['nsurl'],args.last.stringify_keys['name'])] = mod |
| 65 | mod.instance_eval %{ |
| 66 | def name |
| 67 | '#{name}' |
| 68 | end |
| 69 | } |
| 65 | 70 | end |
| 66 | 71 | mod |
| 67 | 72 | end |
| … | … | |
| 86 | 86 | @uuid ||= ::Util.sha1_uuid("meta:#{StrokeDB.nsurl}##{Meta.name.demodulize}") |
| 87 | 87 | end |
| 88 | 88 | |
| 89 | | def extract_meta_name(*args) |
| 90 | | if args.first.is_a?(Hash) |
| 91 | | args.first[:name] |
| 92 | | else |
| 93 | | args[1][:name] unless args.empty? |
| 94 | | end |
| 95 | | end |
| 96 | | |
| 97 | 89 | end |
| 98 | 90 | |
| 99 | 91 | def +(meta) |
| … | … | |
| 240 | 240 | metadocs.size > 1 ? metadocs.inject { |a, b| a + b }.make_immutable! : metadocs.first |
| 241 | 241 | end |
| 242 | 242 | |
| 243 | | private |
| 244 | | |
| 245 | 243 | def make_document(store=nil) |
| 246 | 244 | raise NoDefaultStoreError.new unless store ||= StrokeDB.default_store |
| 247 | 245 | @meta_initialization_procs.each {|proc| proc.call }.clear |
| … | … | |
| 248 | 248 | values[:meta] = Meta.document(store) |
| 249 | 249 | values[:name] ||= name.demodulize |
| 250 | 250 | values[:nsurl] ||= name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl |
| 251 | | values[:uuid] ||= ::Util.sha1_uuid("meta:#{values[:nsurl]}##{values[:name]}") if values[:name] |
| 251 | values[:uuid] ||= Meta.make_uuid(values[:nsurl],values[:name]) if values[:name] |
| 252 | 252 | |
| 253 | 253 | if meta_doc = find_meta_doc(values, store) |
| 254 | 254 | values[:version] = meta_doc.version |
| toggle raw diff |
--- a/lib/strokedb/document/meta.rb
+++ b/lib/strokedb/document/meta.rb
@@ -1,4 +1,7 @@
module StrokeDB
+
+ META_CACHE = {}
+
# Meta is basically a type. Imagine the following document:
#
# some_apple:
@@ -20,8 +23,21 @@ module StrokeDB
#
# Document class will be extended by modules Fruit and Product.
module Meta
-
+
class << self
+
+ def resolve_uuid_name(nsurl,name)
+ "meta:#{nsurl}##{name}"
+ end
+
+ def make_uuid_from_fullname(full_name)
+ StrokeDB::Util.sha1_uuid(full_name)
+ end
+
+ def make_uuid(nsurl, name)
+ StrokeDB::Util.sha1_uuid("meta:#{nsurl}##{name}")
+ end
+
def new(*args, &block)
mod = Module.new
args = args.unshift(nil) if args.empty? || args.first.is_a?(Hash)
@@ -44,8 +60,13 @@ module StrokeDB
initialize_coercions
initialize_virtualizations
end
- if meta_name = extract_meta_name(*args)
- Object.const_set(meta_name, mod)
+ if name = args.last.stringify_keys['name']
+ META_CACHE[make_uuid(args.last.stringify_keys['nsurl'],args.last.stringify_keys['name'])] = mod
+ mod.instance_eval %{
+ def name
+ '#{name}'
+ end
+ }
end
mod
end
@@ -65,14 +86,6 @@ module StrokeDB
@uuid ||= ::Util.sha1_uuid("meta:#{StrokeDB.nsurl}##{Meta.name.demodulize}")
end
- def extract_meta_name(*args)
- if args.first.is_a?(Hash)
- args.first[:name]
- else
- args[1][:name] unless args.empty?
- end
- end
-
end
def +(meta)
@@ -227,8 +240,6 @@ module StrokeDB
metadocs.size > 1 ? metadocs.inject { |a, b| a + b }.make_immutable! : metadocs.first
end
- private
-
def make_document(store=nil)
raise NoDefaultStoreError.new unless store ||= StrokeDB.default_store
@meta_initialization_procs.each {|proc| proc.call }.clear
@@ -237,7 +248,7 @@ module StrokeDB
values[:meta] = Meta.document(store)
values[:name] ||= name.demodulize
values[:nsurl] ||= name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl
- values[:uuid] ||= ::Util.sha1_uuid("meta:#{values[:nsurl]}##{values[:name]}") if values[:name]
+ values[:uuid] ||= Meta.make_uuid(values[:nsurl],values[:name]) if values[:name]
if meta_doc = find_meta_doc(values, store)
values[:version] = meta_doc.version |
| |   |
| 13 | 13 | |
| 14 | 14 | end |
| 15 | 15 | |
| 16 | | describe "Meta meta instantiation" do |
| 17 | | |
| 18 | | before(:each) do |
| 19 | | # @store = mock("store") |
| 20 | | # StrokeDB.stub!(:default_store).and_return(@store) |
| 21 | | setup_default_store |
| 22 | | Object.send!(:remove_const,'SomeName') if defined?(SomeName) |
| 23 | | @meta = Meta.new(:name => "SomeName") |
| 24 | | end |
| 25 | | |
| 26 | | it "should create new meta module and bind it to name passed" do |
| 27 | | @meta.should be_a_kind_of(Meta) |
| 28 | | SomeName.should == @meta |
| 29 | | end |
| 30 | | |
| 31 | | end |
| 32 | | |
| 33 | 16 | describe "Meta meta instantiation with block specified" do |
| 34 | 17 | |
| 35 | 18 | before(:each) do |
| toggle raw diff |
--- a/spec/lib/strokedb/document/meta_meta_spec.rb
+++ b/spec/lib/strokedb/document/meta_meta_spec.rb
@@ -13,23 +13,6 @@ describe "Meta meta" do
end
-describe "Meta meta instantiation" do
-
- before(:each) do
- # @store = mock("store")
- # StrokeDB.stub!(:default_store).and_return(@store)
- setup_default_store
- Object.send!(:remove_const,'SomeName') if defined?(SomeName)
- @meta = Meta.new(:name => "SomeName")
- end
-
- it "should create new meta module and bind it to name passed" do
- @meta.should be_a_kind_of(Meta)
- SomeName.should == @meta
- end
-
-end
-
describe "Meta meta instantiation with block specified" do
before(:each) do |
| |   |
| 32 | 32 | new_doc = nil |
| 33 | 33 | 2.times do |i| |
| 34 | 34 | Object.send!(:remove_const,'SomeName') if defined?(SomeName) |
| 35 | | @meta = Meta.new(:name => "SomeName", :description => "Something") |
| 35 | SomeName = Meta.new(:description => "Something") |
| 36 | 36 | new_doc = SomeName.document |
| 37 | 37 | end |
| 38 | 38 | new_doc.uuid.should == doc.uuid |
| … | … | |
| 162 | 162 | end |
| 163 | 163 | end |
| 164 | 164 | |
| 165 | |
| 166 | describe "Meta module without constant definition" do |
| 167 | |
| 168 | before(:each) do |
| 169 | setup_default_store |
| 170 | setup_index |
| 171 | @some_name = Meta.new(:name => 'SomeName') do |
| 172 | def some |
| 173 | end |
| 174 | end |
| 175 | end |
| 176 | |
| 177 | it "should not set respective constant" do |
| 178 | defined?(SomeName).should be_nil |
| 179 | end |
| 180 | |
| 181 | it "should have its name constantizeable anyway" do |
| 182 | Meta.resolve_uuid_name("","SomeName").constantize.should == @some_name |
| 183 | end |
| 184 | |
| 185 | it "should be loaded into document on its load" do |
| 186 | doc = @some_name.create!.reload |
| 187 | doc.should respond_to(:some) |
| 188 | end |
| 189 | |
| 190 | end |
| 191 | |
| 165 | 192 | describe "Meta module within no module" do |
| 166 | 193 | |
| 167 | 194 | before(:each) do |
| … | … | |
| 281 | 281 | before(:each) do |
| 282 | 282 | setup_default_store |
| 283 | 283 | setup_index |
| 284 | | |
| 285 | 284 | Object.send!(:remove_const,'SomeName') if defined?(SomeName) |
| 286 | 285 | SomeName = Meta.new do |
| 287 | 286 | on_load do |obj| |
| … | … | |
| 297 | 297 | it "should receive this callback on document load" do |
| 298 | 298 | doc = SomeName.create! |
| 299 | 299 | Kernel.should_receive(:on_load_called).with(false) |
| 300 | | SomeName.find(doc.uuid) |
| 300 | d = SomeName.find(doc.uuid) |
| 301 | 301 | end |
| 302 | 302 | |
| 303 | 303 | |
| toggle raw diff |
--- a/spec/lib/strokedb/document/meta_spec.rb
+++ b/spec/lib/strokedb/document/meta_spec.rb
@@ -32,7 +32,7 @@ describe "Meta module", :shared => true do
new_doc = nil
2.times do |i|
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
- @meta = Meta.new(:name => "SomeName", :description => "Something")
+ SomeName = Meta.new(:description => "Something")
new_doc = SomeName.document
end
new_doc.uuid.should == doc.uuid
@@ -162,6 +162,33 @@ describe "Meta module without name" do
end
end
+
+describe "Meta module without constant definition" do
+
+ before(:each) do
+ setup_default_store
+ setup_index
+ @some_name = Meta.new(:name => 'SomeName') do
+ def some
+ end
+ end
+ end
+
+ it "should not set respective constant" do
+ defined?(SomeName).should be_nil
+ end
+
+ it "should have its name constantizeable anyway" do
+ Meta.resolve_uuid_name("","SomeName").constantize.should == @some_name
+ end
+
+ it "should be loaded into document on its load" do
+ doc = @some_name.create!.reload
+ doc.should respond_to(:some)
+ end
+
+end
+
describe "Meta module within no module" do
before(:each) do
@@ -254,7 +281,6 @@ describe "Meta module with on_load callback" do
before(:each) do
setup_default_store
setup_index
-
Object.send!(:remove_const,'SomeName') if defined?(SomeName)
SomeName = Meta.new do
on_load do |obj|
@@ -271,7 +297,7 @@ describe "Meta module with on_load callback" do
it "should receive this callback on document load" do
doc = SomeName.create!
Kernel.should_receive(:on_load_called).with(false)
- SomeName.find(doc.uuid)
+ d = SomeName.find(doc.uuid)
end
|