couchbase-model 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -1,3 +1,12 @@
1
+ ## 0.5.1 / 2013-02-25
2
+
3
+ * Fix attribute inheritance when subclassing (Mike Evans)
4
+ * Added as_json method for rails JSON responses (Stephen von Takach)
5
+ * Add contributing document
6
+ * Fix test hiding
7
+ * Remove comments from the javascript sources
8
+ * Reduce development dependencies and update jar version
9
+
1
10
  ## 0.5.1 / 2012-11-29
2
11
 
3
12
  * Introduce save! and create! methods and raise RecordInvalid only from them
@@ -125,7 +125,7 @@ To generate view you can use yet another generator `rails generate
125
125
  couchbase:view DESIGNDOCNAME VIEWNAME`. For example how `total_count`
126
126
  view could be generated:
127
127
 
128
- $ rails generate link total_count
128
+ $ rails generate couchbase:view link total_count
129
129
 
130
130
  The generated files contains useful info and links about how to write
131
131
  map and reduce functions, you can take a look at them in the [templates
@@ -155,6 +155,6 @@ And request them later:
155
155
 
156
156
 
157
157
  [1]: https://github.com/couchbase/couchbase-ruby-client/
158
- [2]: https://github.com/couchbaselabs/ruby-couchbase-model/tree/master/lib/rails/generators/couchbase/view/templates/
158
+ [2]: https://github.com/couchbase/couchbase-ruby-model/blob/master/lib/rails/generators/couchbase/view/templates/
159
159
  [3]: http://couchbase.com/issues/browse/RCBC
160
160
  [4]: http://freenode.net/irc_servers.shtml
@@ -18,9 +18,6 @@ Gem::Specification.new do |s|
18
18
 
19
19
  s.add_runtime_dependency 'couchbase', '~> 1.2.0.dp'
20
20
 
21
- s.add_development_dependency 'rake', '~> 0.8.7'
21
+ s.add_development_dependency 'rake'
22
22
  s.add_development_dependency 'minitest'
23
- s.add_development_dependency 'rdiscount'
24
- s.add_development_dependency 'yard'
25
- s.add_development_dependency 'debugger'
26
23
  end
@@ -124,10 +124,10 @@ module Couchbase
124
124
  attr_reader :raw
125
125
 
126
126
  # @private Container for all attributes with defaults of all subclasses
127
- @@attributes = ::Hash.new {|hash, key| hash[key] = {}}
127
+ @@attributes = {}
128
128
 
129
129
  # @private Container for all view names of all subclasses
130
- @@views = ::Hash.new {|hash, key| hash[key] = {}}
130
+ @@views = {}
131
131
 
132
132
  # Use custom connection options
133
133
  #
@@ -234,7 +234,7 @@ module Couchbase
234
234
  Configuration.design_documents_paths.each do |path|
235
235
  ff = File.join(path, design_document.to_s, name.to_s, "#{type}.js")
236
236
  if File.file?(ff)
237
- contents = File.read(ff).strip
237
+ contents = File.read(ff).gsub(/^\s*\/\/.*$\n\r?/, '').strip
238
238
  next if contents.empty?
239
239
  mtime = [mtime, File.mtime(ff).to_i].max
240
240
  digest << contents
@@ -640,7 +640,11 @@ module Couchbase
640
640
  #
641
641
  # @return [Hash]
642
642
  def self.attributes
643
- @@attributes[self]
643
+ @attributes ||= if self == Model
644
+ @@attributes.dup
645
+ else
646
+ couchbase_ancestor.attributes.dup
647
+ end
644
648
  end
645
649
 
646
650
  # All defined views within a class.
@@ -651,7 +655,20 @@ module Couchbase
651
655
  #
652
656
  # @return [Array]
653
657
  def self.views
654
- @@views[self]
658
+ @views ||= if self == Model
659
+ @@views.dup
660
+ else
661
+ couchbase_ancestor.views.dup
662
+ end
663
+ end
664
+
665
+ # Returns the first ancestor that is also a Couchbase::Model ancestor.
666
+ #
667
+ # @return Class
668
+ def self.couchbase_ancestor
669
+ ancestors[1..-1].each do |ancestor|
670
+ return ancestor if ancestor.ancestors.include?(Couchbase::Model)
671
+ end
655
672
  end
656
673
 
657
674
  # All the attributes of the current instance
@@ -693,6 +710,16 @@ module Couchbase
693
710
  self
694
711
  end
695
712
 
713
+ # Format the model for use in a JSON response
714
+ #
715
+ # @since 0.5.2
716
+ #
717
+ # @return [Hash] a JSON representation of the model for REST APIs
718
+ #
719
+ def as_json(options = {})
720
+ attributes.merge({:id => @id}).as_json(options)
721
+ end
722
+
696
723
  # @private The thread local storage for model specific stuff
697
724
  #
698
725
  # @since 0.0.1
@@ -19,7 +19,7 @@ module Couchbase
19
19
 
20
20
  class Model
21
21
 
22
- VERSION = "0.5.1"
22
+ VERSION = "0.5.2"
23
23
 
24
24
  end
25
25
 
@@ -19,7 +19,7 @@ require 'rake/testtask'
19
19
  require 'rake/clean'
20
20
 
21
21
  rule 'test/CouchbaseMock.jar' do |task|
22
- jar_path = "0.5-SNAPSHOT/CouchbaseMock-0.5-20120222.060643-15.jar"
22
+ jar_path = "0.5-SNAPSHOT/CouchbaseMock-0.5-20120726.220757-19.jar"
23
23
  sh %{wget -q -O test/CouchbaseMock.jar http://files.couchbase.com/maven2/org/couchbase/mock/CouchbaseMock/#{jar_path}}
24
24
  end
25
25
 
@@ -27,7 +27,6 @@ CLOBBER << 'test/CouchbaseMock.jar'
27
27
 
28
28
  Rake::TestTask.new do |test|
29
29
  test.libs << "test" << "."
30
- test.ruby_opts << "-rruby-debug" if ENV['DEBUG']
31
30
  test.pattern = 'test/test_*.rb'
32
31
  test.options = '--verbose'
33
32
  end
@@ -17,5 +17,5 @@
17
17
 
18
18
  desc 'Start an irb session and load the library.'
19
19
  task :console do
20
- exec "irb -I lib -rruby-debug -rcouchbase-model"
20
+ exec "irb -I lib -rcouchbase-model"
21
21
  end
@@ -45,6 +45,11 @@ class Attachment < Couchbase::Model
45
45
  defaults :format => :plain
46
46
  end
47
47
 
48
+ class Comments < Couchbase::Model
49
+ include Enumerable
50
+ attribute :comments, :default => []
51
+ end
52
+
48
53
  class TestModel < MiniTest::Unit::TestCase
49
54
 
50
55
  def setup
@@ -126,12 +131,12 @@ class TestModel < MiniTest::Unit::TestCase
126
131
  end
127
132
 
128
133
  def test_it_raises_not_found_exception
129
- assert_raises Couchbase::Error::MissingId do
134
+ assert_raises Couchbase::Error::NotFound do
130
135
  Post.find("missing_key")
131
136
  end
132
137
  end
133
138
 
134
- def test_it_raises_not_found_exception
139
+ def test_it_returns_nil_when_key_not_found
135
140
  refute Post.find_by_id("missing_key")
136
141
  end
137
142
 
@@ -221,6 +226,16 @@ class TestModel < MiniTest::Unit::TestCase
221
226
  assert_equal "the-key", Post.new(:key => ["the", "key"]).to_param
222
227
  end
223
228
 
229
+ def test_as_json
230
+ require 'active_support/json/encoding'
231
+
232
+ response = {'id' => 'the-id'}
233
+ assert_equal response, Post.new(:id => "the-id").as_json
234
+
235
+ response = {}
236
+ assert_equal response, Post.new(:id => "the-id").as_json(:except => :id)
237
+ end
238
+
224
239
  def test_validation
225
240
  post = ValidPost.create(:title => 'Hello, World!')
226
241
  assert post.valid?, "post with title should be valid"
@@ -242,4 +257,8 @@ class TestModel < MiniTest::Unit::TestCase
242
257
  assert_equal contents, blob.raw
243
258
  end
244
259
 
260
+ def test_couchbase_ancestor
261
+ assert_equal Couchbase::Model, Comments.couchbase_ancestor
262
+ end
263
+
245
264
  end
@@ -0,0 +1,76 @@
1
+ # Author:: Couchbase <info@couchbase.com>
2
+ # Copyright:: 2011, 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 File.join(File.dirname(__FILE__), 'setup')
19
+
20
+ class Program < Couchbase::Model
21
+ attribute :title
22
+ attribute :genres, :default => []
23
+ end
24
+
25
+ class Movie < Program
26
+ attribute :mpaa_rating
27
+ attribute :runtime
28
+ end
29
+
30
+ class Series < Program
31
+ attribute :vchip_rating
32
+ end
33
+
34
+ class Episode < Series
35
+ attribute :runtime
36
+ end
37
+
38
+ class TestModelRailsIntegration < MiniTest::Unit::TestCase
39
+
40
+ def test_class_attributes_are_inheritable
41
+ program_attributes = [:title, :genres]
42
+ movie_attributes = [:mpaa_rating, :runtime]
43
+ series_attributes = [:vchip_rating]
44
+ episode_attributes = [:runtime]
45
+
46
+ assert_equal program_attributes, Program.attributes.keys
47
+ assert_equal program_attributes + movie_attributes, Movie.attributes.keys
48
+ assert_equal program_attributes + series_attributes, Series.attributes.keys
49
+ assert_equal program_attributes + series_attributes + episode_attributes, Episode.attributes.keys
50
+ end
51
+
52
+ def test_default_attributes_are_inheritable
53
+ assert_equal nil, Movie.attributes[:title]
54
+ assert_equal [], Movie.attributes[:genres]
55
+ end
56
+
57
+ def test_instance_attributes_are_inheritable
58
+ episode = Episode.new(:title => 'Family Guy', :genres => ['Comedy'], :vchip_rating => 'TVPG', :runtime => 30)
59
+
60
+ assert_equal [:title, :genres, :vchip_rating, :runtime], episode.attributes.keys
61
+ assert_equal 'Family Guy', episode.title
62
+ assert_equal ['Comedy'], episode.genres
63
+ assert_equal 30, episode.runtime
64
+ assert_equal 'TVPG', episode.vchip_rating
65
+ end
66
+
67
+ def test_class_attributes_from_subclasses_do_not_propogate_up_ancestor_chain
68
+ assert_equal [:title, :genres, :vchip_rating], Series.attributes.keys
69
+ end
70
+
71
+ def test_instance_attributes_from_subclasses_do_not_propogate_up_ancestor_chain
72
+ series = Series.new(:title => 'Family Guy', :genres => ['Comedy'], :vchip_rating => 'TVPG')
73
+ assert_equal [:title, :genres, :vchip_rating], series.attributes.keys
74
+ end
75
+
76
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchbase-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-29 00:00:00.000000000 Z
12
+ date: 2013-02-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: couchbase
@@ -29,22 +29,6 @@ dependencies:
29
29
  version: 1.2.0.dp
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: rake
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ~>
36
- - !ruby/object:Gem::Version
37
- version: 0.8.7
38
- type: :development
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 0.8.7
46
- - !ruby/object:Gem::Dependency
47
- name: minitest
48
32
  requirement: !ruby/object:Gem::Requirement
49
33
  none: false
50
34
  requirements:
@@ -60,39 +44,7 @@ dependencies:
60
44
  - !ruby/object:Gem::Version
61
45
  version: '0'
62
46
  - !ruby/object:Gem::Dependency
63
- name: rdiscount
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :development
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
- - !ruby/object:Gem::Dependency
79
- name: yard
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ! '>='
84
- - !ruby/object:Gem::Version
85
- version: '0'
86
- type: :development
87
- prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
91
- - - ! '>='
92
- - !ruby/object:Gem::Version
93
- version: '0'
94
- - !ruby/object:Gem::Dependency
95
- name: debugger
47
+ name: minitest
96
48
  requirement: !ruby/object:Gem::Requirement
97
49
  none: false
98
50
  requirements:
@@ -116,6 +68,7 @@ files:
116
68
  - .gitignore
117
69
  - .travis.yml
118
70
  - .yardopts
71
+ - CONTRIBUTING.markdown
119
72
  - Gemfile
120
73
  - HISTORY.markdown
121
74
  - README.markdown
@@ -136,12 +89,12 @@ files:
136
89
  - lib/rails/generators/couchbase/view/templates/reduce.js
137
90
  - lib/rails/generators/couchbase/view/view_generator.rb
138
91
  - lib/rails/generators/couchbase_generator.rb
139
- - tasks/doc.rake
140
92
  - tasks/package.rake
141
93
  - tasks/test.rake
142
94
  - tasks/util.rake
143
95
  - test/setup.rb
144
96
  - test/test_model.rb
97
+ - test/test_model_rails_integration.rb
145
98
  - test/test_uuid.rb
146
99
  homepage: https://github.com/couchbase/couchbase-ruby-model
147
100
  licenses: []
@@ -1,25 +0,0 @@
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 'rake/clean'
19
- require 'yard'
20
- require 'yard/rake/yardoc_task'
21
-
22
- YARD::Rake::YardocTask.new do |t|
23
- t.options = %w(--protected --no-private)
24
- t.files.push('-', 'README.markdown', 'HISTORY.markdown')
25
- end