yas 0.2.3 → 0.2.4
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/README.markdown +13 -2
- data/lib/yas/ext/symbolize.rb +2 -1
- data/lib/yas/version.rb +1 -1
- data/test/ext/test_attribute.rb +7 -0
- data/test/ext/test_symbolize.rb +18 -2
- 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: 8e8d3f4d4fa15965f7f65e3175a6ba8e395f0010
|
4
|
+
data.tar.gz: 4e29b1b399957476619629514759ab78496505d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 946ccc4ee9e06eee567e090a568a5d80c3f0720209697d961a389bf18a9995a292159c59b5ff3c534e7726e52508df35477447d7b2aa51331b949c37622dd3f3
|
7
|
+
data.tar.gz: 67a80c808a9c388b21b9e9a9e2fd26a9b865863da67c5a419b6bbd25c4fab6c56b5df332a212b8962c02ddcba75b33347caf9178c966fdc5e2912342e00cf163
|
data/README.markdown
CHANGED
@@ -7,9 +7,10 @@ Using YAS, you can enforce a specific key to be required, rename them, perform a
|
|
7
7
|
## Installation
|
8
8
|
|
9
9
|
gem install yas
|
10
|
+
require 'yas'
|
10
11
|
|
11
12
|
|
12
|
-
## Quick
|
13
|
+
## Quick Example
|
13
14
|
|
14
15
|
require 'yas'
|
15
16
|
|
@@ -37,6 +38,7 @@ Optionally, you may also use `YAS::SchemaBase` if you want to start off from a c
|
|
37
38
|
|
38
39
|
Declares an attribute/key with various requirements.
|
39
40
|
The Attribute extension allows you to specify certain requirements/restrictions for a given key.
|
41
|
+
`attribute` and `key` are aliases, you can use either one.
|
40
42
|
|
41
43
|
attribute name, &block
|
42
44
|
key name, &block
|
@@ -58,6 +60,15 @@ Example:
|
|
58
60
|
hash.merge!(:email => 'john@email.com')
|
59
61
|
hash.validate! MySchema # Success!
|
60
62
|
|
63
|
+
You can also specify multiple keys per `key`/`attribute` block.
|
64
|
+
|
65
|
+
class MySchema < YAS::Schema
|
66
|
+
attribute :email, :first_name do
|
67
|
+
required
|
68
|
+
type String
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
61
72
|
List of directives you can use:
|
62
73
|
|
63
74
|
* `required`
|
@@ -143,7 +154,7 @@ Example:
|
|
143
154
|
#### Symbolize
|
144
155
|
|
145
156
|
Symbolize keys in your hash.
|
146
|
-
This does not perform deep symbolize. See
|
157
|
+
This does not perform deep symbolize. See `type` in the Attribute section for if you want deep symbolize.
|
147
158
|
|
148
159
|
symbolize true|false
|
149
160
|
|
data/lib/yas/ext/symbolize.rb
CHANGED
@@ -5,7 +5,8 @@ class YAS::SymbolizeExt
|
|
5
5
|
|
6
6
|
module ClassMethods
|
7
7
|
def symbolize sym = nil
|
8
|
-
(!sym.nil? && (sym.class == TrueClass || sym.class == FalseClass))
|
8
|
+
@symbolize = sym if (!sym.nil? && (sym.class == TrueClass || sym.class == FalseClass))
|
9
|
+
@symbolize = false unless defined? @symbolize
|
9
10
|
@symbolize
|
10
11
|
end
|
11
12
|
end
|
data/lib/yas/version.rb
CHANGED
data/test/ext/test_attribute.rb
CHANGED
@@ -231,6 +231,13 @@ class TestAttributeExt < Minitest::Test
|
|
231
231
|
hash.validate! MultipleAttributesSchema
|
232
232
|
assert_equal "when", hash[:personal_info]
|
233
233
|
assert_equal "who", hash["other_info"]
|
234
|
+
|
235
|
+
hash = {
|
236
|
+
personal_info: "when",
|
237
|
+
}
|
238
|
+
assert_raises YAS::ValidationError do
|
239
|
+
hash.validate! MultipleAttributesSchema
|
240
|
+
end
|
234
241
|
end
|
235
242
|
|
236
243
|
end
|
data/test/ext/test_symbolize.rb
CHANGED
@@ -5,13 +5,20 @@ class SymbolizedSchema < YAS::Schema
|
|
5
5
|
symbolize true
|
6
6
|
end
|
7
7
|
|
8
|
+
class NotSymbolizedSchema < YAS::Schema
|
9
|
+
attribute :first_name do
|
10
|
+
required
|
11
|
+
type String
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
8
15
|
|
9
16
|
class TestSymbolizeExt < Minitest::Test
|
10
17
|
|
11
18
|
def test_symbolize
|
12
19
|
hash = {
|
13
|
-
'first_name'
|
14
|
-
'last_name'
|
20
|
+
'first_name' => "john",
|
21
|
+
'last_name' => "doe",
|
15
22
|
}
|
16
23
|
hash.validate! SymbolizedSchema
|
17
24
|
assert_equal "john", hash[:first_name]
|
@@ -20,4 +27,13 @@ class TestSymbolizeExt < Minitest::Test
|
|
20
27
|
assert_nil hash['last_name']
|
21
28
|
end
|
22
29
|
|
30
|
+
def test_not_symbolize
|
31
|
+
hash = {
|
32
|
+
'first_name' => "john",
|
33
|
+
}
|
34
|
+
assert_raises YAS::ValidationError do
|
35
|
+
hash.validate! NotSymbolizedSchema
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
23
39
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Albert Tedja
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|