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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 66228de723806bb775ac7fbca0cddf365587d0c4
4
- data.tar.gz: 9259d1cf49d3fef29edc51161318d4c8bc2e8d28
3
+ metadata.gz: c5518ab48176f05d83deaece3e6a44c3bd77ed7b
4
+ data.tar.gz: efa6a5f8a22501d88c515be06d293d8d68653730
5
5
  SHA512:
6
- metadata.gz: 7135a3ab4453b1132e7f9dd3e4cd29eb6dfd8df9fc57f9698cc13cc84cc06ea7f4b5a528b7ac75cfd9dfecf82d597e0b89091196e44662537ac9959a1477778c
7
- data.tar.gz: 79e7a80d8b594c2eeb090e469f53c7e4f5e5bfccd741562056bd2dfcb17e658c0e548b0c1ba820533acc7cb23049bc89c1435544e438ad2431d20f9a0c7c3f0f
6
+ metadata.gz: 2df4486faea12e1e5f630f62cbc50c274aa1dc36333ea3e42dcb2e956305c983c082bd451e5cbe07c215703a929e1cc4077bfa7227c9a0e07d381581baf2ae6a
7
+ data.tar.gz: 9c554ddfd541931bd8fbf836265378ddee44c61460de96d1e5c8f1f8ef255d53901678465d0407886f9ba4c21c9b82de26ba1b87dad867d5ddb05a551270b263
@@ -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, message = :not_email)
29
- assert_format(att, EMAIL, message: message)
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, message = :not_equal)
33
- assert(value === send(att), att, message: message)
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, message = :not_valid)
37
- assert(format === send(att).to_s, att, message: message)
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, length, message = :wrong_length)
41
- assert(send(att).to_s.length == length, att, message: message, length: length)
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, length, message = :too_long)
45
- assert(send(att).to_s.length <= length, att, message: message, length: length)
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, length, message = :too_short)
49
- assert(send(att).to_s.length >= length, att, message: message, length: length)
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, message = :not_present)
55
- assert(!(BLANK === send(att).to_s), att, message: message)
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, message = :not_url)
63
- assert_format(att, URL, message: message)
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(value, att, options = {})
69
- value or add_error(att, options) && false
70
- end
68
+ def assert(att, error, options = {})
69
+ value = yield(send(att))
71
70
 
72
- def add_error(att, options)
73
- errors[att] << generate_message(options)
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
@@ -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 %{length} characters",
10
- too_short: "must be minimum %{length} characters",
11
- wrong_length: "must be %{length} characters"
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 sprintf(DEFAULTS[message], options)
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 __attributes
3
- @_attributes ||= super & updates.keys.map(&:to_sym)
2
+ def initialize(updates = {})
3
+ super(@updates = updates)
4
4
  end
5
5
 
6
- def assert(value, att, options = {})
7
- super(value, att, options) if __attributes.include?(att)
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(*__attributes)
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "scriba"
3
- s.version = "0.0.1.alpha3"
3
+ s.version = "0.0.1.alpha4"
4
4
  s.summary = "Validation frontend for models."
5
5
  s.description = s.summary + " Inspired by the scrivener gem."
6
6
  s.authors = ["Francesco Rodríguez"]
data/test/partial.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require_relative "helper"
2
+ require_relative "../lib/scriba/partial_update"
2
3
 
3
4
  class Signup < Scriba
4
5
  include Scriba::PartialUpdate
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.alpha3
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-05-25 00:00:00.000000000 Z
11
+ date: 2015-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest