jmoses-couchbase-model 0.5.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +5 -0
- data/.travis.yml +11 -0
- data/.yardopts +5 -0
- data/CONTRIBUTING.markdown +75 -0
- data/Gemfile +4 -0
- data/HISTORY.markdown +112 -0
- data/README.markdown +160 -0
- data/Rakefile +22 -0
- data/couchbase-model.gemspec +24 -0
- data/lib/couchbase-model.rb +23 -0
- data/lib/couchbase/model.rb +865 -0
- data/lib/couchbase/model/configuration.rb +30 -0
- data/lib/couchbase/model/ext/camelize.rb +23 -0
- data/lib/couchbase/model/ext/constantize.rb +29 -0
- data/lib/couchbase/model/ext/singleton_class.rb +24 -0
- data/lib/couchbase/model/uuid.rb +113 -0
- data/lib/couchbase/model/version.rb +26 -0
- data/lib/couchbase/railtie.rb +142 -0
- data/lib/rails/generators/couchbase/config/config_generator.rb +43 -0
- data/lib/rails/generators/couchbase/config/templates/couchbase.yml +23 -0
- data/lib/rails/generators/couchbase/view/templates/map.js +40 -0
- data/lib/rails/generators/couchbase/view/templates/reduce.js +61 -0
- data/lib/rails/generators/couchbase/view/view_generator.rb +43 -0
- data/lib/rails/generators/couchbase_generator.rb +42 -0
- data/tasks/package.rake +27 -0
- data/tasks/test.rake +34 -0
- data/tasks/util.rake +21 -0
- data/test/setup.rb +168 -0
- data/test/test_model.rb +302 -0
- data/test/test_model_rails_integration.rb +76 -0
- data/test/test_uuid.rb +32 -0
- metadata +148 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
before_install:
|
2
|
+
- wget -O- http://packages.couchbase.com/ubuntu/couchbase.key | sudo apt-key add -
|
3
|
+
- echo deb http://packages.couchbase.com/preview/ubuntu lucid lucid/main | sudo tee /etc/apt/sources.list.d/couchbase.list
|
4
|
+
- sudo apt-get update
|
5
|
+
- sudo apt-get -y install libevent-dev libvbucket-dev libcouchbase-dev
|
6
|
+
|
7
|
+
rvm:
|
8
|
+
- 1.8.7
|
9
|
+
- 1.9.2
|
10
|
+
- 1.9.3
|
11
|
+
- ree
|
data/.yardopts
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
We've decided to use "gerrit" for our code review system, making it
|
2
|
+
easier for all of us to contribute with code and comments.
|
3
|
+
|
4
|
+
1. Visit http://review.couchbase.org and "Register" for an account
|
5
|
+
2. Review http://review.couchbase.org/static/individual_agreement.html
|
6
|
+
3. Agree to agreement by visiting http://review.couchbase.org/#/settings/agreements
|
7
|
+
4. If you do not receive an email, please contact us
|
8
|
+
5. Check out the `couchbase-ruby-model` area http://review.couchbase.org/#/q/status:open+project:couchbase-ruby-model,n,z
|
9
|
+
6. Join us on IRC at #libcouchbase on Freenode :-)
|
10
|
+
|
11
|
+
We normally don't go looking for stuff in gerrit, so you should add at
|
12
|
+
least me `"Sergey Avseyev" <sergey.avseyev@gmail.com>` as a reviewer
|
13
|
+
for your patch (and I'll know who else to add and add them for you).
|
14
|
+
|
15
|
+
## Contributing Using Repo Tool
|
16
|
+
|
17
|
+
Follow ["Uploading Changes" guide][1] on the site if you have some code to contribute.
|
18
|
+
|
19
|
+
All you should need to set up your development environment should be:
|
20
|
+
|
21
|
+
~ % mkdir couchbase-ruby
|
22
|
+
~ % cd couchbase-ruby
|
23
|
+
~/couchbase-ruby % repo init -u git://github.com/trondn/manifests.git -m ruby.xml
|
24
|
+
~/couchbase-ruby % repo sync
|
25
|
+
~/couchbase-ruby % repo start my-branch-name --all
|
26
|
+
~/couchbase-ruby % make
|
27
|
+
|
28
|
+
This will build the latest version of `libcouchbase`,
|
29
|
+
`couchbase-ruby-client` and `couchbase-ruby-model` libraries. You must
|
30
|
+
have a C and C++ compiler installed, automake, autoconf.
|
31
|
+
|
32
|
+
If you have to make any changes just commit them before you upload
|
33
|
+
them to gerrit with the following command:
|
34
|
+
|
35
|
+
~/couchbase-ruby/model % repo upload
|
36
|
+
|
37
|
+
You might experience a problem trying to upload the patches if you've
|
38
|
+
selected a different login name at http://review.couchbase.org than
|
39
|
+
your login name. Don't worry, all you need to do is to add the
|
40
|
+
following to your ~/.gitconfig file:
|
41
|
+
|
42
|
+
[review "review.couchbase.org"]
|
43
|
+
username = YOURNAME
|
44
|
+
|
45
|
+
## Contributing Using Plain Git
|
46
|
+
|
47
|
+
If you not so familiar with repo tool and its workflow there is
|
48
|
+
alternative way to do the same job. Lets assume you have installed
|
49
|
+
couchbase gem and libcouchbase from official packages and would you to
|
50
|
+
contribute to couchbase-model gem only. Then you just need to complete
|
51
|
+
gerrit registration steps above and clone the source repository
|
52
|
+
(remember the repository on github.com is just a mirror):
|
53
|
+
|
54
|
+
~ % git clone ssh://YOURNAME@review.couchbase.org:29418/couchbase-ruby-model.git
|
55
|
+
|
56
|
+
Install [`commit-msg` hook][2]:
|
57
|
+
|
58
|
+
~/couchbase-ruby-model % scp -p -P 29418 YOURNAME@review.couchbase.org:hooks/commit-msg .git/hooks/
|
59
|
+
|
60
|
+
Make your changes and upload them for review:
|
61
|
+
|
62
|
+
~/couchbase-ruby-model % git commit
|
63
|
+
~/couchbase-ruby-model % git push origin HEAD:refs/for/master
|
64
|
+
|
65
|
+
If you need to fix or add something to your patch, do it and re-upload
|
66
|
+
the changes (all you need is to keep `Change-Id:` line the same to
|
67
|
+
allow gerrit to track the patch.
|
68
|
+
|
69
|
+
~/couchbase-ruby-model % git commit --amend
|
70
|
+
~/couchbase-ruby-model % git push origin HEAD:refs/for/master
|
71
|
+
|
72
|
+
Happy hacking!
|
73
|
+
|
74
|
+
[1]: http://review.couchbase.org/Documentation/user-upload.html
|
75
|
+
[2]: http://review.couchbase.org/Documentation/user-changeid.html
|
data/Gemfile
ADDED
data/HISTORY.markdown
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
## 0.5.3 / 2013-06-06
|
2
|
+
|
3
|
+
* Prefer single-quoted strings (Andrey Koleshko)
|
4
|
+
* Test for Model.design_document (Andrey Koleshko)
|
5
|
+
* Support for batch finding multiple objects by id (Jon Moses)
|
6
|
+
* Test for activemodel instead of rails for activemodel validations
|
7
|
+
* Update couchbase dependency to 1.3.0
|
8
|
+
|
9
|
+
## 0.5.2 / 2013-02-25
|
10
|
+
|
11
|
+
* Fix attribute inheritance when subclassing (Mike Evans)
|
12
|
+
* Added as_json method for rails JSON responses (Stephen von Takach)
|
13
|
+
* Add contributing document
|
14
|
+
* Fix test hiding
|
15
|
+
* Remove comments from the javascript sources
|
16
|
+
* Reduce development dependencies and update jar version
|
17
|
+
|
18
|
+
## 0.5.1 / 2012-11-29
|
19
|
+
|
20
|
+
* Introduce save! and create! methods and raise RecordInvalid only from them
|
21
|
+
|
22
|
+
## 0.5.0 / 2012-11-21
|
23
|
+
|
24
|
+
* Update template for map function
|
25
|
+
* Use extended get for #find_by_id
|
26
|
+
* Do not use HashWithIndifferentAccess class unless it defined
|
27
|
+
* Pass options to #create method
|
28
|
+
* Ensure validness on create
|
29
|
+
* Fix storing raw data
|
30
|
+
* Define read_attribute and write_attribute methods
|
31
|
+
* Support couchbase 1.2.0.z.beta4
|
32
|
+
|
33
|
+
## 0.4.4 / 2012-10-17
|
34
|
+
|
35
|
+
* Make #to_param aware about keys
|
36
|
+
|
37
|
+
## 0.4.2 / 2012-10-17
|
38
|
+
|
39
|
+
* Update CAS value after mutation
|
40
|
+
* Added ability to pass options to mutators. Thanks to @kierangraham
|
41
|
+
* Always try to include Rails stuff into model
|
42
|
+
* Use key if id is nil (makes sense for some view results)
|
43
|
+
|
44
|
+
## 0.4.1 / 2012-09-26
|
45
|
+
|
46
|
+
* Put support notes in README
|
47
|
+
* Add note about validations in the README
|
48
|
+
* Update repo URL
|
49
|
+
* RCBC-85 Fix typo in `save' method
|
50
|
+
|
51
|
+
## 0.4.0 / 2012-09-25
|
52
|
+
|
53
|
+
* Add validation hooks for Rails application
|
54
|
+
* Check meta presence as more robust indicator of key presence
|
55
|
+
|
56
|
+
## 0.3.1 / 2012-09-22
|
57
|
+
|
58
|
+
* Allow to specify default storage options
|
59
|
+
|
60
|
+
## 0.3.0 / 2012-09-22
|
61
|
+
|
62
|
+
* Implement belongs_to asscociation
|
63
|
+
* Use ActiveModel naming and conversion
|
64
|
+
* Define persisted? method
|
65
|
+
* Allow optional CAS value for mutators
|
66
|
+
* Use replace in save method. Thanks to @scalabl3
|
67
|
+
* Add callbacks for :save, :create, :update and :delete methods
|
68
|
+
|
69
|
+
## 0.2.0 / 2012-09-18
|
70
|
+
|
71
|
+
* Add Rails 3 configuration possibilities, allow configuring
|
72
|
+
ensure_design_documents to disable/enable the auto view upgrade
|
73
|
+
(thanks to David Rice)
|
74
|
+
* Ensure views directory is always set (thanks to David Rice)
|
75
|
+
* Fix tests for ruby 1.8.7
|
76
|
+
* Reword header in README
|
77
|
+
* Merge pull request #3 from davidjrice/master
|
78
|
+
* Use debugger gem
|
79
|
+
* Update Model wrapper to match latest API changes
|
80
|
+
* Strip contents of the JS file
|
81
|
+
* Do not submit empty views
|
82
|
+
* Allow to specify default view options
|
83
|
+
* Display only non-nil values
|
84
|
+
* Rename underscored methods
|
85
|
+
* Load spatial views into design document
|
86
|
+
|
87
|
+
## 0.1.0 / 2012-04-10
|
88
|
+
|
89
|
+
* Allows to define several attributes at once
|
90
|
+
* Allow to specify default value
|
91
|
+
* Add missing @since and @return tags
|
92
|
+
* Add railtie
|
93
|
+
* Add config generator
|
94
|
+
* Use verbose mode by default for GET operation
|
95
|
+
* Add views generators
|
96
|
+
* Update document wrapper
|
97
|
+
* Add code to upgrade design docs automatically
|
98
|
+
* Cache design document signature in memory
|
99
|
+
* Use symbols for attribute hash
|
100
|
+
* Skip connection errors during start up
|
101
|
+
* Don't show config warning for config generator
|
102
|
+
* Calculate mtime of the design document
|
103
|
+
* Assign current_doc after creation
|
104
|
+
* Update readme file
|
105
|
+
* Use preview repository for travis
|
106
|
+
* Do not make zipball
|
107
|
+
* Show model attributes with model class
|
108
|
+
* Update test. The couchbase gem is using new defaults
|
109
|
+
|
110
|
+
## 0.0.1/ 2012-03-17
|
111
|
+
|
112
|
+
* Initial version
|
data/README.markdown
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
# Couchbase Model
|
2
|
+
|
3
|
+
This library allows to declare models for [couchbase gem][1].
|
4
|
+
|
5
|
+
## SUPPORT
|
6
|
+
|
7
|
+
If you found an issue, please file it in our [JIRA][3]. Also you are
|
8
|
+
always welcome on `#libcouchbase` channel at [freenode.net IRC servers][4].
|
9
|
+
|
10
|
+
Documentation: [http://rdoc.info/gems/couchbase-model](http://rdoc.info/gems/couchbase-model)
|
11
|
+
|
12
|
+
## Rails integration
|
13
|
+
|
14
|
+
To generate config you can use `rails generate couchbase:config`:
|
15
|
+
|
16
|
+
$ rails generate couchbase:config
|
17
|
+
create config/couchbase.yml
|
18
|
+
|
19
|
+
It will generate this `config/couchbase.yml` for you:
|
20
|
+
|
21
|
+
common: &common
|
22
|
+
hostname: localhost
|
23
|
+
port: 8091
|
24
|
+
username:
|
25
|
+
password:
|
26
|
+
pool: default
|
27
|
+
|
28
|
+
development:
|
29
|
+
<<: *common
|
30
|
+
bucket: couchbase_tinyurl_development
|
31
|
+
|
32
|
+
test:
|
33
|
+
<<: *common
|
34
|
+
bucket: couchbase_tinyurl_test
|
35
|
+
|
36
|
+
# set these environment variables on your production server
|
37
|
+
production:
|
38
|
+
hostname: <%= ENV['COUCHBASE_HOST'] %>
|
39
|
+
port: <%= ENV['COUCHBASE_PORT'] %>
|
40
|
+
username: <%= ENV['COUCHBASE_USERNAME'] %>
|
41
|
+
password: <%= ENV['COUCHBASE_PASSWORD'] %>
|
42
|
+
pool: <%= ENV['COUCHBASE_POOL'] %>
|
43
|
+
bucket: <%= ENV['COUCHBASE_BUCKET'] %>
|
44
|
+
|
45
|
+
## Examples
|
46
|
+
|
47
|
+
require 'couchbase/model'
|
48
|
+
|
49
|
+
class Post < Couchbase::Model
|
50
|
+
attribute :title
|
51
|
+
attribute :body
|
52
|
+
attribute :draft
|
53
|
+
end
|
54
|
+
|
55
|
+
p = Post.new(:id => 'hello-world',
|
56
|
+
:title => 'Hello world',
|
57
|
+
:draft => true)
|
58
|
+
p.save
|
59
|
+
p = Post.find('hello-world')
|
60
|
+
p.body = "Once upon the times...."
|
61
|
+
p.save
|
62
|
+
p.update(:draft => false)
|
63
|
+
Post.bucket.get('hello-world') #=> {"title"=>"Hello world", "draft"=>false,
|
64
|
+
# "body"=>"Once upon the times...."}
|
65
|
+
|
66
|
+
You can also let the library generate the unique identifier for you:
|
67
|
+
|
68
|
+
p = Post.create(:title => 'How to generate ID',
|
69
|
+
:body => 'Open up the editor...')
|
70
|
+
p.id #=> "74f43c3116e788d09853226603000809"
|
71
|
+
|
72
|
+
There are several algorithms available. By default it use `:sequential`
|
73
|
+
algorithm, but you can change it to more suitable one for you:
|
74
|
+
|
75
|
+
class Post < Couchbase::Model
|
76
|
+
attribute :title
|
77
|
+
attribute :body
|
78
|
+
attribute :draft
|
79
|
+
|
80
|
+
uuid_algorithm :random
|
81
|
+
end
|
82
|
+
|
83
|
+
You can define connection options on per model basis:
|
84
|
+
|
85
|
+
class Post < Couchbase::Model
|
86
|
+
attribute :title
|
87
|
+
attribute :body
|
88
|
+
attribute :draft
|
89
|
+
|
90
|
+
connect :port => 80, :bucket => 'blog'
|
91
|
+
end
|
92
|
+
|
93
|
+
## Validations
|
94
|
+
|
95
|
+
There are all methods from ActiveModel::Validations accessible in
|
96
|
+
context of rails application:
|
97
|
+
|
98
|
+
class Comment < Couchbase::Model
|
99
|
+
attribute :author, :body
|
100
|
+
|
101
|
+
validates_presence_of :author, :body
|
102
|
+
end
|
103
|
+
|
104
|
+
## Views (aka Map/Reduce indexes)
|
105
|
+
|
106
|
+
Views are stored in models directory in subdirectory named after the
|
107
|
+
model (to be precious `design_document` attribute of the model class).
|
108
|
+
Here is an example of directory layout for `Link` model with three
|
109
|
+
views.
|
110
|
+
|
111
|
+
.
|
112
|
+
└── app
|
113
|
+
└── models
|
114
|
+
├── link
|
115
|
+
│ ├── total_count
|
116
|
+
│ │ ├── map.js
|
117
|
+
│ │ └── reduce.js
|
118
|
+
│ ├── by_created_at
|
119
|
+
│ │ └── map.js
|
120
|
+
│ └── by_view_count
|
121
|
+
│ └── map.js
|
122
|
+
└── link.rb
|
123
|
+
|
124
|
+
To generate view you can use yet another generator `rails generate
|
125
|
+
couchbase:view DESIGNDOCNAME VIEWNAME`. For example how `total_count`
|
126
|
+
view could be generated:
|
127
|
+
|
128
|
+
$ rails generate couchbase:view link total_count
|
129
|
+
|
130
|
+
The generated files contains useful info and links about how to write
|
131
|
+
map and reduce functions, you can take a look at them in the [templates
|
132
|
+
directory][2].
|
133
|
+
|
134
|
+
In the model class you should declare accessible views:
|
135
|
+
|
136
|
+
class Post < Couchbase::Model
|
137
|
+
attribute :title
|
138
|
+
attribute :body
|
139
|
+
attribute :draft
|
140
|
+
attribute :view_count
|
141
|
+
attribute :created_at, :default => lambda { Time.now }
|
142
|
+
|
143
|
+
view :total_count, :by_created_at, :by_view_count
|
144
|
+
end
|
145
|
+
|
146
|
+
And request them later:
|
147
|
+
|
148
|
+
Post.by_created_at(:include_docs => true).each do |post|
|
149
|
+
puts post.title
|
150
|
+
end
|
151
|
+
|
152
|
+
Post.by_view_count(:include_docs => true).group_by(&:view_count) do |count, posts|
|
153
|
+
p "#{count} -> #{posts.map{|pp| pp.inspect}.join(', ')}"
|
154
|
+
end
|
155
|
+
|
156
|
+
|
157
|
+
[1]: https://github.com/couchbase/couchbase-ruby-client/
|
158
|
+
[2]: https://github.com/couchbase/couchbase-ruby-model/blob/master/lib/rails/generators/couchbase/view/templates/
|
159
|
+
[3]: http://couchbase.com/issues/browse/RCBC
|
160
|
+
[4]: http://freenode.net/irc_servers.shtml
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Author:: Couchbase <info@couchbase.com>
|
2
|
+
# Copyright:: 2012 Couchbase, Inc.
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
require 'bundler/gem_tasks'
|
19
|
+
|
20
|
+
Dir['tasks/*.rake'].sort.each { |f| load f }
|
21
|
+
|
22
|
+
task :default => :test
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'couchbase/model/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'jmoses-couchbase-model'
|
7
|
+
s.version = Couchbase::Model::VERSION
|
8
|
+
s.author = ['Couchbase', 'Jon Moses']
|
9
|
+
s.email = ['support@couchbase.com', 'jon@burningbush.us']
|
10
|
+
s.homepage = 'https://github.com/couchbase/couchbase-ruby-model'
|
11
|
+
s.summary = %q{Declarative interface to Couchbase. (Fork for connection-pool 2.0)}
|
12
|
+
s.description = %q{ORM-like interface allows you to persist your models to Couchbase. This fork requires my forked couchbase client, which uses connection-pool 2.0 instead of 1.0}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ['lib']
|
18
|
+
|
19
|
+
s.add_runtime_dependency 'jmoses-couchbase', '~> 1.3.0'
|
20
|
+
|
21
|
+
s.add_development_dependency 'rake'
|
22
|
+
s.add_development_dependency 'minitest'
|
23
|
+
s.add_development_dependency 'activesupport'
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# Author:: Couchbase <info@couchbase.com>
|
2
|
+
# Copyright:: 2012 Couchbase, Inc.
|
3
|
+
# License:: Apache License, Version 2.0
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
#
|
17
|
+
|
18
|
+
require 'couchbase/model'
|
19
|
+
|
20
|
+
# If we are using Rails then we will include the Couchbase railtie.
|
21
|
+
if defined?(Rails)
|
22
|
+
require 'couchbase/railtie'
|
23
|
+
end
|