Skip to content

Commit

Permalink
View#define == View#new, View#define! == View#create!; they all share…
Browse files Browse the repository at this point in the history
… necessary functionality
  • Loading branch information
yrashk committed Apr 29, 2008
1 parent a53d9ab commit 42d9143
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 9 additions & 3 deletions lib/strokedb/views/view.rb
Expand Up @@ -236,14 +236,15 @@ class << View
def [](name)
# TODO: find viewdoc by name
end


alias :original_new :new
# Define a view.
#
# Examples
# View.define("view_name", :option => "value") do |viewdoc| ... end
# View.define(:name => "view_name", :option => "value") do |viewdoc| ... end
#
def define(*args, &block)
def new(*args, &block)
if args.first.is_a? String
options = args[1] || {}
options['name'] = args.first
Expand All @@ -262,11 +263,16 @@ def define(*args, &block)
options['uuid'] = ::Util.sha1_uuid("view:#{nsurl}##{name}")

unless v = find(options['uuid'])
v = create!(options, &block)
v = original_new(options, &block)
end
v
end

alias :define :new
alias :define! :create!

end


class InvalidViewError < StandardError ; end

Expand Down
8 changes: 4 additions & 4 deletions spec/lib/strokedb/views/view_spec.rb
Expand Up @@ -8,7 +8,7 @@

it "could not be initialized" do
lambda do
@post_comments = View.define
@post_comments = View.define!
end.should raise_error(ArgumentError)
end

Expand All @@ -17,7 +17,7 @@
describe View, "without #map method defined" do
before(:each) do
setup_default_store
@post_comments = View.define(:name => "post_comments")
@post_comments = View.define!(:name => "post_comments")
end

it "should raise exception when #map is used" do
Expand All @@ -29,7 +29,7 @@

before(:all) do
setup_default_store
@view = View.define("post_comments") do |view|
@view = View.define!("post_comments") do |view|
def view.map(uuid, doc)
doc['type'] =~ /comment/ ? [[[doc.parent, doc.created_at], doc]] : nil
end
Expand Down Expand Up @@ -94,7 +94,7 @@ def view.map(uuid, doc)

before(:each) do
setup_default_store
@view = View.define("SomeView") do |view|
@view = View.define!("SomeView") do |view|
def view.map(uuid, doc)
[[doc,doc]]
end
Expand Down

0 comments on commit 42d9143

Please sign in to comment.