rufus-doric 0.1.13 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,11 @@
2
2
  = rufus-doric CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-doric - 0.1.14 released 2010/05/22
6
+
7
+ - map and now, map and reduce (Model.view_by)
8
+
9
+
5
10
  == rufus-doric - 0.1.13 released 2010/05/21
6
11
 
7
12
  - fixed issue with model and missing association
@@ -160,11 +160,11 @@ module Doric
160
160
  #
161
161
  # The all method supports the same options.
162
162
  #
163
- def self.view_by (key, func=nil)
163
+ def self.view_by (key, map=nil, reduce=nil)
164
164
 
165
- if func
165
+ if map
166
166
 
167
- k = { key => func }
167
+ k = { :view => key, :map => map, :reduce => reduce }
168
168
 
169
169
  instance_eval %{
170
170
  def by_#{key} (val, opts={})
@@ -581,13 +581,22 @@ module Doric
581
581
  "#{self.class.doric_type}__#{s}"
582
582
  end
583
583
 
584
- def self.func (body)
585
- %{
586
- function (doc) {
587
- if (doc.doric_type != '#{@doric_type}') return;
588
- #{body}
584
+ def self.func (body, type=:map)
585
+
586
+ if type == :map
587
+ %{
588
+ function (doc) {
589
+ if (doc.doric_type != '#{@doric_type}') return;
590
+ #{body}
591
+ }
589
592
  }
590
- }
593
+ else
594
+ %{
595
+ function (keys, values, rereduce) {
596
+ #{body}
597
+ }
598
+ }
599
+ end
591
600
  end
592
601
 
593
602
  def self.put_design_doc (key=nil)
@@ -626,9 +635,15 @@ module Doric
626
635
 
627
636
  elsif key.is_a?(Hash)
628
637
 
629
- ddoc['views']["by_#{key.keys.first}"] = {
630
- 'map' => func(key.values.first)
631
- }
638
+ h = {}
639
+
640
+ h['map'] = func(key[:map])
641
+
642
+ if red = key[:reduce]
643
+ h['reduce'] = func(red, :reduce)
644
+ end
645
+
646
+ ddoc['views']["by_#{key[:view]}"] = h
632
647
 
633
648
  elsif key.is_a?(Array)
634
649
 
@@ -697,7 +712,9 @@ module Doric
697
712
 
698
713
  #p [ key, val, opts ]
699
714
 
700
- qs = [ 'include_docs=true' ]
715
+ reduce = key.is_a?(Hash) && key[:reduce]
716
+
717
+ qs = reduce ? [] : [ 'include_docs=true' ]
701
718
 
702
719
  if is_option_hash(val)
703
720
  opts = val
@@ -716,7 +733,7 @@ module Doric
716
733
 
717
734
  skey = case key
718
735
  when Array then key.join('_and_')
719
- when Hash then key.keys.first
736
+ when Hash then key[:view]
720
737
  else key
721
738
  end
722
739
 
@@ -724,7 +741,14 @@ module Doric
724
741
 
725
742
  result = get_result(path, key)
726
743
 
727
- result['rows'].collect { |r| self.new(r['doc']) }
744
+ row = result['rows']
745
+
746
+ if reduce
747
+ r = result['rows'].first
748
+ r ? r['value'] : nil
749
+ else
750
+ result['rows'].collect { |r| self.new(r['doc']) }
751
+ end
728
752
  end
729
753
 
730
754
  # Ensures the necessary design_doc is loaded (if first query failed)
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Rufus
3
3
  module Doric
4
- VERSION = '0.1.13'
4
+ VERSION = '0.1.14'
5
5
  end
6
6
  end
7
7
 
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rufus-doric}
8
- s.version = "0.1.13"
8
+ s.version = "0.1.14"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Mettraux"]
12
- s.date = %q{2010-05-21}
12
+ s.date = %q{2010-05-22}
13
13
  s.description = %q{
14
14
  something at the intersection of Rails3, CouchDB and rufus-jig
15
15
  }
@@ -55,6 +55,7 @@ something at the intersection of Rails3, CouchDB and rufus-jig
55
55
  "test/ut_15_model_view_by_and.rb",
56
56
  "test/ut_16_model_custom_view.rb",
57
57
  "test/ut_17_model_and_attachments.rb",
58
+ "test/ut_18_model_map_reduce.rb",
58
59
  "test/ut_1_model.rb",
59
60
  "test/ut_2_model_view.rb",
60
61
  "test/ut_3_model_lint.rb",
@@ -27,6 +27,32 @@ class Team < Rufus::Doric::Model
27
27
  }
