relaxo-model 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85f3d648fadd1ad073b9ba8b19c4bc343022143f
4
- data.tar.gz: 6da3f03fcdcfcae4a6ada3e1a856649e135c407d
3
+ metadata.gz: b1ae3a29f77dae4356693e9b75175407e1c6d867
4
+ data.tar.gz: 432e8decdccadbfeacb067439e918256183b6f68
5
5
  SHA512:
6
- metadata.gz: 73c64d7d6b81ba525e7348205e400213876f5b7c675f8c299c55497fae3f8eb6125f21fc7297dfb70d721dc598287df73678538a663ae6e5a07a855dba53d778
7
- data.tar.gz: ab3091e7dd54e8fceec551486522e8822368109b57a684c6c769374ebb9d713135ac51c2014b19c71cd099f0c1fa95145022ac6dfbc9db80f8020efd6605aa30
6
+ metadata.gz: 204332daa91dcf3887f22fe3d2e0e597616a6d944af12c44887812245188f2315b540dae775d9f1f044999a3b1a2c7c33d7f215a9e9f3ad85262db738bbe3cb8
7
+ data.tar.gz: b76b115b30e5b612b5ef974660e51463fca035a235e88fca2462371b80b4097f740e10ab103d9f813ddc5b69221e2a2f7553287a00bedf9bef39dc9632c5047a
data/.gitignore CHANGED
@@ -12,6 +12,7 @@ lib/bundler/man
12
12
  pkg
13
13
  rdoc
14
14
  spec/reports
15
+ spec/relaxo/model/test
15
16
  test/tmp
16
17
  test/version_tmp
17
18
  tmp
data/README.md CHANGED
@@ -10,41 +10,82 @@ Relaxo Model provides a framework for business logic on top of Relaxo, a documen
10
10
 
11
11
  Here is a simple example of a traditional ORM style model:
12
12
 
13
- require 'relaxo/model'
13
+ ```ruby
14
+ require 'relaxo/model'
14
15
 
15
- database = Relaxo.connect("test")
16
+ database = Relaxo.connect("test")
16
17
 
17
- trees = [
18
+ trees = [
19
+ {:name => 'Hinoki', :planted => Date.parse("2013/11/17")},
20
+ {:name => 'Keyaki', :planted => Date.parse("2016/9/24")}
21
+ ]
22
+
23
+ class Tree
24
+ include Relaxo::Model
25
+
26
+ property :id, UUID
27
+ property :name
28
+ property :planted, Attribute[Date]
29
+
30
+ view :all, [:type], index: [:id]
31
+ end
32
+
33
+ database.commit(message: "Create trees") do |changeset|
34
+ trees.each do |tree|
35
+ Tree.insert(changeset, tree)
36
+ end
37
+ end
38
+
39
+ database.current do |dataset|
40
+ Tree.all(dataset).each do |tree|
41
+ puts "A #{tree.name} was planted on #{tree.planted.to_s}."
42
+
43
+ # Expected output:
44
+ # A Hinoki was planted on 2013-11-17.
45
+ # A Keyaki was planted on 2016-09-24.
46
+ end
47
+ end
48
+ ```
49
+
50
+ ### Non-UUID Primary Key
51
+
52
+ ```ruby
53
+ #!/usr/bin/env ruby
54
+
55
+ gem 'relaxo'
56
+
57
+ require 'relaxo/model'
58
+
59
+ database = Relaxo.connect("test")
60
+
61
+ trees = [
18
62
  {:name => 'Hinoki', :planted => Date.parse("2013/11/17")},
19
63
  {:name => 'Keyaki', :planted => Date.parse("2016/9/24")}
20
- ]
64
+ ]
21
65
 
22
- class Tree
66
+ class Tree
23
67
  include Relaxo::Model
24
-
25
- property :id, UUID
68
+
26
69
  property :name
27
70
  property :planted, Attribute[Date]
28
-
29
- view :all, [:type], index: [:id]
30
- end
31
71
 
32
- database.commit(message: "Create trees") do |changeset|
72
+ view :all, [:type], index: [:name]
73
+ end
74
+
75
+ database.commit(message: "Create trees") do |changeset|
33
76
  trees.each do |tree|
34
- Tree.insert(changeset, tree)
77
+ Tree.insert(changeset, tree)
35
78
  end
36
- end
37
-
38
- database.current do |dataset|
39
- Tree.all(dataset).each do |tree|
40
- puts "A #{tree.name} was planted on #{tree.planted.to_s}."
79
+ end
41
80
 
42
- # Expected output:
43
- # A Hinoki was planted on 2013-11-17.
44
- # A Keyaki was planted on 2016-09-24.
45
- end
81
+ database.current do |dataset|
82
+ trees.each do |tree|
83
+ object = Tree.fetch_all(dataset, name: tree[:name])
84
+ puts object
46
85
  end
47
-
86
+ end
87
+ ```
88
+
48
89
  ## Contributing
