couchbase-view-helpers 1.0.1-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +78 -0
- data/Rakefile +10 -0
- data/couchbase-view-helpers.gemspec +30 -0
- data/lib/couchbase/view_helpers/paginate_params.rb +21 -0
- data/lib/couchbase/view_helpers/version.rb +5 -0
- data/lib/couchbase/view_helpers.rb +87 -0
- data/lib/couchbase.rb +10 -0
- data/test/helper.rb +11 -0
- data/test/test_integration.rb +16 -0
- data/test/test_paginate_params.rb +25 -0
- data/test/test_view_helpers.rb +110 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: bb7791c34f4a225c0dbcbcdb026e3b62fd047bb4
|
4
|
+
data.tar.gz: 7bbc4457fb39cf4f3ce0ef0f051cc79fff29953a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8dafed7c89761253d72462209e3177591788264320409ad0a953c2d4cfb47f3c90d88b6450d0015bcd4c621e8baebcd1a8ccd0494a6101ca2f81129dc7a7860f
|
7
|
+
data.tar.gz: 62391ca5827cb8ec9561a9d0067010cfe3095a841f37b40dc4559c707aaa8ea292360472f09353af0446d2c13abb46256a94bf578b968a9deb2a8a052478f4e5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mike Evans
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
# Couchbase::ViewHelpers
|
2
|
+
|
3
|
+
After using Couchbase::View in a few projects, I quickly tired of having to reference the API
|
4
|
+
docs for the specific options needed. This gem adds some simple methods to make configuring
|
5
|
+
a View before the query is executed.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'couchbase-view-helpers'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install couchbase-view-helpers
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
require 'couchbase/model'
|
25
|
+
|
26
|
+
class Post < Couchbase::Model
|
27
|
+
attribute :title
|
28
|
+
attribute :body
|
29
|
+
attribute :draft
|
30
|
+
|
31
|
+
view :all, :by_draft_status
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Normal View access:
|
36
|
+
|
37
|
+
```
|
38
|
+
drafts = Post.by_draft_status(key: true)
|
39
|
+
all = Post.all(descending: true)
|
40
|
+
```
|
41
|
+
|
42
|
+
With ViewHelpers:
|
43
|
+
|
44
|
+
```
|
45
|
+
drafts = Post.by_draft_status.key(true)
|
46
|
+
all = Post.all.desc
|
47
|
+
```
|
48
|
+
|
49
|
+
Supports will_paginate and kaminari style pagination. Normal View pagination:
|
50
|
+
|
51
|
+
```
|
52
|
+
page = 3
|
53
|
+
per = 20
|
54
|
+
skip = (page - 1) * per
|
55
|
+
limit = per
|
56
|
+
page = Post.all(skip: skip, limit: limit)
|
57
|
+
```
|
58
|
+
|
59
|
+
With ViewHelpers:
|
60
|
+
|
61
|
+
```
|
62
|
+
page = Post.all.paginate(page: 3, per_page: 20)
|
63
|
+
# or
|
64
|
+
page = Post.all.page(3).per(20)
|
65
|
+
```
|
66
|
+
|
67
|
+
## TODO
|
68
|
+
|
69
|
+
1. Full coverage of all options (key ranges, etc.)
|
70
|
+
2. Integrate pagination and other defaults in the class level view declaration.
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
1. Fork it
|
75
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
76
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
77
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
78
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'couchbase/view_helpers/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'couchbase-view-helpers'
|
8
|
+
spec.version = Couchbase::ViewHelpers::VERSION
|
9
|
+
spec.authors = ['Mike Evans']
|
10
|
+
spec.email = ['mike@urlgonomics.com']
|
11
|
+
spec.description = %q{Adds syntactic sugar to Couchbase view options hashes.}
|
12
|
+
spec.summary = %q{Adds syntactic sugar to Couchbase view options hashes.}
|
13
|
+
spec.homepage = 'https://github.com/mje113/couchbase-view-helpers'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
spec.platform = 'java' if defined?(JRUBY_VERSION)
|
21
|
+
|
22
|
+
if defined?(JRUBY_VERSION)
|
23
|
+
spec.add_dependency 'couchbase-jruby-client', '~> 0.1.4'
|
24
|
+
else
|
25
|
+
spec.add_dependency 'couchbase', '~> 1.3.3'
|
26
|
+
end
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
29
|
+
spec.add_development_dependency 'rake'
|
30
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Couchbase
|
2
|
+
|
3
|
+
module ViewHelpers
|
4
|
+
class PaginateParams
|
5
|
+
|
6
|
+
PER_PAGE_DEFAULT = 100
|
7
|
+
|
8
|
+
def initialize(params = {})
|
9
|
+
@page = (params[:page] || 1).to_i
|
10
|
+
@per_page = (params[:per_page] || PER_PAGE_DEFAULT).to_i
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_h
|
14
|
+
{
|
15
|
+
skip: (@page - 1) * @per_page,
|
16
|
+
limit: @per_page
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'couchbase/view_helpers/version'
|
2
|
+
|
3
|
+
module Couchbase
|
4
|
+
|
5
|
+
module ViewHelpers
|
6
|
+
|
7
|
+
# Keys
|
8
|
+
def key(key)
|
9
|
+
add_param(key: key)
|
10
|
+
end
|
11
|
+
|
12
|
+
def keys(*keys)
|
13
|
+
add_param(keys: Array(keys).flatten)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Pagination
|
17
|
+
def paginate(params = {})
|
18
|
+
add_param(PaginateParams.new(params).to_h)
|
19
|
+
end
|
20
|
+
|
21
|
+
def page(page_num)
|
22
|
+
paginate(page: page_num)
|
23
|
+
end
|
24
|
+
|
25
|
+
def per(per_page)
|
26
|
+
paginate(per_page: per_page)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Ordering
|
30
|
+
def order(direction)
|
31
|
+
add_param(descending: (direction.to_s =~ /^asc/i) != 0)
|
32
|
+
end
|
33
|
+
|
34
|
+
def desc
|
35
|
+
order('desc')
|
36
|
+
end
|
37
|
+
|
38
|
+
def asc
|
39
|
+
order('asc')
|
40
|
+
end
|
41
|
+
|
42
|
+
# Grouping
|
43
|
+
def group(group = true)
|
44
|
+
add_param(group: group)
|
45
|
+
end
|
46
|
+
|
47
|
+
def group_level(group_level)
|
48
|
+
add_param(group_level: group_level)
|
49
|
+
end
|
50
|
+
alias_method :level, :group_level
|
51
|
+
|
52
|
+
# Reducing
|
53
|
+
def reduce(reduce = true)
|
54
|
+
add_param(reduce: reduce)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Freshness
|
58
|
+
def stale(stale = :ok)
|
59
|
+
add_param(stale: stale)
|
60
|
+
end
|
61
|
+
|
62
|
+
def fresh
|
63
|
+
stale(false)
|
64
|
+
end
|
65
|
+
|
66
|
+
def update_after
|
67
|
+
stale(:update_after)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Docs
|
71
|
+
def include_docs(docs = true)
|
72
|
+
add_param(include_docs: docs)
|
73
|
+
end
|
74
|
+
|
75
|
+
def no_docs
|
76
|
+
include_docs(false)
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
def add_param(param)
|
82
|
+
@params.merge!(param)
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
data/lib/couchbase.rb
ADDED
data/test/helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestIntegration < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
skip 'strangeness with View constructor'
|
7
|
+
@view = Couchbase::View.new('bucket', 'endpoint')
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_has_public_methods
|
11
|
+
[:key, :keys, :paginate, :page, :per, :order, :desc, :asc].each do |meth|
|
12
|
+
assert @view.respond_to?(meth), "View not responding to #{meth}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestPaginateParams < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def test_can_parse_params_into_view_params
|
6
|
+
paginator = Couchbase::ViewHelpers::PaginateParams.new({ page: 2, per_page: 20 })
|
7
|
+
assert_equal({ skip: 20, limit: 20 }, paginator.to_h)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_can_have_defaults
|
11
|
+
paginator = Couchbase::ViewHelpers::PaginateParams.new
|
12
|
+
assert_equal({ skip: 0, limit: 100 }, paginator.to_h)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_can_interface_with_view
|
16
|
+
view = MockView.new
|
17
|
+
view.paginate
|
18
|
+
assert_equal({ skip: 0, limit: 100 }, view.params)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_can_deal_wth_strings
|
22
|
+
paginator = Couchbase::ViewHelpers::PaginateParams.new({ page: '2', per_page: '20' })
|
23
|
+
assert_equal({ skip: 20, limit: 20 }, paginator.to_h)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestViewHelpers < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@view = MockView.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_can_set_key
|
10
|
+
assert_instance_of MockView, @view.key('foo')
|
11
|
+
assert_equal 'foo', @view.params[:key]
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_can_set_keys
|
15
|
+
assert_instance_of MockView, @view.keys(['foo', 'bar'])
|
16
|
+
assert_equal ['foo', 'bar'], @view.params[:keys]
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_can_set_keys_with_args
|
20
|
+
assert_instance_of MockView, @view.keys('foo', 'bar')
|
21
|
+
assert_equal ['foo', 'bar'], @view.params[:keys]
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_can_paginate
|
25
|
+
assert_instance_of MockView, @view.paginate(page: 2, per_page: 20)
|
26
|
+
assert_equal 20, @view.params[:skip]
|
27
|
+
assert_equal 20, @view.params[:limit]
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_can_paginate_by_page
|
31
|
+
assert_instance_of MockView, @view.page(2)
|
32
|
+
assert_equal 100, @view.params[:skip]
|
33
|
+
assert_equal 100, @view.params[:limit]
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_can_paginate_by_per
|
37
|
+
assert_instance_of MockView, @view.per(20)
|
38
|
+
assert_equal 0, @view.params[:skip]
|
39
|
+
assert_equal 20, @view.params[:limit]
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_can_paginate_by_page_and_per
|
43
|
+
skip "need to persist"
|
44
|
+
assert_instance_of MockView, @view.page(2).per(20)
|
45
|
+
assert_equal 20, @view.params[:skip]
|
46
|
+
assert_equal 20, @view.params[:limit]
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_can_set_order
|
50
|
+
assert_instance_of MockView, @view.order('asc')
|
51
|
+
refute false, @view.params[:descending]
|
52
|
+
|
53
|
+
@view.order('ascending')
|
54
|
+
refute false, @view.params[:descending]
|
55
|
+
|
56
|
+
@view.order('desc')
|
57
|
+
assert true, @view.params[:descending]
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_can_set_order_by_desc
|
61
|
+
assert_instance_of MockView, @view.desc
|
62
|
+
assert true, @view.params[:descending]
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_can_set_order_by_asc
|
66
|
+
assert_instance_of MockView, @view.asc
|
67
|
+
refute false, @view.params[:descending]
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_can_group
|
71
|
+
assert_instance_of MockView, @view.group
|
72
|
+
assert @view.params[:group]
|
73
|
+
|
74
|
+
@view.group(false)
|
75
|
+
refute @view.params[:group]
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_can_group_level
|
79
|
+
assert_instance_of MockView, @view.group_level(2)
|
80
|
+
assert_equal 2, @view.params[:group_level]
|
81
|
+
|
82
|
+
@view.level(3)
|
83
|
+
assert_equal 3, @view.params[:group_level]
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_can_set_stale
|
87
|
+
assert_instance_of MockView, @view.stale
|
88
|
+
assert_equal :ok, @view.params[:stale]
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_can_set_fresh
|
92
|
+
assert_instance_of MockView, @view.fresh
|
93
|
+
refute @view.params[:stale]
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_can_set_update_after
|
97
|
+
assert_instance_of MockView, @view.update_after
|
98
|
+
assert_equal :update_after, @view.params[:stale]
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_can_set_include_docs
|
102
|
+
assert_instance_of MockView, @view.include_docs
|
103
|
+
assert @view.params[:include_docs]
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_can_set_no_docs
|
107
|
+
assert_instance_of MockView, @view.include_docs
|
108
|
+
assert @view.params[:include_docs]
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: couchbase-view-helpers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: java
|
6
|
+
authors:
|
7
|
+
- Mike Evans
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: couchbase-jruby-client
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.4
|
20
|
+
requirement: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.1.4
|
25
|
+
prerelease: false
|
26
|
+
type: :runtime
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
requirement: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '1.3'
|
39
|
+
prerelease: false
|
40
|
+
type: :development
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
prerelease: false
|
54
|
+
type: :development
|
55
|
+
description: Adds syntactic sugar to Couchbase view options hashes.
|
56
|
+
email:
|
57
|
+
- mike@urlgonomics.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- couchbase-view-helpers.gemspec
|
68
|
+
- lib/couchbase.rb
|
69
|
+
- lib/couchbase/view_helpers.rb
|
70
|
+
- lib/couchbase/view_helpers/paginate_params.rb
|
71
|
+
- lib/couchbase/view_helpers/version.rb
|
72
|
+
- test/helper.rb
|
73
|
+
- test/test_integration.rb
|
74
|
+
- test/test_paginate_params.rb
|
75
|
+
- test/test_view_helpers.rb
|
76
|
+
homepage: https://github.com/mje113/couchbase-view-helpers
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.1.9
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Adds syntactic sugar to Couchbase view options hashes.
|
100
|
+
test_files:
|
101
|
+
- test/helper.rb
|
102
|
+
- test/test_integration.rb
|
103
|
+
- test/test_paginate_params.rb
|
104
|
+
- test/test_view_helpers.rb
|