Skip to content

Commit

Permalink
Merge branch 'master' into new-views
Browse files Browse the repository at this point in the history
  • Loading branch information
yrashk committed Apr 29, 2008
2 parents 3fbc394 + c6286ab commit d933fcf
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 46 deletions.
1 change: 1 addition & 0 deletions lib/strokedb.rb
Expand Up @@ -70,6 +70,7 @@ def DEBUG
class NoDefaultStoreError < Exception ; end
end

require 'strokedb/nsurl'
require 'strokedb/util'
require 'strokedb/document'
require 'strokedb/config'
Expand Down
6 changes: 5 additions & 1 deletion lib/strokedb/core_ext/string.rb
Expand Up @@ -25,11 +25,15 @@ def demodulize
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

Expand Down
8 changes: 6 additions & 2 deletions lib/strokedb/document.rb
Expand Up @@ -684,11 +684,15 @@ def self.collect_meta_modules(store, meta) #:nodoc:
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
Expand Down
2 changes: 1 addition & 1 deletion lib/strokedb/document/delete.rb
Expand Up @@ -3,7 +3,7 @@ module StrokeDB
class DocumentDeletionError < StandardError
end

DeletedDocument = Meta.new(:nsurl => STROKEDB_NSURL) do
DeletedDocument = Meta.new do
on_load do |doc|
doc.make_immutable!
end
Expand Down
17 changes: 4 additions & 13 deletions lib/strokedb/document/meta.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -62,7 +53,7 @@ def new(*args, &block)
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
Expand All @@ -71,7 +62,7 @@ def document(store=nil)
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)
Expand Down Expand Up @@ -243,8 +234,8 @@ def make_document(store=nil)

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)
Expand Down
23 changes: 23 additions & 0 deletions lib/strokedb/nsurl.rb
@@ -0,0 +1,23 @@
Module.module_eval do

def self.clear_nsurls
@@nsurls = {}
end
def self.find_by_nsurl(url)
@@nsurls[url]
end

def nsurl(url = nil)
return @nsurl unless url
@@nsurls ||= {}
mod = @@nsurls[url]
raise ArgumentError, "nsurl #{url.inspect} is already referenced by #{mod.inspect} module" if mod && mod != self
@@nsurls.delete(url)
@@nsurls[url] = self
@nsurl = url
end

end

Module.nsurl ''
StrokeDB.nsurl StrokeDB::STROKEDB_NSURL
2 changes: 1 addition & 1 deletion lib/strokedb/store.rb
@@ -1,6 +1,6 @@
module StrokeDB

StoreInfo = Meta.new(:nsurl => STROKEDB_NSURL)
StoreInfo = Meta.new

class Store
include Enumerable
Expand Down
2 changes: 1 addition & 1 deletion lib/strokedb/sync/diff.rb
Expand Up @@ -68,7 +68,7 @@ def self.patch(from, patch)
end
end

Diff = Meta.new(:nsurl => STROKEDB_NSURL) do
Diff = Meta.new do

on_initialization do |diff|
diff.added_slots = {} unless diff[:added_slots]
Expand Down
4 changes: 2 additions & 2 deletions 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 = []
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/strokedb/views/view.rb
Expand Up @@ -234,7 +234,7 @@ def self.[](view_name)
end

class << View
def [](name, nsurl = Meta.default_nsurl) # FIXME: Meta seems to be a bad place for default_nsurl now
def [](name, nsurl = name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl) # FIXME: it is not nice
uuid = ::Util.sha1_uuid("view:#{nsurl}##{name}")
StrokeDB.default_store.find(uuid)
end
Expand All @@ -260,7 +260,7 @@ def new(*args, &block)
raise ArgumentError, "View name must be specified!"
end

nsurl = options['nsurl'] ||= Meta.default_nsurl # FIXME: Meta seems to be a bad place for default_nsurl now
nsurl = options['nsurl'] ||= name.modulize.empty? ? Module.nsurl : name.modulize.constantize.nsurl # FIXME: it is not nice (and the same shit is in meta.rb)

options['uuid'] = ::Util.sha1_uuid("view:#{nsurl}##{name}")

Expand Down
17 changes: 17 additions & 0 deletions spec/lib/strokedb/core_ext/string_spec.rb
Expand Up @@ -5,4 +5,21 @@
"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
10 changes: 0 additions & 10 deletions spec/lib/strokedb/document/meta_meta_spec.rb
Expand Up @@ -10,16 +10,6 @@
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

Expand Down
66 changes: 53 additions & 13 deletions spec/lib/strokedb/document/meta_spec.rb
Expand Up @@ -2,18 +2,6 @@

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)
Expand Down Expand Up @@ -174,6 +162,58 @@
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
Expand Down Expand Up @@ -228,7 +268,7 @@
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)
Expand Down
60 changes: 60 additions & 0 deletions spec/lib/strokedb/nsurl_spec.rb
@@ -0,0 +1,60 @@
require File.dirname(__FILE__) + '/spec_helper'

describe "Some", Module do

before(:each) do
@module = Module.new
Module.clear_nsurls
end

it "should have nil nsurl by default" do
@module.nsurl.should be_nil
end

it "should be able to change nsurl" do
@module.nsurl 'http://some.url'
@module.nsurl.should == 'http://some.url'
end

it "should be findable by nsurl" do
@module.nsurl 'http://some.url'
Module.find_by_nsurl(@module.nsurl).should == @module
end

it "should be able to change nsurl to the same value" do
@module.nsurl 'http://some.url'
lambda { @module.nsurl 'http://some.url' }.should_not raise_error(ArgumentError)
end

it "should not be able to change nsurl to the value already assigned to some module" do
@some_module = Module.new
@some_module.nsurl 'http://some.url'
lambda { @module.nsurl 'http://some.url' }.should raise_error(ArgumentError)
end

end

describe Module do


before(:each) do
Module.clear_nsurls
end

it "should have empty nsurl by default" do
Module.nsurl.should be_empty
end

end

describe StrokeDB do

before(:each) do
Module.clear_nsurls
end

it "should have #{STROKEDB_NSURL} nsurl by default" do
StrokeDB.nsurl.should == STROKEDB_NSURL
end

end

0 comments on commit d933fcf

Please sign in to comment.