Commit 189454807c0529da8e975b02d826c727542a5691

Module#nsurl has been introduced [#24 state:resolved]

Commit diff

lib/strokedb.rb

 
7070 class NoDefaultStoreError < Exception ; end
7171end
7272
73require 'strokedb/nsurl'
7374require 'strokedb/util'
7475require 'strokedb/document'
7576require 'strokedb/config'
toggle raw diff

lib/strokedb/core_ext/string.rb

 
2525 gsub(/^.*::/, '')
2626 end
2727
28 def modulize
29 return '' unless include?('::') && self[0,2] != '::'
30 self.gsub(/^(.+)::(#{demodulize})$/,'\\1')
31 end
32
2833 def constantize
2934 unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
3035 raise NameError, "#{self.inspect} is not a valid constant name!"
3136 end
32
3337 Object.module_eval("::#{$1}", __FILE__, __LINE__)
3438 end
3539
toggle raw diff

lib/strokedb/document.rb

 
684684 case meta
685685 when VERSIONREF
686686 if m = store.find($1, $2)
687 meta_names << m[:name]
687 mod = Module.find_by_nsurl(m[:nsurl])
688 mod = nil if mod == Module
689 meta_names << (mod ? mod.name : "") + "::" + m[:name]
688690 end
689691 when DOCREF
690692 if m = store.find($1)
691 meta_names << m[:name]
693 mod = Module.find_by_nsurl(m[:nsurl])
694 mod = nil if mod == Module
695 meta_names << (mod ? mod.name : "") + "::" + m[:name]
692696 end
693697 when Array
694698 meta_names = meta.map { |m| collect_meta_modules(store, m) }.flatten
toggle raw diff

lib/strokedb/document/delete.rb

 
33 class DocumentDeletionError < StandardError
44 end
55
6 DeletedDocument = Meta.new(:nsurl => STROKEDB_NSURL) do
6 DeletedDocument = Meta.new do
77 on_load do |doc|
88 doc.make_immutable!
99 end
toggle raw diff

lib/strokedb/document/meta.rb

 
2222 module Meta
2323
2424 class << self
25
26 def default_nsurl
27 @default_nsurl ||= ""
28 end
29
30 def default_nsurl=(nsurl)
31 @default_nsurl = nsurl
32 end
33
3425 def new(*args, &block)
3526 mod = Module.new
3627 args = args.unshift(nil) if args.empty? || args.first.is_a?(Hash)
5353 def document(store=nil)
5454 raise NoDefaultStoreError.new unless store ||= StrokeDB.default_store
5555 unless meta_doc = store.find(uuid)
56 meta_doc = Document.create!(store, :name => Meta.name, :uuid => uuid, :nsurl => STROKEDB_NSURL)
56 meta_doc = Document.create!(store, :name => Meta.name.demodulize, :uuid => uuid, :nsurl => StrokeDB.nsurl)
5757 end
5858 meta_doc
5959 end
6262 private
6363
6464 def uuid
65 @uuid ||= ::Util.sha1_uuid("meta:#{STROKEDB_NSURL}##{Meta.name}")
65 @uuid ||= ::Util.sha1_uuid("meta:#{StrokeDB.nsurl}##{Meta.name.demodulize}")
6666 end
6767
6868 def extract_meta_name(*args)
234234
235235 values = @args.clone.select{|a| a.is_a?(Hash) }.first
236236 values[:meta] = Meta.document(store)
237 values[:name] ||= name
238 values[:nsurl] ||= Meta.default_nsurl
237 values[:name] ||= name.demodulize
238 values[:nsurl] ||= name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl
239239 values[:uuid] ||= ::Util.sha1_uuid("meta:#{values[:nsurl]}##{values[:name]}") if values[:name]
240240
241241 if meta_doc = find_meta_doc(values, store)
toggle raw diff

lib/strokedb/store.rb

 
11module StrokeDB
22
3 StoreInfo = Meta.new(:nsurl => STROKEDB_NSURL)
3 StoreInfo = Meta.new
44
55 class Store
66 include Enumerable
toggle raw diff

lib/strokedb/sync/diff.rb

 
6868 end
6969 end
7070
71 Diff = Meta.new(:nsurl => STROKEDB_NSURL) do
71 Diff = Meta.new do
7272
7373 on_initialization do |diff|
7474 diff.added_slots = {} unless diff[:added_slots]
toggle raw diff

lib/strokedb/sync/store_sync.rb

 
11module StrokeDB
22
3 SynchronizationReport = Meta.new(:nsurl => STROKEDB_NSURL) do
3 SynchronizationReport = Meta.new do
44 on_new_document do |report|
55 report.conflicts = []
66 report.added_documents = []
99 end
1010 end
1111
12 SynchronizationConflict = Meta.new(:nsurl => STROKEDB_NSURL) do
12 SynchronizationConflict = Meta.new do
1313 def resolve!
1414 # by default, do nothing
1515 end
toggle raw diff

lib/strokedb/view.rb

 
11module StrokeDB
2 View = Meta.new(:nsurl => STROKEDB_NSURL) do
2 View = Meta.new do
33 attr_accessor :map_with_proc
44 attr_reader :reduce_with_proc
55
2222 end
2323
2424 end
25 ViewCut = Meta.new(:nsurl => STROKEDB_NSURL) do
25 ViewCut = Meta.new do
2626
2727 on_new_document do |cut|
2828 cut.instance_eval do
toggle raw diff

spec/lib/strokedb/core_ext/string_spec.rb

 
55 "lib"/"core_ext".should == "lib/core_ext"
66 "lib/core_ext"/"foo".should == "lib/core_ext/foo"
77 end
8end
9
10describe "String#modulize" do
11
12 it "if there is no module, leave nothing" do
13 "A".modulize.should == ""
14 "::A".modulize.should == ""
15 end
16
17 it "should leave single module" do
18 "A::B".modulize.should == "A"
19 end
20
21 it "should leave multiple modules" do
22 "A::B::C".modulize.should == "A::B"
23 end
24
825end
toggle raw diff

spec/lib/strokedb/document/meta_meta_spec.rb

 
1010 it "should have nsurl http://strokedb.com/" do
1111 Meta.document.nsurl.should == STROKEDB_NSURL
1212 end
13
14 it "should have blank default nsurl by default" do
15 Meta.default_nsurl.should be_blank
16 end
17
18 it "should be able to configure new default nsurl" do
19 Meta.default_nsurl = "http://mycoolapp.com"
20 Meta.default_nsurl.should == "http://mycoolapp.com"
21 Meta.default_nsurl = ""
22 end
2313
2414end
2515
toggle raw diff

spec/lib/strokedb/document/meta_spec.rb

 
22
33describe "Meta module", :shared => true do
44
5 it "should use Meta.default_nsurl if nsurl is not specified" do
6 Meta.default_nsurl = "http://some/"
7 SomeName.document.nsurl.should == "http://some/"
8 end
9
10 it "should not use Meta.default_nsurl if nsurl is specified" do
11 Meta.default_nsurl = "http://some/"
12 Object.send!(:remove_const,'SomeName') if defined?(SomeName)
13 SomeName = Meta.new(:nsurl => "http://another/")
14 SomeName.document.nsurl.should == "http://another/"
15 end
16
175 it "should be able to instantiate new Document which is also SomeName" do
186 obj = SomeName.new
197 obj.should be_a_kind_of(Document)
162162 end
163163end
164164
165describe "Meta module within no module" do
166
167 before(:each) do
168 setup_default_store
169 setup_index
170
171 Object.send!(:remove_const,'SomeName') if defined?(SomeName)
172 end
173
174 it "should use Module.nsurl by default" do
175 Module.nsurl "test"
176 SomeName = Meta.new
177 SomeName.document.nsurl.should == Module.nsurl
178 Module.nsurl ''
179 end
180
181 it "should not use Module.nsurl if nsurl is specified" do
182 Module.nsurl "test"
183 SomeName = Meta.new(:nsurl => 'passed')
184 SomeName.document.nsurl.should == 'passed'
185 Module.nsurl ''
186 end
187
188end
189
190
191describe "Meta module within module" do
192
193 before(:each) do
194 setup_default_store
195 setup_index
196 module A
197 nsurl "some url"
198 end
199 A.send!(:remove_const,'SomeName') if defined?(A::SomeName)
200 end
201
202 it "should use Module.nsurl by default" do
203 module A
204 SomeName = Meta.new
205 end
206 A::SomeName.document.nsurl.should == A.nsurl
207 end
208
209 it "should not use Module.nsurl if nsurl is specified" do
210 module A
211 SomeName = Meta.new(:nsurl => "nsurl")
212 end
213 A::SomeName.document.nsurl.should == "nsurl"
214 end
215
216end
165217describe "Meta module with on_initialization callback" do
166218
167219 before(:each) do
268268 doc = SomeName.new
269269 end
270270
271 it "should receive this callback on document load" do
271 it "should receive this callback on document load" do
272272 doc = SomeName.create!
273273 Kernel.should_receive(:on_load_called).with(false)
274274 SomeName.find(doc.uuid)
toggle raw diff