public
Description: StrokeDB is an embeddable distributed document database written in Ruby
Homepage: http://strokedb.com/
Clone URL: git://github.com/yrashk/strokedb.git
Metas without constant initial implementation [#30 state:resolved]
yrashk (author)
Tue Apr 29 22:18:03 -0700 2008
commit  591e7fe8a219f16de2f3428dc01950a67c231e90
tree    01b5cb648758b55a46021200c8de2b8d9949f1fd
parent  78c6023fa1c1c046a503e3854255f574d12bce98
...
31
32
33
 
 
 
34
35
36
...
31
32
33
34
35
36
37
38
39
0
@@ -31,6 +31,9 @@ class String
0
   end
0
 
0
   def constantize
0
+    if /^meta:/ =~ self
0
+      return StrokeDB::META_CACHE[Meta.make_uuid_from_fullname(self)]
0
+    end
0
     unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
0
       raise NameError, "#{self.inspect} is not a valid constant name!"
0
     end
...
344
345
346
347
348
349
350
...
686
687
688
689
 
 
 
 
 
690
691
692
693
694
695
 
 
 
 
 
696
697
698
...
344
345
346
 
347
348
349
...
685
686
687
 
688
689
690
691
692
693
694
695
696
697
 
698
699
700
701
702
703
704
705
0
@@ -344,7 +344,6 @@ module StrokeDB
0
       collect_meta_modules(store, raw_slots['meta']).each do |meta_module|
0
         unless doc.is_a? meta_module
0
           doc.extend(meta_module)
0
-
0
           meta_module.send!(:setup_callbacks, doc) rescue nil
0
         end
0
       end
0
@@ -686,13 +685,21 @@ module StrokeDB
0
         if m = store.find($1, $2)
0
           mod = Module.find_by_nsurl(m[:nsurl])
0
           mod = nil if mod == Module
0
-          meta_names << (mod ? mod.name : "") + "::" + m[:name]
0
+          if (mod.nil? && Object.constants.include?(m[:name])) || (mod && mod.constants.include?(m[:name]))
0
+            meta_names << (mod ? mod.name : "") + "::" + m[:name]
0
+          else
0
+            meta_names << Meta.resolve_uuid_name(m[:nsurl],m[:name])
0
+          end
0
         end
0
       when DOCREF
0
         if m = store.find($1)
0
           mod = Module.find_by_nsurl(m[:nsurl])
0
           mod = nil if mod == Module
0
-          meta_names << (mod ? mod.name : "") + "::" + m[:name]
0
+          if (mod.nil? && Object.constants.include?(m[:name])) || (mod && mod.constants.include?(m[:name]))
0
+            meta_names << (mod ? mod.name : "") + "::" + m[:name]
0
+          else
0
+            meta_names << Meta.resolve_uuid_name(m[:nsurl],m[:name])
0
+          end
0
         end
0
       when Array
0
         meta_names = meta.map { |m| collect_meta_modules(store, m) }.flatten
...
1
 
 
 
2
3
4
...
20
21
22
23
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
25
26
27
...
44
45
46
47
48
 
 
 
 
 
 
 
49
50
51
...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
...
227
228
229
230
231
232
233
234
...
237
238
239
240
 
241
242
243
...
1
2
3
4
5
6
7
...
23
24
25
 
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
...
60
61
62
 
 
63
64
65
66
67
68
69
70
71
72
...
86
87
88
 
 
 
 
 
 
 
 
89
90
91
...
240
241
242
 
 
243
244
245
...
248
249
250
 
251
252
253
254
0
@@ -1,4 +1,7 @@
0
 module StrokeDB
0
+  
0
+  META_CACHE = {}
0
+  
0
   # Meta is basically a type. Imagine the following document:
0
   #
0
   # some_apple:
0
@@ -20,8 +23,21 @@ module StrokeDB
0
   #
0
   # Document class will be extended by modules Fruit and Product.
0
   module Meta
0
-
0
+    
0
     class << self
0
+
0
+      def resolve_uuid_name(nsurl,name)
0
+        "meta:#{nsurl}##{name}"
0
+      end
0
+
0
+      def make_uuid_from_fullname(full_name)
0
+        StrokeDB::Util.sha1_uuid(full_name)
0
+      end
0
+      
0
+      def make_uuid(nsurl, name)
0
+        StrokeDB::Util.sha1_uuid("meta:#{nsurl}##{name}")
0
+      end
0
+      
0
       def new(*args, &block)
0
         mod = Module.new
0
         args = args.unshift(nil) if args.empty? || args.first.is_a?(Hash)
0
@@ -44,8 +60,13 @@ module StrokeDB
0
           initialize_coercions
0
           initialize_virtualizations
0
         end
0
-        if meta_name = extract_meta_name(*args)
0
-          Object.const_set(meta_name, mod)
0
+        if name = args.last.stringify_keys['name']
0
+          META_CACHE[make_uuid(args.last.stringify_keys['nsurl'],args.last.stringify_keys['name'])] = mod 
0
+          mod.instance_eval %{
0
+            def name 
0
+              '#{name}'
0
+            end
0
+          }
0
         end
0
         mod
0
       end
0
@@ -65,14 +86,6 @@ module StrokeDB
0
         @uuid ||= ::Util.sha1_uuid("meta:#{StrokeDB.nsurl}##{Meta.name.demodulize}")
0
       end
0
 
0
-      def extract_meta_name(*args)
0
-        if args.first.is_a?(Hash)
0
-          args.first[:name]
0
-        else
0
-          args[1][:name] unless args.empty?
0
-        end
0
-      end
0
-
0
     end
0
 
0
     def +(meta)
0
@@ -227,8 +240,6 @@ module StrokeDB
0
       metadocs.size > 1 ? metadocs.inject { |a, b| a + b }.make_immutable! : metadocs.first
0
     end
0
     
0
-    private
0
-
0
     def make_document(store=nil)
0
       raise NoDefaultStoreError.new unless store ||= StrokeDB.default_store
0
       @meta_initialization_procs.each {|proc| proc.call }.clear
0
@@ -237,7 +248,7 @@ module StrokeDB
0
       values[:meta] = Meta.document(store)
0
       values[:name] ||= name.demodulize
0
       values[:nsurl] ||= name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl 
0
-      values[:uuid] ||= ::Util.sha1_uuid("meta:#{values[:nsurl]}##{values[:name]}") if values[:name]
0
+      values[:uuid] ||= Meta.make_uuid(values[:nsurl],values[:name]) if values[:name]
0
       
0
       if meta_doc = find_meta_doc(values, store)
0
         values[:version] = meta_doc.version
...
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
...
13
14
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
17
18
0
@@ -13,23 +13,6 @@ describe "Meta meta" do
0
 
0
 end
0
 
0
-describe "Meta meta instantiation" do
0
-
0
-  before(:each) do
0
-    # @store = mock("store")
0
-    # StrokeDB.stub!(:default_store).and_return(@store)
0
-    setup_default_store
0
-    Object.send!(:remove_const,'SomeName') if defined?(SomeName)
0
-    @meta = Meta.new(:name => "SomeName")
0
-  end
0
-
0
-  it "should create new meta module and bind it to name passed" do
0
-    @meta.should be_a_kind_of(Meta)
0
-    SomeName.should == @meta
0
-  end
0
-
0
-end
0
-
0
 describe "Meta meta instantiation with block specified" do
0
   
0
   before(:each) do
...
32
33
34
35
 
36
37
38
...
162
163
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
166
167
...
254
255
256
257
258
259
260
...
271
272
273
274
 
275
276
277
...
32
33
34
 
35
36
37
38
...
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
...
281
282
283
 
284
285
286
...
297
298
299
 
300
301
302
303
0
@@ -32,7 +32,7 @@ describe "Meta module", :shared => true do
0
     new_doc = nil
0
     2.times do |i|
0
       Object.send!(:remove_const,'SomeName') if defined?(SomeName)
0
-      @meta = Meta.new(:name => "SomeName", :description => "Something")  
0
+      SomeName = Meta.new(:description => "Something")  
0
       new_doc = SomeName.document
0
     end
0
     new_doc.uuid.should == doc.uuid
0
@@ -162,6 +162,33 @@ describe "Meta module without name" do
0
   end
0
 end
0
 
0
+
0
+describe "Meta module without constant definition" do
0
+  
0
+  before(:each) do
0
+    setup_default_store
0
+    setup_index
0
+    @some_name = Meta.new(:name => 'SomeName') do
0
+      def some
0
+      end
0
+    end
0
+  end
0
+  
0
+  it "should not set respective constant" do
0
+    defined?(SomeName).should be_nil
0
+  end
0
+  
0
+  it "should have its name constantizeable anyway" do
0
+    Meta.resolve_uuid_name("","SomeName").constantize.should == @some_name
0
+  end
0
+
0
+  it "should be loaded into document on its load" do
0
+    doc = @some_name.create!.reload
0
+    doc.should respond_to(:some)
0
+  end
0
+
0
+end
0
+
0
 describe "Meta module within no module" do
0
   
0
   before(:each) do
0
@@ -254,7 +281,6 @@ describe "Meta module with on_load callback" do
0
   before(:each) do
0
     setup_default_store
0
     setup_index
0
-    
0
     Object.send!(:remove_const,'SomeName') if defined?(SomeName)
0
     SomeName = Meta.new do
0
       on_load do |obj|
0
@@ -271,7 +297,7 @@ describe "Meta module with on_load callback" do
0
   it "should receive this callback on document load" do
0
     doc = SomeName.create!
0
     Kernel.should_receive(:on_load_called).with(false)
0
-    SomeName.find(doc.uuid)
0
+    d = SomeName.find(doc.uuid)
0
   end
0
   
0
   

Comments