rom-model 0.1.0 → 0.1.1
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/CHANGELOG.md +14 -0
- data/lib/rom/model/validator.rb +23 -6
- data/lib/rom/model/version.rb +1 -1
- data/spec/unit/validator/embedded_spec.rb +90 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6603e93e6f5f8d81a8ea8e11b90eab50a1838341
|
4
|
+
data.tar.gz: dbf3b3ca0ef55c9414f7bd495e08858c5ba67f9d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57127a346d805caec84570a3ce603d4a049b22412a74be6ef8fa12b5707a5ea4282ac1b9efa3d73c5142160772eade72cd6f322abf716e75c200e08ab1048da0
|
7
|
+
data.tar.gz: e4ff1ee087a43c703b1f6c39b35b898df2ef99c3d27f1624b0f2806b0456c8d83c9f6e818eb7a8e4983e1ba7fbddfdcec12843f0cdd5083ef95e2f52dfe9856b
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,17 @@
|
|
1
|
+
# 0.1.1 2015-08-19
|
2
|
+
|
3
|
+
## Added
|
4
|
+
|
5
|
+
- Embedded validator has access to `root` node of the input attributes (solnic)
|
6
|
+
- Embedded validator has access to `parent` node of the input attributes (solnic)
|
7
|
+
- Presence validation for embedded validation can be skipped with `presence: false` option (solnic)
|
8
|
+
|
9
|
+
## Fixed
|
10
|
+
|
11
|
+
- `set_model_name` is correctly set for embedded validators (solnic)
|
12
|
+
|
13
|
+
[Compare v0.1.0...v0.1.1](https://github.com/rom-rb/rom-model/compare/v0.1.0...v0.1.1)
|
14
|
+
|
1
15
|
# 0.1.0 2015-08-14
|
2
16
|
|
3
17
|
Extracted code from rom-rails 0.5.0
|
data/lib/rom/model/validator.rb
CHANGED
@@ -60,11 +60,23 @@ module ROM
|
|
60
60
|
# @api private
|
61
61
|
attr_reader :attr_names
|
62
62
|
|
63
|
+
# @attr_reader [Object] root The root node in attributes hash of an embedded validator
|
64
|
+
#
|
65
|
+
# @api public
|
66
|
+
attr_reader :root
|
67
|
+
|
68
|
+
# @attr_reader [Object] root The parent node in attributes hash of an embedded validator
|
69
|
+
#
|
70
|
+
# @api public
|
71
|
+
attr_reader :parent
|
72
|
+
|
63
73
|
delegate :model_name, to: :attributes
|
64
74
|
|
65
75
|
# @api private
|
66
|
-
def initialize(attributes)
|
76
|
+
def initialize(attributes, root = attributes, parent = nil)
|
67
77
|
@attributes = attributes
|
78
|
+
@root = root
|
79
|
+
@parent = parent
|
68
80
|
@attr_names = self.class.validators.map(&:attributes).flatten.uniq
|
69
81
|
end
|
70
82
|
|
@@ -169,21 +181,26 @@ module ROM
|
|
169
181
|
# validator.errors[:tasks] # errors for tasks
|
170
182
|
#
|
171
183
|
# @api public
|
172
|
-
def embedded(name, &block)
|
173
|
-
|
174
|
-
|
184
|
+
def embedded(name, options = {}, &block)
|
185
|
+
presence = options.fetch(:presence, true)
|
186
|
+
|
187
|
+
validator_class = Class.new {
|
188
|
+
include ROM::Model::Validator
|
189
|
+
}
|
190
|
+
|
175
191
|
validator_class.set_model_name(name.to_s.classify)
|
192
|
+
validator_class.class_eval(&block)
|
176
193
|
|
177
194
|
embedded_validators[name] = validator_class
|
178
195
|
|
179
|
-
validates name, presence: true
|
196
|
+
validates name, presence: true if presence
|
180
197
|
|
181
198
|
validate do
|
182
199
|
value = attributes[name]
|
183
200
|
|
184
201
|
if value.present?
|
185
202
|
Array([value]).flatten.each do |object|
|
186
|
-
validator = validator_class.new(object)
|
203
|
+
validator = validator_class.new(object, root, attributes)
|
187
204
|
validator.validate
|
188
205
|
|
189
206
|
if validator.errors.any?
|
data/lib/rom/model/version.rb
CHANGED
@@ -113,4 +113,94 @@ describe 'Embedded validators' do
|
|
113
113
|
|
114
114
|
expect(user_validator.embedded_validators[:tasks]).to be_present
|
115
115
|
end
|
116
|
+
|
117
|
+
it 'adds access to the root node in attribute hash' do
|
118
|
+
user_validator = Class.new do
|
119
|
+
include ROM::Model::Validator
|
120
|
+
|
121
|
+
set_model_name 'User'
|
122
|
+
|
123
|
+
validates :name, presence: true
|
124
|
+
|
125
|
+
embedded :tasks do
|
126
|
+
set_model_name 'Task'
|
127
|
+
|
128
|
+
validate do
|
129
|
+
if attributes[:title] != "#{root[:name]} Task"
|
130
|
+
errors.add(:base, 'does not look correct')
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
attributes = { name: 'Jade', tasks: [{ title: 'Jane Task' }] }
|
137
|
+
|
138
|
+
validator = user_validator.new(attributes)
|
139
|
+
|
140
|
+
validator.validate
|
141
|
+
|
142
|
+
expect(validator.errors[:tasks][0][:base]).to include('does not look correct')
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'adds access to the parent node in attribute hash' do
|
146
|
+
user_validator = Class.new do
|
147
|
+
include ROM::Model::Validator
|
148
|
+
|
149
|
+
set_model_name 'User'
|
150
|
+
|
151
|
+
validates :name, presence: true
|
152
|
+
|
153
|
+
embedded :tasks do
|
154
|
+
set_model_name 'Task'
|
155
|
+
|
156
|
+
validate do
|
157
|
+
if attributes[:title] != "#{parent[:name]} Task"
|
158
|
+
errors.add(:base, 'does not look correct')
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
attributes = { name: 'Jade', tasks: [{ title: 'Jane Task' }] }
|
165
|
+
|
166
|
+
validator = user_validator.new(attributes)
|
167
|
+
|
168
|
+
validator.validate
|
169
|
+
|
170
|
+
expect(validator.errors[:tasks][0][:base]).to include('does not look correct')
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'allows skipping presence check' do
|
174
|
+
user_validator = Class.new do
|
175
|
+
include ROM::Model::Validator
|
176
|
+
|
177
|
+
set_model_name 'User'
|
178
|
+
|
179
|
+
validates :name, presence: true
|
180
|
+
|
181
|
+
embedded :tasks, presence: false do
|
182
|
+
set_model_name 'Task'
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
attributes = { name: 'Jade' }
|
187
|
+
|
188
|
+
validator = user_validator.new(attributes)
|
189
|
+
|
190
|
+
expect(validator).to be_valid
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'sets model name' do
|
194
|
+
user_validator = Class.new do
|
195
|
+
include ROM::Model::Validator
|
196
|
+
|
197
|
+
set_model_name 'User'
|
198
|
+
|
199
|
+
embedded :tasks, presence: false do
|
200
|
+
set_model_name 'Task'
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
expect(user_validator.embedded_validators[:tasks].model_name.name).to eql('Task')
|
205
|
+
end
|
116
206
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rom-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
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-08-
|
11
|
+
date: 2015-08-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rom-support
|