good_times 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/good_times.rb +35 -0
- data/lib/good_times/design_doc_helper.rb +58 -0
- data/lib/good_times/query_result.rb +15 -0
- data/lib/good_times/version.rb +3 -0
- data/spec/good_times_spec.rb +50 -0
- metadata +61 -0
data/lib/good_times.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'json/ext'
|
2
|
+
require 'couchrest'
|
3
|
+
require_relative 'good_times/query_result'
|
4
|
+
require_relative 'good_times/design_doc_helper'
|
5
|
+
|
6
|
+
class GoodTimes < CouchRest::Document
|
7
|
+
|
8
|
+
include DesignDocHelper
|
9
|
+
|
10
|
+
class << self
|
11
|
+
|
12
|
+
# alias :find :get
|
13
|
+
|
14
|
+
def query(method, view, options)
|
15
|
+
meta = class << self; self end
|
16
|
+
meta.instance_eval {
|
17
|
+
send(:define_method,"query_#{method.to_s}") do |*opts|
|
18
|
+
results = @design.view(view, options.merge(opts.first || {}))
|
19
|
+
if options[:include_docs]
|
20
|
+
results["rows"].map! {|row| self.new(row["doc"]) }
|
21
|
+
end
|
22
|
+
QueryResult.new(results)
|
23
|
+
end
|
24
|
+
}
|
25
|
+
end
|
26
|
+
|
27
|
+
def get(id)
|
28
|
+
new database.get(id) rescue nil
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# DesignDocHelper makes the following assumptions:
|
2
|
+
# 1) if you don't have a design doc created and you call views it will create that for you
|
3
|
+
# 2) when looking at your views, you will get the doc["views"] initially pulled from database, this means you can alter the design doc by hand after the class is loaded and DesignDocHelper won't know. Views are indexes created in the database. Any mismatch will resolve to what is in the db.
|
4
|
+
# 3) the views macro can only be called after a database has been specified (through use_database)
|
5
|
+
# 4) views specified in you class will overwrite the views in your couchdb
|
6
|
+
# 5) you will keep anything else already defined (lists, shows, etc)
|
7
|
+
|
8
|
+
module DesignDocHelper
|
9
|
+
|
10
|
+
def self.included(base)
|
11
|
+
base.extend(ClassMethods)
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
|
16
|
+
def use_database(db)
|
17
|
+
super
|
18
|
+
@design =
|
19
|
+
begin
|
20
|
+
database.get("_design/#{design_doc_name}")
|
21
|
+
rescue RestClient::ResourceNotFound
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# must be called only after use_database has been called
|
27
|
+
def views_setup(views)
|
28
|
+
initialize_design_doc(views) and return unless @design
|
29
|
+
if @design["views"] != views
|
30
|
+
update_design_doc(views)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def views
|
35
|
+
@design["views"] if @design
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def initialize_design_doc(views)
|
41
|
+
@design = CouchRest::Design.new
|
42
|
+
@design.name = design_doc_name
|
43
|
+
@design.database = database
|
44
|
+
@design["views"] = views
|
45
|
+
@design.save
|
46
|
+
end
|
47
|
+
|
48
|
+
def update_design_doc(views)
|
49
|
+
@design['views'] = views
|
50
|
+
@design.save
|
51
|
+
end
|
52
|
+
|
53
|
+
def design_doc_name
|
54
|
+
self.name.downcase
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class QueryResult
|
2
|
+
include Enumerable
|
3
|
+
|
4
|
+
attr_reader :total_rows, :offset, :rows
|
5
|
+
|
6
|
+
def initialize(results)
|
7
|
+
results.each do |key, value|
|
8
|
+
instance_variable_set("@#{key.to_s}", value)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def each(&block)
|
13
|
+
@rows.each {|row| block.call(row)}
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class NewMapper < GoodTimes
|
4
|
+
use_database TESTDB
|
5
|
+
end
|
6
|
+
|
7
|
+
describe GoodTimes do
|
8
|
+
describe ".query" do
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
NewMapper.views_setup("by_id" => {"map" => "function(doc) { emit(doc._id, null) } "})
|
12
|
+
NewMapper.query(:all, :by_id, {:include_docs => true})
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns a QueryResult object" do
|
16
|
+
NewMapper.query_all.should be_an_instance_of(QueryResult)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "calls a specific view on a design doc" do
|
20
|
+
# expect
|
21
|
+
NewMapper.instance_variable_get("@design").should_receive(:view).with(:by_id, anything()).and_return({"total_rows"=>0, "offset"=>0, "rows"=>[]})
|
22
|
+
|
23
|
+
# when
|
24
|
+
NewMapper.query_all
|
25
|
+
end
|
26
|
+
|
27
|
+
it "defines new methods on the class" do
|
28
|
+
NewMapper.should respond_to(:query_all)
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have CouchRest::Document as its ancestor" do
|
34
|
+
NewMapper.ancestors.should include(CouchRest::Document)
|
35
|
+
end
|
36
|
+
|
37
|
+
describe ".get" do
|
38
|
+
it "should retrieve a document by id" do
|
39
|
+
TESTDB.save_doc({"_id" => "1234"})
|
40
|
+
nm = NewMapper.get("1234")
|
41
|
+
nm.class.should == NewMapper
|
42
|
+
nm["_id"].should == "1234"
|
43
|
+
end
|
44
|
+
it "should return nil if no document exists" do
|
45
|
+
nm = NewMapper.get("nonexistent")
|
46
|
+
nm.should == nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: good_times
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- George South
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-24 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: couchrest
|
16
|
+
requirement: &70138512240520 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.1.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70138512240520
|
25
|
+
description: Connecting couchrest functionality while keeping the couchdb paradigm
|
26
|
+
email: ecuageo@gmail.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- lib/good_times.rb
|
32
|
+
- lib/good_times/version.rb
|
33
|
+
- lib/good_times/query_result.rb
|
34
|
+
- lib/good_times/design_doc_helper.rb
|
35
|
+
- spec/good_times_spec.rb
|
36
|
+
homepage: http://rubygems.org/gems/good_times
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.10
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Connecting couchrest functionality while keeping the couchdb paradigm
|
60
|
+
test_files:
|
61
|
+
- spec/good_times_spec.rb
|