28
28
  end
29
29
 
30
+ #class City < Rufus::Doric::Model
31
+ #
32
+ # db :doric
33
+ # doric_type :cities
34
+ #
35
+ # id_field :name
36
+ #
37
+ # h_accessor :name
38
+ # h_accessor :sister
39
+ #
40
+ # view_by 'sister', %{
41
+ # emit(doc.sister, null);
42
+ # }
43
+ #end
44
+ #
45
+ # def test_custom_properties_should_not_overwrite_properties
46
+ #
47
+ # City.new('name' => 'Zurich', 'sister' => 'Kunming').save!
48
+ # City.new('name' => 'Yokohama', 'sister' => 'Vancouver').save!
49
+ # City.new('name' => 'Portland', 'sister' => 'Sapporo').save!
50
+ # City.new('name' => 'Nara', 'sister' => 'Toledo').save!
51
+ # City.new('name' => "Xi'an", 'sister' => 'Quebec').save!
52
+ # end
53
+ #
54
+ # keeping for a next round of testing
55
+
30
56
 
31
57
  class UtModelCustomViewTest < Test::Unit::TestCase
32
58
 
@@ -0,0 +1,65 @@
1
+
2
+ #
3
+ # testing rufus-doric
4
+ #
5
+ # Fri May 21 23:41:59 JST 2010
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'base')
9
+
10
+ require 'rufus/doric'
11
+
12
+
13
+ class Auth < Rufus::Doric::Model
14
+
15
+ db :doric
16
+ doric_type :auths
17
+
18
+ h_accessor :role
19
+ h_accessor :user
20
+
21
+ view_by 'roles', %{
22
+ emit(null, doc.role);
23
+ }, %{
24
+ // unique ...
25
+ var a = [];
26
+ for (var i = 0; i < values.length; i++) {
27
+ var v = values[i];
28
+ if (a.indexOf(v) < 0) a.push(v);
29
+ }
30
+ return a;
31
+ }
32
+ end
33
+
34
+
35
+ class UtModelMapReduceTest < Test::Unit::TestCase
36
+
37
+ def setup
38
+
39
+ Rufus::Doric.db('doric').delete('.')
40
+ Rufus::Doric.db('doric').put('.')
41
+
42
+ Rufus::Doric.db('doric').http.cache.clear
43
+ # CouchDB feeds the same etags for views, even after a db has
44
+ # been deleted and put back, so have to do that 'forgetting'
45
+ end
46
+
47
+ #def teardown
48
+ #end
49
+
50
+ def test_mr
51
+
52
+ Auth.new('role' => 'president', 'user' => 'Amedeo').save!
53
+ Auth.new('role' => 'prime minister', 'user' => 'Claudio').save!
54
+ Auth.new('role' => 'minister of war', 'user' => 'Sergio').save!
55
+ Auth.new('role' => 'minister of war', 'user' => 'John').save!
56
+
57
+ assert_equal(
58
+ [ 'president', 'prime minister', 'minister of war' ],
59
+ Auth.roles(nil))
60
+ assert_equal(
61
+ [ 'president', 'prime minister', 'minister of war' ],
62
+ Auth.by_roles(nil))
63
+ end
64
+ end
65
+
@@ -205,5 +205,14 @@ class UtModelTest < Test::Unit::TestCase
205
205
  Thing.find(nil)
206
206
  end
207
207
  end
208
+
209
+ def test_various_ids
210
+
211
+ Thing.new('name' => "Xi'an").save!
212
+ assert_not_nil Thing.find("Xi'an")
213
+ #Thing.all.each do |thing|
214
+ # p thing
215
+ #end
216
+ end
208
217
  end
209
218
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 13
9
- version: 0.1.13
8
+ - 14
9
+ version: 0.1.14
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Mettraux
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-21 00:00:00 +09:00
17
+ date: 2010-05-22 00:00:00 +09:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -142,6 +142,7 @@ files:
142
142
  - test/ut_15_model_view_by_and.rb
143
143
  - test/ut_16_model_custom_view.rb
144
144
  - test/ut_17_model_and_attachments.rb
145
+ - test/ut_18_model_map_reduce.rb
145
146
  - test/ut_1_model.rb
146
147
  - test/ut_2_model_view.rb
147
148
  - test/ut_3_model_lint.rb