49
90
 
50
91
  1. Fork it
@@ -19,6 +19,7 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require_relative 'recordset'
22
+ require 'uri'
22
23
 
23
24
  module Relaxo
24
25
  module Model
@@ -39,11 +40,19 @@ module Relaxo
39
40
  end
40
41
 
41
42
  def object_path(model, **arguments)
42
- resolve(self.prefix + self.index, model, **arguments).join('/')
43
+ encode resolve(self.prefix + self.index, model, **arguments)
43
44
  end
44
45
 
45
46
  def prefix_path(model, **arguments)
46
- resolve(self.prefix, model, **arguments).join('/')
47
+ encode resolve(self.prefix, model, **arguments)
48
+ end
49
+
50
+ private
51
+
52
+ ENCODE = {'/' => '%2F', '%' => '%25'}
53
+
54
+ def encode(path)
55
+ path.collect{|part| part.to_s.gsub(/[\/%]/, ENCODE)}.join('/')
47
56
  end
48
57
  end
49
58
 
@@ -44,6 +44,11 @@ module Relaxo
44
44
  end
45
45
  end
46
46
 
47
+ def reload
48
+ @changed.clear
49
+ self.load_object
50
+ end
51
+
47
52
  def dump
48
53
  flatten!
49
54
 
@@ -191,10 +191,6 @@ module Relaxo
191
191
  return self
192
192
  end
193
193
 
194
- def reload(dataset)
195
- @dataset = dataset
196
- end
197
-
198
194
  def before_delete
199
195
  end
200
196
 
@@ -207,7 +203,7 @@ module Relaxo
207
203
  @changed.clear
208
204
 
209
205
  paths.each do |path|
210
- @dataset.delete(path)
206
+ dataset.delete(path)
211
207
  end
212
208
 
213
209
  after_delete
@@ -29,9 +29,9 @@ module Relaxo
29
29
  @model = model
30
30
  end
31
31
 
32
- attr :klass
33
- attr :database
34
- attr :view
32
+ attr :dataset
33
+ attr :path
34
+ attr :model
35
35
 
36
36
  def empty?
37
37
  !@dataset.each(@path).any?
@@ -20,6 +20,6 @@
20
20
 
21
21
  module Relaxo
22
22
  module Model
23
- VERSION = "0.9.0"
23
+ VERSION = "0.10.0"
24
24
  end
25
25
  end
data/relaxo-model.gemspec CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["samuel.williams@oriontransfer.co.nz"]
11
11
  spec.description = <<-EOF
12
12
  Relaxo Model provides a framework for business logic on top of
13
- Relaxo/CouchDB. While it supports some traditional ORM style patterns, it is
13
+ Relaxo. While it supports some traditional ORM style patterns, it is
14
14
  primary focus is to model business processes and logic.
15
15
  EOF
16
16
  spec.summary = "A model layer for CouchDB with minimal global state."
@@ -0,0 +1,36 @@
1
+
2
+ require 'relaxo/model'
3
+
4
+ class Aggregate
5
+ include Relaxo::Model
6
+
7
+ property :id, UUID
8
+ property :array_value
9
+ property :hash_value
10
+ end
11
+
12
+ RSpec.describe Relaxo::Model::Document do
13
+ let(:database_path) {File.join(__dir__, 'test')}
14
+ let(:database) {Relaxo.connect(database_path)}
15
+
16
+ let(:document_path) {'test/document.json'}
17
+
18
+ before(:each) {FileUtils.rm_rf(database_path)}
19
+
20
+ it "should create and save document" do
21
+ model = Aggregate.create(database.current,
22
+ array_value: [1, 2, 3],
23
+ hash_value: {x: 10, y: 20}
24
+ )
25
+
26
+ database.commit(message: "Adding test model") do |dataset|
27
+ model.save(dataset)
28
+ end
29
+
30
+ # Force all attributes to be reloaded from the object store:
31
+ model.reload
32
+
33
+ expect(model.array_value).to be_kind_of Array
34
+ expect(model.hash_value).to be_kind_of Hash
35
+ end
36
+ end
@@ -33,6 +33,18 @@ class Invoice::Transaction
33
33
  view :by_invoice, [:type, 'by_invoice', :invoice], index: [[:date, :id]]
