valideizer 1.1.8 → 1.1.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 392cc887d7c1ed3dd023363b1ef3c8c610f2aff05f6434f37b4ec07a95045a2a
4
- data.tar.gz: fcb343c519261c8b6b4f13e7db67546c4c87efd2686b9656d12f8b7e508c506f
3
+ metadata.gz: 14545dce6ae6d5c5fc1df9deee0a1663b5e1d2eb86b52abf4804045538b453b7
4
+ data.tar.gz: 0b99531d4a7897ac09f3c572c2266897b9bef945c91f5d6030ce85633063ac89
5
5
  SHA512:
6
- metadata.gz: 8e5aeb86c274ef7e13107ff18c14de65312b20fb28f9e01cdd154d7869311ee68c8c6741aa8e2d37112c0aa5300208d3e61be3bad49cc2089fa27e551f101f55
7
- data.tar.gz: fdf0ec79bf57da759149ad016c99c5a92e51907518b05485e50fab729eca6bd13502323f46e3b552a806f88e7f6bb605a077693c1290f36890e3565d0763bc00
6
+ metadata.gz: 2d763dcae2a1b36faffab0024404b28d8064e3852d8ff8cd4212d388d42540442b0a4a7b814d76badfa4bd6656e8970bee1477ad94ab073f91e034d8ac2051ab
7
+ data.tar.gz: 53580c7f4e8e343eda72be22fd2b69093d44e13fc26a12c4a67180bbf34a89acc5e537b7f1f920c1668e8789dc246a120e9923df7ff840f141d6cf32085c9f8a
@@ -20,8 +20,7 @@ module Valideizer
20
20
  alias valideize add_rule
21
21
 
22
22
  def valideized?(params)
23
- reinit_errrors
24
- setup_defaults(params)
23
+ pre_process params
25
24
 
26
25
  params.each do |param, value|
27
26
  next unless nil_check(param, value)
@@ -35,7 +34,7 @@ module Valideizer
35
34
  end
36
35
 
37
36
  if @errors.empty?
38
- recast(params)
37
+ post_process params
39
38
  true
40
39
  else
41
40
  false
@@ -49,6 +48,11 @@ module Valideizer
49
48
 
50
49
  private
51
50
 
51
+ def pre_process(params)
52
+ reinit_errrors
53
+ setup_defaults params
54
+ end
55
+
52
56
  def reinit_errrors
53
57
  @errors = {}
54
58
  @error_messages = []
@@ -90,30 +94,46 @@ module Valideizer
90
94
  nil_rule.nil? ? false : nil_rule.last
91
95
  end
92
96
 
93
- def recast(params)
94
- @rules.each do |param, rules|
95
- next if params[param.to_s].nil? && params[param.to_sym].nil?
97
+ def post_process(params)
98
+ @rules.each do |key, rules|
99
+ next if params[key.to_s].nil? && params[key.to_sym].nil?
100
+ key = key.to_sym if params.include? key.to_sym
96
101
 
97
102
  type_rule = rules.find { |r, _c| r == :type }
103
+ recast(params, key, type_rule.last) if type_rule
98
104
 
99
- if type_rule
100
- constraint = type_rule.last
101
- param = param.to_sym if params.include? param.to_sym
102
- value = params[param]
103
-
104
- if validate(value, :type, constraint)
105
- params[param] = case constraint
106
- when :json then cast_from_json value
107
- when :bool then cast_to_bool value
108
- when :float then cast_to_float value
109
- when :integer then cast_to_integer value
110
- else value
111
- end
112
- end
113
- end
105
+ regexp_rule = rules.find { |r, _c| r == :regexp }
106
+ regexp_groups_substitution(params, key, regexp_rule.last) if regexp_rule
114
107
  end
115
108
  end
116
109
 
110
+
111
+ def recast(params, key, type)
112
+ value = params[key]
113
+ params[key] = case type
114
+ when :json then cast_from_json value
115
+ when :bool then cast_to_bool value
116
+ when :float then cast_to_float value
117
+ when :integer then cast_to_integer value
118
+ else value
119
+ end
120
+ end
121
+
122
+ def regexp_groups_substitution(params, key, regexp)
123
+ value = params[key]
124
+
125
+ matched = value.match regexp
126
+ return if matched.nil?
127
+
128
+ params[key] = if matched.named_captures.any?
129
+ matched.named_captures
130
+ elsif matched.captures.count > 1
131
+ matched.captures
132
+ elsif matched.captures.count == 1
133
+ matched.captures[0]
134
+ end
135
+ end
136
+
117
137
  def build_error_messages
118
138
  @error_messages = print_errors
119
139
  end
@@ -1,3 +1,3 @@
1
1
  module Valideizer
2
- VERSION = "1.1.8"
2
+ VERSION = "1.1.9"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valideizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.8
4
+ version: 1.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arthur 'ArtK0DE' Korochansky
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-14 00:00:00.000000000 Z
11
+ date: 2019-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -32,7 +32,6 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - lib/valideizer.rb
35
- - lib/valideizer/caster.rb
36
35
  - lib/valideizer/core.rb
37
36
  - lib/valideizer/error_printer.rb
38
37
  - lib/valideizer/holder.rb
@@ -1,20 +0,0 @@
1
- module Valideizer
2
- module Caster
3
- def cast_from_json(value)
4
- JSON.parse(value) rescue nil
5
- end
6
-
7
- def cast_to_integer(value)
8
- value.to_i rescue nil
9
- end
10
-
11
- def cast_to_float(value)
12
- value.to_f rescue nil
13
- end
14
-
15
- def cast_to_bool(value)
16
- return false if ['0', 'false'].include? value.to_s.downcase
17
- return true if ['1', 'true'].include? value.to_s.downcase
18
- end
19
- end
20
- end