| |   |
| 25 | 25 | gsub(/^.*::/, '') |
| 26 | 26 | end |
| 27 | 27 | |
| 28 | def modulize |
| 29 | return '' unless include?('::') && self[0,2] != '::' |
| 30 | self.gsub(/^(.+)::(#{demodulize})$/,'\\1') |
| 31 | end |
| 32 | |
| 28 | 33 | def constantize |
| 29 | 34 | unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self |
| 30 | 35 | raise NameError, "#{self.inspect} is not a valid constant name!" |
| 31 | 36 | end |
| 32 | | |
| 33 | 37 | Object.module_eval("::#{$1}", __FILE__, __LINE__) |
| 34 | 38 | end |
| 35 | 39 | |
| toggle raw diff |
--- a/lib/strokedb/core_ext/string.rb
+++ b/lib/strokedb/core_ext/string.rb
@@ -25,11 +25,15 @@ class String
gsub(/^.*::/, '')
end
+ def modulize
+ return '' unless include?('::') && self[0,2] != '::'
+ self.gsub(/^(.+)::(#{demodulize})$/,'\\1')
+ end
+
def constantize
unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
raise NameError, "#{self.inspect} is not a valid constant name!"
end
-
Object.module_eval("::#{$1}", __FILE__, __LINE__)
end
|
| |   |
| 684 | 684 | case meta |
| 685 | 685 | when VERSIONREF |
| 686 | 686 | 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] |
| 688 | 690 | end |
| 689 | 691 | when DOCREF |
| 690 | 692 | 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] |
| 692 | 696 | end |
| 693 | 697 | when Array |
| 694 | 698 | meta_names = meta.map { |m| collect_meta_modules(store, m) }.flatten |
| toggle raw diff |
--- a/lib/strokedb/document.rb
+++ b/lib/strokedb/document.rb
@@ -684,11 +684,15 @@ module StrokeDB
case meta
when VERSIONREF
if m = store.find($1, $2)
- meta_names << m[:name]
+ mod = Module.find_by_nsurl(m[:nsurl])
+ mod = nil if mod == Module
+ meta_names << (mod ? mod.name : "") + "::" + m[:name]
end
when DOCREF
if m = store.find($1)
- meta_names << m[:name]
+ mod = Module.find_by_nsurl(m[:nsurl])
+ mod = nil if mod == Module
+ meta_names << (mod ? mod.name : "") + "::" + m[:name]
end
when Array
meta_names = meta.map { |m| collect_meta_modules(store, m) }.flatten |
| |   |
| 22 | 22 | module Meta |
| 23 | 23 | |
| 24 | 24 | 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 | | |
| 34 | 25 | def new(*args, &block) |
| 35 | 26 | mod = Module.new |
| 36 | 27 | args = args.unshift(nil) if args.empty? || args.first.is_a?(Hash) |
| … | … | |
| 53 | 53 | def document(store=nil) |
| 54 | 54 | raise NoDefaultStoreError.new unless store ||= StrokeDB.default_store |
| 55 | 55 | 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) |
| 57 | 57 | end |
| 58 | 58 | meta_doc |
| 59 | 59 | end |
| … | … | |
| 62 | 62 | private |
| 63 | 63 | |
| 64 | 64 | def uuid |
| 65 | | @uuid ||= ::Util.sha1_uuid("meta:#{STROKEDB_NSURL}##{Meta.name}") |
| 65 | @uuid ||= ::Util.sha1_uuid("meta:#{StrokeDB.nsurl}##{Meta.name.demodulize}") |
| 66 | 66 | end |
| 67 | 67 | |
| 68 | 68 | def extract_meta_name(*args) |
| … | … | |
| 234 | 234 | |
| 235 | 235 | values = @args.clone.select{|a| a.is_a?(Hash) }.first |
| 236 | 236 | 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 |
| 239 | 239 | values[:uuid] ||= ::Util.sha1_uuid("meta:#{values[:nsurl]}##{values[:name]}") if values[:name] |
| 240 | 240 | |
| 241 | 241 | if meta_doc = find_meta_doc(values, store) |
| toggle raw diff |
--- a/lib/strokedb/document/meta.rb
+++ b/lib/strokedb/document/meta.rb
@@ -22,15 +22,6 @@ module StrokeDB
module Meta
class << self
-
- def default_nsurl
- @default_nsurl ||= ""
- end
-
- def default_nsurl=(nsurl)
- @default_nsurl = nsurl
- end
-
def new(*args, &block)
mod = Module.new
args = args.unshift(nil) if args.empty? || args.first.is_a?(Hash)
@@ -62,7 +53,7 @@ module StrokeDB
def document(store=nil)
raise NoDefaultStoreError.new unless store ||= StrokeDB.default_store
unless meta_doc = store.find(uuid)
- meta_doc = Document.create!(store, :name => Meta.name, :uuid => uuid, :nsurl => STROKEDB_NSURL)
+ meta_doc = Document.create!(store, :name => Meta.name.demodulize, :uuid => uuid, :nsurl => StrokeDB.nsurl)
end
meta_doc
end
@@ -71,7 +62,7 @@ module StrokeDB
private
def uuid
- @uuid ||= ::Util.sha1_uuid("meta:#{STROKEDB_NSURL}##{Meta.name}")
+ @uuid ||= ::Util.sha1_uuid("meta:#{StrokeDB.nsurl}##{Meta.name.demodulize}")
end
def extract_meta_name(*args)
@@ -243,8 +234,8 @@ module StrokeDB
values = @args.clone.select{|a| a.is_a?(Hash) }.first
values[:meta] = Meta.document(store)
- values[:name] ||= name
- values[:nsurl] ||= Meta.default_nsurl
+ 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]
if meta_doc = find_meta_doc(values, store) |
| |   |
| 1 | 1 | module StrokeDB |
| 2 | 2 | |
| 3 | | SynchronizationReport = Meta.new(:nsurl => STROKEDB_NSURL) do |
| 3 | SynchronizationReport = Meta.new do |
| 4 | 4 | on_new_document do |report| |
| 5 | 5 | report.conflicts = [] |
| 6 | 6 | report.added_documents = [] |
| … | … | |
| 9 | 9 | end |
| 10 | 10 | end |
| 11 | 11 | |
| 12 | | SynchronizationConflict = Meta.new(:nsurl => STROKEDB_NSURL) do |
| 12 | SynchronizationConflict = Meta.new do |
| 13 | 13 | def resolve! |
| 14 | 14 | # by default, do nothing |
| 15 | 15 | end |
| toggle raw diff |
--- a/lib/strokedb/sync/store_sync.rb
+++ b/lib/strokedb/sync/store_sync.rb
@@ -1,6 +1,6 @@
module StrokeDB
- SynchronizationReport = Meta.new(:nsurl => STROKEDB_NSURL) do
+ SynchronizationReport = Meta.new do
on_new_document do |report|
report.conflicts = []
report.added_documents = []
@@ -9,7 +9,7 @@ module StrokeDB
end
end
- SynchronizationConflict = Meta.new(:nsurl => STROKEDB_NSURL) do
+ SynchronizationConflict = Meta.new do
def resolve!
# by default, do nothing
end |
| |   |
| 5 | 5 | "lib"/"core_ext".should == "lib/core_ext" |
| 6 | 6 | "lib/core_ext"/"foo".should == "lib/core_ext/foo" |
| 7 | 7 | end |
| 8 | end |
| 9 | |
| 10 | describe "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 | |
| 8 | 25 | end |
| toggle raw diff |
--- a/spec/lib/strokedb/core_ext/string_spec.rb
+++ b/spec/lib/strokedb/core_ext/string_spec.rb
@@ -5,4 +5,21 @@ describe "String#/" do
"lib"/"core_ext".should == "lib/core_ext"
"lib/core_ext"/"foo".should == "lib/core_ext/foo"
end
+end
+
+describe "String#modulize" do
+
+ it "if there is no module, leave nothing" do
+ "A".modulize.should == ""
+ "::A".modulize.should == ""
+ end
+
+ it "should leave single module" do
+ "A::B".modulize.should == "A"
+ end
+
+ it "should leave multiple modules" do
+ "A::B::C".modulize.should == "A::B"
+ end
+
end
\ No newline at end of file |
| |   |
| 10 | 10 | it "should have nsurl http://strokedb.com/" do |
| 11 | 11 | Meta.document.nsurl.should == STROKEDB_NSURL |
| 12 | 12 | 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 |
| 23 | 13 | |
| 24 | 14 | end |
| 25 | 15 | |
| toggle raw diff |
--- a/spec/lib/strokedb/document/meta_meta_spec.rb
+++ b/spec/lib/strokedb/document/meta_meta_spec.rb
@@ -10,16 +10,6 @@ describe "Meta meta" do
it "should have nsurl http://strokedb.com/" do
Meta.document.nsurl.should == STROKEDB_NSURL
end
-
- it "should have blank default nsurl by default" do
- Meta.default_nsurl.should be_blank
- end
-
- it "should be able to configure new default nsurl" do
- Meta.default_nsurl = "http://mycoolapp.com"
- Meta.default_nsurl.should == "http://mycoolapp.com"
- Meta.default_nsurl = ""
- end
end
|
| |   |
| 2 | 2 | |
| 3 | 3 | describe "Meta module", :shared => true do |
| 4 | 4 | |
| 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 | | |
| 17 | 5 | it "should be able to instantiate new Document which is also SomeName" do |
| 18 | 6 | obj = SomeName.new |
| 19 | 7 | obj.should be_a_kind_of(Document) |
| … | … | |
| 162 | 162 | end |
| 163 | 163 | end |
| 164 | 164 | |
| 165 | describe "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 | |
| 188 | end |
| 189 | |
| 190 | |
| 191 | describe "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 | |
| 216 | end |
| 165 | 217 | describe "Meta module with on_initialization callback" do |
| 166 | 218 | |
| 167 | 219 | before(:each) do |
| … | … | |
| 268 | 268 | doc = SomeName.new |
| 269 | 269 | end |
| 270 | 270 | |
| 271 | | it "should receive this callback on document load" do |
| 271 | it "should receive this callback on document load" do |
| 272 | 272 | doc = SomeName.create! |
| 273 | 273 | Kernel.should_receive(:on_load_called).with(false) |
| 274 | 274 | SomeName.find(doc.uuid) |
| toggle raw diff |
--- a/spec/lib/strokedb/document/meta_spec.rb
+++ b/spec/lib/strokedb/document/meta_spec.rb
@@ -2,18 +2,6 @@ require File.dirname(__FILE__) + '/spec_helper'
describe "Meta module", :shared => true do
- it "should use Meta.default_nsurl if nsurl is not specified" do
- Meta.default_nsurl = "http://some/"
- SomeName.document.nsurl.should == "http://some/"
- end
-
- it "should not use Meta.default_nsurl if nsurl is specified" do
- Meta.default_nsurl = "http://some/"
- Object.send!(:remove_const,'SomeName') if defined?(SomeName)
- SomeName = Meta.new(:nsurl => "http://another/")
- SomeName.document.nsurl.should == "http://another/"
- end
-
it "should be able to instantiate new Document which is also SomeName" do
obj = SomeName.new
obj.should be_a_kind_of(Document)
@@ -174,6 +162,58 @@ describe "Meta module without name" do
end
end
+describe "Meta module within no module" do
+
+ before(:each) do
+ setup_default_store
+ setup_index
+
+ Object.send!(:remove_const,'SomeName') if defined?(SomeName)
+ end
+
+ it "should use Module.nsurl by default" do
+ Module.nsurl "test"
+ SomeName = Meta.new
+ SomeName.document.nsurl.should == Module.nsurl
+ Module.nsurl ''
+ end
+
+ it "should not use Module.nsurl if nsurl is specified" do
+ Module.nsurl "test"
+ SomeName = Meta.new(:nsurl => 'passed')
+ SomeName.document.nsurl.should == 'passed'
+ Module.nsurl ''
+ end
+
+end
+
+
+describe "Meta module within module" do
+
+ before(:each) do
+ setup_default_store
+ setup_index
+ module A
+ nsurl "some url"
+ end
+ A.send!(:remove_const,'SomeName') if defined?(A::SomeName)
+ end
+
+ it "should use Module.nsurl by default" do
+ module A
+ SomeName = Meta.new
+ end
+ A::SomeName.document.nsurl.should == A.nsurl
+ end
+
+ it "should not use Module.nsurl if nsurl is specified" do
+ module A
+ SomeName = Meta.new(:nsurl => "nsurl")
+ end
+ A::SomeName.document.nsurl.should == "nsurl"
+ end
+
+end
describe "Meta module with on_initialization callback" do
before(:each) do
@@ -228,7 +268,7 @@ describe "Meta module with on_load callback" do
doc = SomeName.new
end
- it "should receive this callback on document load" 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) |