scriba 0.0.1.alpha3 → 0.0.1.alpha4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/scriba/assertions.rb +24 -21
- data/lib/scriba/messages.rb +9 -7
- data/lib/scriba/partial_update.rb +12 -4
- data/lib/scriba.rb +1 -12
- data/scriba.gemspec +1 -1
- data/test/partial.rb +1 -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: c5518ab48176f05d83deaece3e6a44c3bd77ed7b
|
4
|
+
data.tar.gz: efa6a5f8a22501d88c515be06d293d8d68653730
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2df4486faea12e1e5f630f62cbc50c274aa1dc36333ea3e42dcb2e956305c983c082bd451e5cbe07c215703a929e1cc4077bfa7227c9a0e07d381581baf2ae6a
|
7
|
+
data.tar.gz: 9c554ddfd541931bd8fbf836265378ddee44c61460de96d1e5c8f1f8ef255d53901678465d0407886f9ba4c21c9b82de26ba1b87dad867d5ddb05a551270b263
|
data/lib/scriba/assertions.rb
CHANGED
@@ -25,51 +25,54 @@ module Scriba::Assertions
|
|
25
25
|
((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+
|
26
26
|
[a-z]{2,12})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)\z/ix
|
27
27
|
|
28
|
-
def assert_email(att,
|
29
|
-
assert_format(att, EMAIL,
|
28
|
+
def assert_email(att, error = :not_email)
|
29
|
+
assert_format(att, EMAIL, error)
|
30
30
|
end
|
31
31
|
|
32
|
-
def assert_equal(att, value,
|
33
|
-
assert(value ===
|
32
|
+
def assert_equal(att, value, error = :not_equal)
|
33
|
+
assert(att, error) { |v| value === v }
|
34
34
|
end
|
35
35
|
|
36
|
-
def assert_format(att, format,
|
37
|
-
assert(format ===
|
36
|
+
def assert_format(att, format, error = :not_valid)
|
37
|
+
assert(att, error) { |v| format === v.to_s }
|
38
38
|
end
|
39
39
|
|
40
|
-
def assert_length(att,
|
41
|
-
assert(
|
40
|
+
def assert_length(att, value, error = :wrong_length)
|
41
|
+
assert(att, error, value: value) { |v| v.to_s.length == value }
|
42
42
|
end
|
43
43
|
|
44
|
-
def assert_maximum_length(att,
|
45
|
-
assert(
|
44
|
+
def assert_maximum_length(att, value, error = :too_long)
|
45
|
+
assert(att, error, value: value) { |v| v.to_s.length <= value }
|
46
46
|
end
|
47
47
|
|
48
|
-
def assert_minimum_length(att,
|
49
|
-
assert(
|
48
|
+
def assert_minimum_length(att, value, error = :too_short)
|
49
|
+
assert(att, error, value: value) { |v| v.to_s.length >= value }
|
50
50
|
end
|
51
51
|
|
52
52
|
BLANK = /\A[[:space:]]*\z/
|
53
53
|
|
54
|
-
def assert_present(att,
|
55
|
-
assert(!(BLANK ===
|
54
|
+
def assert_present(att, error = :not_present)
|
55
|
+
assert(att, error) { |v| !(BLANK === v.to_s) }
|
56
56
|
end
|
57
57
|
|
58
58
|
URL = /\A(http|https):\/\/([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,12}|(2
|
59
59
|
5[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}
|
60
60
|
|localhost)(:[0-9]{1,5})?(\/.*)?\z/ix
|
61
61
|
|
62
|
-
def assert_url(att,
|
63
|
-
assert_format(att, URL,
|
62
|
+
def assert_url(att, error = :not_url)
|
63
|
+
assert_format(att, URL, error)
|
64
64
|
end
|
65
65
|
|
66
66
|
private
|
67
67
|
|
68
|
-
def assert(
|
69
|
-
value
|
70
|
-
end
|
68
|
+
def assert(att, error, options = {})
|
69
|
+
value = yield(send(att))
|
71
70
|
|
72
|
-
|
73
|
-
|
71
|
+
if value
|
72
|
+
return true
|
73
|
+
else
|
74
|
+
errors[att] << generate_message(error, options)
|
75
|
+
return false
|
76
|
+
end
|
74
77
|
end
|
75
78
|
end
|
data/lib/scriba/messages.rb
CHANGED
@@ -6,20 +6,22 @@ module Scriba::Messages
|
|
6
6
|
not_unique: "is already taken",
|
7
7
|
not_url: "is an invalid URL",
|
8
8
|
not_valid: "is invalid",
|
9
|
-
too_long: "must be maximum %{
|
10
|
-
too_short: "must be minimum %{
|
11
|
-
wrong_length: "must be %{
|
9
|
+
too_long: "must be maximum %{value} characters",
|
10
|
+
too_short: "must be minimum %{value} characters",
|
11
|
+
wrong_length: "must be %{value} characters"
|
12
12
|
}
|
13
13
|
|
14
14
|
private
|
15
15
|
|
16
|
-
def generate_message(options)
|
17
|
-
message = options.delete(:message) || :not_valid
|
18
|
-
|
16
|
+
def generate_message(message, options = {})
|
19
17
|
if message.is_a?(Symbol)
|
20
|
-
return
|
18
|
+
return error_message(message, options)
|
21
19
|
else
|
22
20
|
return message
|
23
21
|
end
|
24
22
|
end
|
23
|
+
|
24
|
+
def error_message(message, options)
|
25
|
+
return sprintf(DEFAULTS[message], options)
|
26
|
+
end
|
25
27
|
end
|
@@ -1,9 +1,17 @@
|
|
1
1
|
module Scriba::PartialUpdate
|
2
|
-
def
|
3
|
-
@
|
2
|
+
def initialize(updates = {})
|
3
|
+
super(@updates = updates)
|
4
4
|
end
|
5
5
|
|
6
|
-
def assert(
|
7
|
-
super(
|
6
|
+
def assert(att, error, options = {})
|
7
|
+
return __partials.include?(att) ? super(att, error, options) : false
|
8
|
+
end
|
9
|
+
|
10
|
+
def attributes
|
11
|
+
return slice(*__partials)
|
12
|
+
end
|
13
|
+
|
14
|
+
def __partials
|
15
|
+
@_partials ||= self.class.attributes & @updates.keys.map(&:to_sym)
|
8
16
|
end
|
9
17
|
end
|
data/lib/scriba.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
class Scriba
|
2
2
|
require_relative "scriba/assertions"
|
3
3
|
require_relative "scriba/messages"
|
4
|
-
require_relative "scriba/partial_update"
|
5
4
|
|
6
5
|
include Scriba::Assertions
|
7
6
|
include Scriba::Messages
|
@@ -19,13 +18,7 @@ class Scriba
|
|
19
18
|
@attributes ||= []
|
20
19
|
end
|
21
20
|
|
22
|
-
attr :updates
|
23
|
-
|
24
21
|
def initialize(updates = {})
|
25
|
-
update(@updates = updates)
|
26
|
-
end
|
27
|
-
|
28
|
-
def update(updates)
|
29
22
|
updates.each do |att, value|
|
30
23
|
accessor = "#{att}="
|
31
24
|
send(accessor, value) if respond_to?(accessor)
|
@@ -33,11 +26,7 @@ class Scriba
|
|
33
26
|
end
|
34
27
|
|
35
28
|
def attributes
|
36
|
-
return slice(*
|
37
|
-
end
|
38
|
-
|
39
|
-
def __attributes
|
40
|
-
return self.class.attributes
|
29
|
+
return slice(*self.class.attributes)
|
41
30
|
end
|
42
31
|
|
43
32
|
def slice(*atts)
|
data/scriba.gemspec
CHANGED
data/test/partial.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scriba
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.
|
4
|
+
version: 0.0.1.alpha4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Francesco Rodríguez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cutest
|