34
34
  end
35
35
 
36
+ class User
37
+ include Relaxo::Model
38
+
39
+ property :email, Attribute[String]
40
+ property :name
41
+ property :intro
42
+
43
+ view :all, [:type], index: [:email]
44
+
45
+ view :by_name, [:type, 'by_name'], index: [:name]
46
+ end
47
+
36
48
  RSpec.describe Relaxo::Model::Document do
37
49
  let(:database_path) {File.join(__dir__, 'test')}
38
50
  let(:database) {Relaxo.connect(database_path)}
@@ -96,9 +108,38 @@ RSpec.describe Relaxo::Model::Document do
96
108
  transaction.save(dataset)
97
109
  end
98
110
 
111
+ transactions = Invoice::Transaction.all(database.current)
112
+
99
113
  database.commit(message: "Adding test model") do |dataset|
100
114
  transaction.date = Date.today - 1
101
115
  transaction.save(dataset)
102
116
  end
117
+
118
+ transactions = Invoice::Transaction.all(database.current)
119
+ end
120
+
121
+ it "can query by index" do
122
+ database.commit(message: 'Adding new users.') do |changes|
123
+ User.insert(changes, email: 'john.doe@aol.com', name: 'John Doe')
124
+ User.insert(changes, email: 'jane.doe@aol.com', name: 'Jane Doe')
125
+ end
126
+
127
+ expect(User.fetch_all(database.current, email: 'john.doe@aol.com').name).to be == 'John Doe'
128
+ end
129
+
130
+ it "can handle indexes with reserved characters" do
131
+ database.commit(message: "Add new user") do |changeset|
132
+ User.insert(changeset, email: "its@complicated.com", name: "/John/James/")
133
+ end
134
+
135
+ expect(User.fetch_all(database.current, email: 'its@complicated.com').name).to be == '/John/James/'
136
+ end
137
+
138
+ it "can handle indexes with unicode" do
139
+ database.commit(message: "Add new user") do |changeset|
140
+ User.insert(changeset, email: "its@complicated.com", name: "こんにちは")
141
+ end
142
+
143
+ expect(User.fetch_by_name(database.current, name: "こんにちは")).to_not be nil
103
144
  end
104
145
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaxo-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-12 00:00:00.000000000 Z
11
+ date: 2017-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: relaxo
@@ -80,7 +80,7 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description: "\t\tRelaxo Model provides a framework for business logic on top of\n\t\tRelaxo/CouchDB.
83
+ description: "\t\tRelaxo Model provides a framework for business logic on top of\n\t\tRelaxo.
84
84
  While it supports some traditional ORM style patterns, it is\n\t\tprimary focus
85
85
  is to model business processes and logic.\n"
86
86
  email:
@@ -109,6 +109,7 @@ files:
109
109
  - lib/relaxo/model/recordset.rb
110
110
  - lib/relaxo/model/version.rb
111
111
  - relaxo-model.gemspec
112
+ - spec/relaxo/model/attribute_spec.rb
112
113
  - spec/relaxo/model/document_spec.rb
113
114
  homepage: http://www.codeotaku.com/projects/relaxo/model
114
115
  licenses:
@@ -130,9 +131,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  version: '0'
131
132
  requirements: []
132
133
  rubyforge_project:
133
- rubygems_version: 2.6.10
134
+ rubygems_version: 2.6.12
134
135
  signing_key:
135
136
  specification_version: 4
136
137
  summary: A model layer for CouchDB with minimal global state.
137
138
  test_files:
139
+ - spec/relaxo/model/attribute_spec.rb
138
140
  - spec/relaxo/model/document_spec.rb