rom-model 0.1.1 → 0.2.0.beta1
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.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/Gemfile +3 -0
- data/README.md +1 -1
- data/lib/rom/model/validator.rb +29 -2
- data/lib/rom/model/version.rb +1 -1
- data/rom-model.gemspec +2 -1
- data/spec/integration/chainable_validator_spec.rb +53 -0
- data/spec/shared/database.rb +7 -4
- data/spec/spec_helper.rb +0 -2
- data/spec/unit/validator_spec.rb +2 -2
- metadata +23 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8e29081a0af75745a0bc941c1e04de03a39521d
|
4
|
+
data.tar.gz: a97ee12e2918f9d59aa9d423c2046774c60c9e4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c90b1614112a4b2649d5e3366b9f8e9f5908913bc12b3976aceb59417ed6368eb15fa00b1554b66262d207d82ae7fa813176dcda9a3c1c6e7023e55b335cbe90
|
7
|
+
data.tar.gz: 79cadeb47cc841d120042c3ec1fe6595439d1b5b4ec856ec279ec88f00c1e5ad672a9006eb1cb1ac3e2f13c493e896620fcdbcf4acbadb2a5e0f8eb46c3aeee8
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
@@ -4,6 +4,9 @@ gemspec
|
|
4
4
|
|
5
5
|
gem 'rom', github: 'rom-rb/rom', branch: 'master'
|
6
6
|
gem 'rom-sql', github: 'rom-rb/rom-sql', branch: 'master'
|
7
|
+
gem 'rom-support', github: 'rom-rb/rom-support', branch: 'master'
|
8
|
+
gem 'rom-mapper', github: 'rom-rb/rom-mapper', branch: 'master'
|
9
|
+
gem 'transproc', github: 'solnic/transproc', branch: 'master'
|
7
10
|
gem 'pg', platforms: [:mri, :rbx]
|
8
11
|
gem 'pg_jruby', platforms: :jruby
|
9
12
|
|
data/README.md
CHANGED
@@ -25,7 +25,7 @@ The package includes:
|
|
25
25
|
## The Plan™
|
26
26
|
|
27
27
|
This gem is built on top of existing 3rd party gems that have proven to be stable
|
28
|
-
and good-enough.
|
28
|
+
and good-enough. Unfortunately neither Virtus nor ActiveModel do not meet certain
|
29
29
|
design requirements to be a good fit in the long term.
|
30
30
|
|
31
31
|
For that reason we're exploring how to build a better foundation for rom-model.
|
data/lib/rom/model/validator.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'dry-equalizer'
|
1
2
|
require 'charlatan'
|
2
3
|
|
3
4
|
require 'rom/support/constants'
|
@@ -6,11 +7,21 @@ require 'rom/constants'
|
|
6
7
|
require 'rom/model/validator/uniqueness_validator'
|
7
8
|
require 'rom/support/class_macros'
|
8
9
|
|
10
|
+
require 'rom/pipeline'
|
11
|
+
|
9
12
|
module ROM
|
10
13
|
module Model
|
11
14
|
class ValidationError < CommandError
|
12
15
|
include Charlatan.new(:errors)
|
13
|
-
include Equalizer
|
16
|
+
include Dry::Equalizer(:errors)
|
17
|
+
end
|
18
|
+
|
19
|
+
class Composite < Pipeline::Composite
|
20
|
+
def call(attributes)
|
21
|
+
left.call(attributes)
|
22
|
+
|
23
|
+
right.call(attributes)
|
24
|
+
end
|
14
25
|
end
|
15
26
|
|
16
27
|
# Mixin for ROM-compliant validator objects
|
@@ -44,7 +55,7 @@ module ROM
|
|
44
55
|
extend ROM::ClassMacros
|
45
56
|
|
46
57
|
include ActiveModel::Validations
|
47
|
-
include Equalizer
|
58
|
+
include Dry::Equalizer(:attributes, :errors)
|
48
59
|
|
49
60
|
base.defines :embedded_validators
|
50
61
|
|
@@ -99,6 +110,7 @@ module ROM
|
|
99
110
|
attributes
|
100
111
|
end
|
101
112
|
|
113
|
+
|
102
114
|
private
|
103
115
|
|
104
116
|
# This is needed for ActiveModel::Validations to work properly
|
@@ -157,6 +169,21 @@ module ROM
|
|
157
169
|
validator.call
|
158
170
|
end
|
159
171
|
|
172
|
+
# Compose a validation with a command
|
173
|
+
#
|
174
|
+
# The command will be called if validations succeed
|
175
|
+
#
|
176
|
+
# @example
|
177
|
+
# validated_command = (UserValidator >> users.create)
|
178
|
+
# validated_command.call(attributes)
|
179
|
+
#
|
180
|
+
# @return [Composite]
|
181
|
+
#
|
182
|
+
# @api public
|
183
|
+
def >>(other)
|
184
|
+
Composite.new(self, other)
|
185
|
+
end
|
186
|
+
|
160
187
|
# Specify an embedded validator for nested structures
|
161
188
|
#
|
162
189
|
# @example
|
data/lib/rom/model/version.rb
CHANGED
data/rom-model.gemspec
CHANGED
@@ -17,7 +17,8 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_runtime_dependency '
|
20
|
+
spec.add_runtime_dependency 'dry-equalizer', '~> 0.2'
|
21
|
+
spec.add_runtime_dependency 'rom-support', '~> 1.0.0.beta1'
|
21
22
|
spec.add_runtime_dependency 'charlatan', '~> 0.1'
|
22
23
|
spec.add_runtime_dependency 'virtus', '~> 1.0', '>= 1.0.5'
|
23
24
|
spec.add_runtime_dependency 'activemodel', '>= 3.0', '< 5.0'
|
@@ -0,0 +1,53 @@
|
|
1
|
+
describe 'Chaning validations into a commad' do
|
2
|
+
include_context 'database'
|
3
|
+
|
4
|
+
subject(:validator) { user_validator.new(attributes) }
|
5
|
+
|
6
|
+
let(:user_attrs) do
|
7
|
+
Class.new {
|
8
|
+
include ROM::Model::Attributes
|
9
|
+
|
10
|
+
set_model_name 'User'
|
11
|
+
|
12
|
+
attribute :name, String
|
13
|
+
attribute :email, String
|
14
|
+
attribute :birthday, Date
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:user_validator) do
|
19
|
+
Class.new {
|
20
|
+
include ROM::Model::Validator
|
21
|
+
|
22
|
+
relation :users
|
23
|
+
|
24
|
+
validates :name, presence: true, uniqueness: { message: 'TAKEN!' }
|
25
|
+
validates :email, uniqueness: true
|
26
|
+
|
27
|
+
def self.name
|
28
|
+
'User'
|
29
|
+
end
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'works' do
|
34
|
+
expect { rom.command(:users).create.call(name: 'John', email: 'john@doe.org') }
|
35
|
+
.to change(rom.relation(:users), :count).by 1
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'runs validations' do
|
39
|
+
validated_command = (user_validator >> rom.command(:users).create)
|
40
|
+
expect {
|
41
|
+
validated_command.call({})
|
42
|
+
}.to raise_error(ROM::Model::ValidationError)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'calls the nested command when validations pass' do
|
46
|
+
validated_command = (user_validator >> rom.command(:users).create)
|
47
|
+
attributes = user_attrs.new(name: 'John', email: 'john@doe.org')
|
48
|
+
expect {
|
49
|
+
validated_command.call(attributes)
|
50
|
+
}.to change(rom.relation(:users), :count).by 1
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
data/spec/shared/database.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
shared_context 'database' do
|
2
|
-
let(:rom) { ROM.env }
|
2
|
+
let(:rom) { ROM.env = ROM.container(configuration) }
|
3
|
+
let(:configuration) { ROM::Configuration.new(:sql, DB_URI).use(:macros) }
|
3
4
|
let(:uri) { DB_URI }
|
4
5
|
let(:conn) { Sequel.connect(uri) }
|
5
6
|
|
@@ -10,8 +11,6 @@ shared_context 'database' do
|
|
10
11
|
end
|
11
12
|
|
12
13
|
before do
|
13
|
-
setup = ROM.setup(:sql, conn)
|
14
|
-
|
15
14
|
drop_tables
|
16
15
|
|
17
16
|
conn.create_table :users do
|
@@ -23,7 +22,11 @@ shared_context 'database' do
|
|
23
22
|
check { char_length(name) > 2 }
|
24
23
|
end
|
25
24
|
|
26
|
-
|
25
|
+
configuration.relation(:users)
|
26
|
+
|
27
|
+
configuration.commands(:users) do
|
28
|
+
define(:create)
|
29
|
+
end
|
27
30
|
end
|
28
31
|
|
29
32
|
after do
|
data/spec/spec_helper.rb
CHANGED
data/spec/unit/validator_spec.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
describe 'Validation' do
|
2
2
|
include_context 'database'
|
3
3
|
|
4
|
+
before { rom }
|
5
|
+
|
4
6
|
subject(:validator) { user_validator.new(attributes) }
|
5
7
|
|
6
8
|
let(:user_attrs) do
|
@@ -30,8 +32,6 @@ describe 'Validation' do
|
|
30
32
|
}
|
31
33
|
end
|
32
34
|
|
33
|
-
before { ROM.finalize }
|
34
|
-
|
35
35
|
describe '#call' do
|
36
36
|
let(:attributes) { user_attrs.new }
|
37
37
|
|
metadata
CHANGED
@@ -1,29 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0.beta1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Solnica
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-equalizer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.2'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rom-support
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
31
|
- - "~>"
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
33
|
+
version: 1.0.0.beta1
|
20
34
|
type: :runtime
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
38
|
- - "~>"
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
40
|
+
version: 1.0.0.beta1
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: charlatan
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -159,6 +173,7 @@ files:
|
|
159
173
|
- rakelib/mutant.rake
|
160
174
|
- rakelib/rubocop.rake
|
161
175
|
- rom-model.gemspec
|
176
|
+
- spec/integration/chainable_validator_spec.rb
|
162
177
|
- spec/shared/database.rb
|
163
178
|
- spec/spec_helper.rb
|
164
179
|
- spec/unit/attributes_spec.rb
|
@@ -179,9 +194,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
179
194
|
version: '0'
|
180
195
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
181
196
|
requirements:
|
182
|
-
- - "
|
197
|
+
- - ">"
|
183
198
|
- !ruby/object:Gem::Version
|
184
|
-
version:
|
199
|
+
version: 1.3.1
|
185
200
|
requirements: []
|
186
201
|
rubyforge_project:
|
187
202
|
rubygems_version: 2.4.5
|
@@ -189,8 +204,10 @@ signing_key:
|
|
189
204
|
specification_version: 4
|
190
205
|
summary: A small collection of extensions useful for data coercion and validation
|
191
206
|
test_files:
|
207
|
+
- spec/integration/chainable_validator_spec.rb
|
192
208
|
- spec/shared/database.rb
|
193
209
|
- spec/spec_helper.rb
|
194
210
|
- spec/unit/attributes_spec.rb
|
195
211
|
- spec/unit/validator/embedded_spec.rb
|
196
212
|
- spec/unit/validator_spec.rb
|
213
|
+
has_rdoc:
|