rufus-doric 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +8 -0
- data/lib/rufus/doric/model.rb +35 -11
- data/lib/rufus/doric/models.rb +34 -1
- data/lib/rufus/doric/one_doc_model.rb +1 -1
- data/lib/rufus/doric/version.rb +1 -1
- data/rufus-doric.gemspec +5 -2
- data/test/ut_11_model_view_range.rb +87 -0
- data/test/ut_12_model_default.rb +52 -0
- data/test/ut_13_one_doc_model_default.rb +47 -0
- data/test/ut_1_model.rb +5 -0
- data/test/ut_2_model_view.rb +1 -1
- data/test/ut_4_one_doc_model.rb +5 -0
- metadata +6 -3
data/CHANGELOG.txt
CHANGED
@@ -2,6 +2,14 @@
|
|
2
2
|
= rufus-doric CHANGELOG.txt
|
3
3
|
|
4
4
|
|
5
|
+
== rufus-doric - 0.1.5 released 2010/04/12
|
6
|
+
|
7
|
+
- implemented property :x, :default => 'y'
|
8
|
+
- added :limit, :skip, :descending, :inclusive_end to by_x and all
|
9
|
+
- added :limit and :skip to by_xxx methods (absolute and range OK)
|
10
|
+
- Schedules.by_day([ '20101204', '20110105' ]) by range implemented
|
11
|
+
|
12
|
+
|
5
13
|
== rufus-doric - 0.1.4 released 2010/04/09
|
6
14
|
|
7
15
|
- adapted to rails3 beta2
|
data/lib/rufus/doric/model.rb
CHANGED
@@ -131,7 +131,7 @@ module Doric
|
|
131
131
|
|
132
132
|
def initialize (doc={})
|
133
133
|
|
134
|
-
@h = doc.inject(
|
134
|
+
@h = doc.inject(self.class.defaults.dup) { |h, (k, v)| h[k.to_s] = v; h }
|
135
135
|
@h['doric_type'] = self.class.doric_type
|
136
136
|
end
|
137
137
|
|
@@ -278,7 +278,7 @@ module Doric
|
|
278
278
|
db.put(dd)
|
279
279
|
end
|
280
280
|
|
281
|
-
i =
|
281
|
+
i = Rufus::Doric.escape(_id)
|
282
282
|
|
283
283
|
result = db.get("_design/doric/_view/#{view}?key=#{i}&include_docs=true")
|
284
284
|
|
@@ -455,13 +455,29 @@ module Doric
|
|
455
455
|
db.put(ddoc)
|
456
456
|
end
|
457
457
|
|
458
|
+
def self.add_common_options (qs, opts)
|
459
|
+
|
460
|
+
if limit = opts[:limit]
|
461
|
+
qs << "limit=#{limit}"
|
462
|
+
end
|
463
|
+
if skip = opts[:skip]
|
464
|
+
qs << "skip=#{skip}"
|
465
|
+
end
|
466
|
+
if opts[:descending]
|
467
|
+
qs << "descending=true"
|
468
|
+
end
|
469
|
+
if opts[:inclusive_end]
|
470
|
+
qs << "inclusive_end=true"
|
471
|
+
end
|
472
|
+
end
|
473
|
+
|
458
474
|
def self.get_all (opts)
|
459
475
|
|
460
|
-
|
476
|
+
qs = [ 'include_docs=true', "key=%22#{@doric_type}%22" ]
|
461
477
|
|
462
|
-
|
463
|
-
|
464
|
-
|
478
|
+
add_common_options(qs, opts)
|
479
|
+
|
480
|
+
path = "_design/doric/_view/by_doric_type?#{qs.join('&')}"
|
465
481
|
|
466
482
|
result = get_result(path)
|
467
483
|
|
@@ -470,13 +486,21 @@ module Doric
|
|
470
486
|
|
471
487
|
def self.by (key, val, opts)
|
472
488
|
|
473
|
-
|
489
|
+
qs = [ 'include_docs=true' ]
|
490
|
+
|
491
|
+
if val.is_a?(Array)
|
492
|
+
|
493
|
+
st, en = val
|
494
|
+
qs << "startkey=#{Rufus::Doric.escape(st)}" if st
|
495
|
+
qs << "endkey=#{Rufus::Doric.escape(en)}" if en
|
496
|
+
else
|
497
|
+
|
498
|
+
qs << "key=#{Rufus::Doric.escape(val)}"
|
499
|
+
end
|
474
500
|
|
475
|
-
|
476
|
-
#v = CGI.escape(v)
|
477
|
-
v = "%22#{val}%22"
|
501
|
+
add_common_options(qs, opts)
|
478
502
|
|
479
|
-
path = "#{design_path}/_view/by_#{key}
|
503
|
+
path = "#{design_path}/_view/by_#{key}?#{qs.join('&')}"
|
480
504
|
|
481
505
|
result = get_result(path, key)
|
482
506
|
|
data/lib/rufus/doric/models.rb
CHANGED
@@ -36,6 +36,12 @@ module Doric
|
|
36
36
|
s.to_s.strip.gsub(/[\s\/:;\*\\\+\?]/, '_')
|
37
37
|
end
|
38
38
|
|
39
|
+
def self.escape (o)
|
40
|
+
|
41
|
+
#"%22#{o}%22"
|
42
|
+
CGI.escape(Rufus::Json.encode(o))
|
43
|
+
end
|
44
|
+
|
39
45
|
#
|
40
46
|
# The including classes get two new class methods : h_reader and h_accessor
|
41
47
|
# plus property as an alias to h_accessor
|
@@ -44,28 +50,55 @@ module Doric
|
|
44
50
|
|
45
51
|
def self.included (target)
|
46
52
|
|
53
|
+
def target.defaults
|
54
|
+
|
55
|
+
@defaults || {}
|
56
|
+
end
|
57
|
+
|
47
58
|
def target.known_fields
|
59
|
+
|
48
60
|
@known_fields
|
49
61
|
end
|
50
62
|
|
51
63
|
def target.h_reader (*names)
|
64
|
+
|
52
65
|
names.each do |name|
|
66
|
+
|
53
67
|
name = name.to_s
|
68
|
+
|
54
69
|
(@known_fields ||= []) << name
|
70
|
+
|
55
71
|
define_method(name) do
|
56
72
|
@h[name]
|
57
73
|
end
|
58
74
|
end
|
59
75
|
end
|
76
|
+
|
60
77
|
def target.h_accessor (*names)
|
78
|
+
|
79
|
+
default = nil
|
80
|
+
|
81
|
+
if names.last.is_a?(Hash)
|
82
|
+
opts = names.pop
|
83
|
+
default = opts[:default]
|
84
|
+
end
|
85
|
+
|
61
86
|
h_reader(*names)
|
87
|
+
|
62
88
|
names.each do |name|
|
89
|
+
|
90
|
+
name = name.to_s
|
91
|
+
|
92
|
+
(@defaults ||= {})[name] = default if default
|
93
|
+
|
63
94
|
define_method("#{name}=") do |v|
|
64
|
-
@h[name
|
95
|
+
@h[name] = v
|
65
96
|
end
|
66
97
|
end
|
67
98
|
end
|
99
|
+
|
68
100
|
def target.property (*names)
|
101
|
+
|
69
102
|
h_accessor(*names)
|
70
103
|
end
|
71
104
|
end
|
data/lib/rufus/doric/version.rb
CHANGED
data/rufus-doric.gemspec
CHANGED
@@ -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.
|
8
|
+
s.version = "0.1.5"
|
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-04-
|
12
|
+
s.date = %q{2010-04-12}
|
13
13
|
s.description = %q{
|
14
14
|
something at the intersection of Rails3, CouchDB and rufus-jig
|
15
15
|
}
|
@@ -47,6 +47,9 @@ something at the intersection of Rails3, CouchDB and rufus-jig
|
|
47
47
|
"test/test.rb",
|
48
48
|
"test/ut_0_fixtures.rb",
|
49
49
|
"test/ut_10_value.rb",
|
50
|
+
"test/ut_11_model_view_range.rb",
|
51
|
+
"test/ut_12_model_default.rb",
|
52
|
+
"test/ut_13_one_doc_model_default.rb",
|
50
53
|
"test/ut_1_model.rb",
|
51
54
|
"test/ut_2_model_view.rb",
|
52
55
|
"test/ut_3_model_lint.rb",
|
@@ -0,0 +1,87 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# testing rufus-doric
|
4
|
+
#
|
5
|
+
# Mon Apr 12 10:38:53 JST 2010
|
6
|
+
#
|
7
|
+
|
8
|
+
require File.join(File.dirname(__FILE__), 'base')
|
9
|
+
|
10
|
+
require 'rufus/doric'
|
11
|
+
|
12
|
+
|
13
|
+
class Schedule < Rufus::Doric::Model
|
14
|
+
|
15
|
+
db :doric
|
16
|
+
doric_type :schedules
|
17
|
+
|
18
|
+
_id_field :name
|
19
|
+
h_accessor :name
|
20
|
+
h_accessor :day #yyyymmdd
|
21
|
+
|
22
|
+
view_by :day
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
class UtModelViewRangeTest < Test::Unit::TestCase
|
27
|
+
|
28
|
+
def setup
|
29
|
+
|
30
|
+
Rufus::Doric.db('doric').delete('.')
|
31
|
+
Rufus::Doric.db('doric').put('.')
|
32
|
+
|
33
|
+
Rufus::Doric.db('doric').http.cache.clear
|
34
|
+
# CouchDB feeds the same etags for views, even after a db has
|
35
|
+
# been deleted and put back, so have to do that 'forgetting'
|
36
|
+
|
37
|
+
Schedule.new('name' => 'wrestling', 'day' => '20101224').save!
|
38
|
+
Schedule.new('name' => 'shopping', 'day' => '20101224').save!
|
39
|
+
Schedule.new('name' => 'cooking', 'day' => '20101225').save!
|
40
|
+
Schedule.new('name' => 'climbing', 'day' => '20101226').save!
|
41
|
+
Schedule.new('name' => 'drinking', 'day' => '20101227').save!
|
42
|
+
end
|
43
|
+
|
44
|
+
#def teardown
|
45
|
+
#end
|
46
|
+
|
47
|
+
def test_view_by_range
|
48
|
+
|
49
|
+
assert_equal 'shopping', Schedule.by_day('20101224').first.name
|
50
|
+
|
51
|
+
assert_equal(
|
52
|
+
%w[ climbing drinking ],
|
53
|
+
Schedule.by_day([ '20101226', nil ]).collect { |s| s.name })
|
54
|
+
assert_equal(
|
55
|
+
%w[ climbing drinking ],
|
56
|
+
Schedule.by_day([ '20101226' ]).collect { |s| s.name })
|
57
|
+
|
58
|
+
assert_equal(
|
59
|
+
%w[ shopping wrestling cooking ],
|
60
|
+
Schedule.by_day([ nil, '20101225' ]).collect { |s| s.name })
|
61
|
+
|
62
|
+
assert_equal(
|
63
|
+
%w[ drinking climbing cooking ],
|
64
|
+
Schedule.by_day([ nil, '20101225' ], :descending => true).collect { |s| s.name })
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_view_limit
|
68
|
+
|
69
|
+
assert_equal(
|
70
|
+
%w[ shopping ],
|
71
|
+
Schedule.by_day('20101224', :limit => 1).collect { |s| s.name })
|
72
|
+
|
73
|
+
assert_equal(
|
74
|
+
%w[ climbing ],
|
75
|
+
Schedule.by_day([ '20101226', nil ], :limit => 1).collect { |s| s.name })
|
76
|
+
assert_equal(
|
77
|
+
%w[ drinking ],
|
78
|
+
Schedule.by_day([ '20101226', nil ], :skip => 1).collect { |s| s.name })
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_all_opts
|
82
|
+
|
83
|
+
assert_equal 4, Schedule.all(:skip => 1).size
|
84
|
+
assert_equal 2, Schedule.all(:skip => 2, :limit => 2).size
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# testing rufus-doric
|
4
|
+
#
|
5
|
+
# Mon Apr 12 14:32:35 JST 2010
|
6
|
+
#
|
7
|
+
|
8
|
+
require File.join(File.dirname(__FILE__), 'base')
|
9
|
+
|
10
|
+
require 'rufus/doric'
|
11
|
+
|
12
|
+
|
13
|
+
class Can < Rufus::Doric::Model
|
14
|
+
|
15
|
+
db :doric
|
16
|
+
doric_type :cans
|
17
|
+
|
18
|
+
_id_field :serial
|
19
|
+
h_accessor :serial
|
20
|
+
h_accessor :content, :default => 'tuna'
|
21
|
+
h_accessor :colour
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
class UtModelDefaultTest < Test::Unit::TestCase
|
26
|
+
|
27
|
+
def setup
|
28
|
+
|
29
|
+
Rufus::Doric.db('doric').delete('.')
|
30
|
+
Rufus::Doric.db('doric').put('.')
|
31
|
+
|
32
|
+
Rufus::Doric.db('doric').http.cache.clear
|
33
|
+
# CouchDB feeds the same etags for views, even after a db has
|
34
|
+
# been deleted and put back, so have to do that 'forgetting'
|
35
|
+
end
|
36
|
+
|
37
|
+
#def teardown
|
38
|
+
#end
|
39
|
+
|
40
|
+
def test_default
|
41
|
+
|
42
|
+
Can.new(:serial => 'abcd', :content => 'anchovy').save!
|
43
|
+
Can.new(:serial => 'efgh').save!
|
44
|
+
|
45
|
+
assert_equal({ 'content' => 'tuna' }, Can.defaults)
|
46
|
+
|
47
|
+
assert_equal 'anchovy', Can.find('abcd').content
|
48
|
+
assert_equal 'tuna', Can.find('efgh').content
|
49
|
+
assert_equal nil, Can.find('abcd').colour
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# testing rufus-doric
|
4
|
+
#
|
5
|
+
# Mon Apr 12 14:48:28 JST 2010
|
6
|
+
#
|
7
|
+
|
8
|
+
require File.join(File.dirname(__FILE__), 'base')
|
9
|
+
|
10
|
+
require 'rufus/doric'
|
11
|
+
|
12
|
+
|
13
|
+
class Place < Rufus::Doric::OneDocModel
|
14
|
+
|
15
|
+
doc_id :places
|
16
|
+
db :doric
|
17
|
+
|
18
|
+
h_accessor :name
|
19
|
+
h_accessor :zip
|
20
|
+
h_accessor :country, :default => 'UK'
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
class UtOneDocModelDefaultTest < Test::Unit::TestCase
|
25
|
+
|
26
|
+
#def setup
|
27
|
+
# Rufus::Doric.db('doric').delete('.')
|
28
|
+
# Rufus::Doric.db('doric').put('.')
|
29
|
+
#end
|
30
|
+
#def teardown
|
31
|
+
#end
|
32
|
+
|
33
|
+
def test_defaults
|
34
|
+
|
35
|
+
assert_equal({ 'country' => 'UK' }, Place.defaults)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_default
|
39
|
+
|
40
|
+
london = Place.new(:name => 'London')
|
41
|
+
berlin = Place.new(:name => 'Berlin', :country => 'Germany')
|
42
|
+
|
43
|
+
assert_equal 'UK', london.country
|
44
|
+
assert_equal 'Germany', berlin.country
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
data/test/ut_1_model.rb
CHANGED
data/test/ut_2_model_view.rb
CHANGED
data/test/ut_4_one_doc_model.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 5
|
9
|
+
version: 0.1.5
|
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-04-
|
17
|
+
date: 2010-04-12 00:00:00 +09:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -134,6 +134,9 @@ files:
|
|
134
134
|
- test/test.rb
|
135
135
|
- test/ut_0_fixtures.rb
|
136
136
|
- test/ut_10_value.rb
|
137
|
+
- test/ut_11_model_view_range.rb
|
138
|
+
- test/ut_12_model_default.rb
|
139
|
+
- test/ut_13_one_doc_model_default.rb
|
137
140
|
- test/ut_1_model.rb
|
138
141
|
- test/ut_2_model_view.rb
|
139
142
|
- test/ut_3_model_lint.rb
|