Commit 591e7fe8a219f16de2f3428dc01950a67c231e90

Metas without constant initial implementation [#30 state:resolved]

Commit diff

lib/strokedb/core_ext/string.rb

 
3131 end
3232
3333 def constantize
34 if /^meta:/ =~ self
35 return StrokeDB::META_CACHE[Meta.make_uuid_from_fullname(self)]
36 end
3437 unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
3538 raise NameError, "#{self.inspect} is not a valid constant name!"
3639 end
toggle raw diff

lib/strokedb/document.rb

 
344344 collect_meta_modules(store, raw_slots['meta']).each do |meta_module|
345345 unless doc.is_a? meta_module
346346 doc.extend(meta_module)
347
348347 meta_module.send!(:setup_callbacks, doc) rescue nil
349348 end
350349 end
685685 if m = store.find($1, $2)
686686 mod = Module.find_by_nsurl(m[:nsurl])
687687 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
689693 end
690694 when DOCREF
691695 if m = store.find($1)
692696 mod = Module.find_by_nsurl(m[:nsurl])
693697 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
695703 end
696704 when Array
697705 meta_names = meta.map { |m| collect_meta_modules(store, m) }.flatten
toggle raw diff

lib/strokedb/document/meta.rb

 
11module StrokeDB
2
3 META_CACHE = {}
4
25 # Meta is basically a type. Imagine the following document:
36 #
47 # some_apple:
2323 #
2424 # Document class will be extended by modules Fruit and Product.
2525 module Meta
26
26
2727 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
2841 def new(*args, &block)
2942 mod = Module.new
3043 args = args.unshift(nil) if args.empty? || args.first.is_a?(Hash)
6060 initialize_coercions
6161 initialize_virtualizations
6262 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 }
6570 end
6671 mod
6772 end
8686 @uuid ||= ::Util.sha1_uuid("meta:#{StrokeDB.nsurl}##{Meta.name.demodulize}")
8787 end
8888
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
9789 end
9890
9991 def +(meta)
240240 metadocs.size > 1 ? metadocs.inject { |a, b| a + b }.make_immutable! : metadocs.first
241241 end
242242
243 private
244
245243 def make_document(store=nil)
246244 raise NoDefaultStoreError.new unless store ||= StrokeDB.default_store
247245 @meta_initialization_procs.each {|proc| proc.call }.clear
248248 values[:meta] = Meta.document(store)
249249 values[:name] ||= name.demodulize
250250 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]
252252
253253 if meta_doc = find_meta_doc(values, store)
254254 values[:version] = meta_doc.version
toggle raw diff

spec/lib/strokedb/document/meta_meta_spec.rb

 
1313
1414end
1515
16describe "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
31end
32
3316describe "Meta meta instantiation with block specified" do
3417
3518 before(:each) do
toggle raw diff

spec/lib/strokedb/document/meta_spec.rb

 
3232 new_doc = nil
3333 2.times do |i|
3434 Object.send!(:remove_const,'SomeName') if defined?(SomeName)
35 @meta = Meta.new(:name => "SomeName", :description => "Something")
35 SomeName = Meta.new(:description => "Something")
3636 new_doc = SomeName.document
3737 end
3838 new_doc.uuid.should == doc.uuid
162162 end
163163end
164164
165
166describe "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
190end
191
165192describe "Meta module within no module" do
166193
167194 before(:each) do
281281 before(:each) do
282282 setup_default_store
283283 setup_index
284
285284 Object.send!(:remove_const,'SomeName') if defined?(SomeName)
286285 SomeName = Meta.new do
287286 on_load do |obj|
297297 it "should receive this callback on document load" do
298298 doc = SomeName.create!
299299 Kernel.should_receive(:on_load_called).with(false)
300 SomeName.find(doc.uuid)
300 d = SomeName.find(doc.uuid)
301301 end
302302
303303
toggle raw diff