otoroshi 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/otoroshi/sanctuary.rb +11 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 125c31a56b83045874e5c91780c2541b93c73a567914917c2b8d54c38650c32d
|
4
|
+
data.tar.gz: 45869708d77341f0a45cf3fed38f7719cdd50dac6e13cb7dc528fe36ac2b3f10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 435e9e8988c073ac9c3d8a661c0bdc0030e17118ffb39ca15674b86f6a2ccf9a20d0308a4a126a9c74b0b54aaaee27110e9d6a66d86b99b3f6727f3068484563
|
7
|
+
data.tar.gz: d32b924aa3c1446481a2ada3390783881ef1b66a52db41f4e4beb7e72a596e5a854606276443e6f59bad5527fd69c9c3759cb94a84e2d88e1eee9ab1bb6910ac
|
data/lib/otoroshi/sanctuary.rb
CHANGED
@@ -5,11 +5,11 @@ module Otoroshi
|
|
5
5
|
class Sanctuary
|
6
6
|
class << self
|
7
7
|
# Add a new property to the class (called by inherited class)
|
8
|
-
# Example: property name, type: String, validate: ->(v) { v.length > 3 },
|
9
|
-
def property(name, type = Object, validate: ->(_) { true },
|
8
|
+
# Example: property name, type: String, validate: ->(v) { v.length > 3 }, allow_nil: true
|
9
|
+
def property(name, type = Object, validate: ->(_) { true }, allow_nil: false, default: nil)
|
10
10
|
define_default(name, default)
|
11
|
-
define_validate_type!(name, type,
|
12
|
-
define_validate!(name, validate,
|
11
|
+
define_validate_type!(name, type, allow_nil)
|
12
|
+
define_validate!(name, validate, allow_nil)
|
13
13
|
define_getter(name)
|
14
14
|
define_setter(name)
|
15
15
|
add_to_properties(name)
|
@@ -49,19 +49,19 @@ module Otoroshi
|
|
49
49
|
# ::define_validate_type!("score", Integer, false)
|
50
50
|
# ----------------------------------------------
|
51
51
|
# def validate_score_type!(value)
|
52
|
-
# return if
|
52
|
+
# return if allow_nil && value.nil?
|
53
53
|
# return if value.is_a?(Integer)
|
54
54
|
#
|
55
|
-
# raise ArgumentError, ":score does not match type
|
55
|
+
# raise ArgumentError, ":score does not match required type"
|
56
56
|
# end
|
57
57
|
#
|
58
|
-
def define_validate_type!(name, type,
|
58
|
+
def define_validate_type!(name, type, allow_nil)
|
59
59
|
lambda = type_validation(type)
|
60
60
|
define_method :"validate_#{name}_type!" do |value|
|
61
|
-
return if type.nil? ||
|
61
|
+
return if type.nil? || allow_nil && value.nil?
|
62
62
|
return if lambda.call(value)
|
63
63
|
|
64
|
-
raise ArgumentError, ":#{name} does not match type
|
64
|
+
raise ArgumentError, ":#{name} does not match required type"
|
65
65
|
end
|
66
66
|
private :"validate_#{name}_type!"
|
67
67
|
end
|
@@ -91,9 +91,9 @@ module Otoroshi
|
|
91
91
|
# raise ArgumentError, ":score does not match validation"
|
92
92
|
# end
|
93
93
|
#
|
94
|
-
def define_validate!(name, validate,
|
94
|
+
def define_validate!(name, validate, allow_nil)
|
95
95
|
define_method :"validate_#{name}!" do |value|
|
96
|
-
return if
|
96
|
+
return if allow_nil && value.nil?
|
97
97
|
return if instance_exec(value, &validate)
|
98
98
|
|
99
99
|
raise ArgumentError, ":#{name} does not match validation"
|