gourami 0.4.0 → 0.5.0
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 +5 -5
- data/lib/gourami/coercer.rb +52 -53
- data/lib/gourami/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 98ec9c2d757ec710e65dd5aa0662530f0f92363b72d56edce9fecebe85d0d800
|
4
|
+
data.tar.gz: 80842660cd5e2ed28a244414ef8a8f8f091d111d8fd2ba614bcec52dc0efe807
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94897c3b427a7528020c6df8b657c193e88488b52610bf853d2e3fffda962ba1e0ba642ba7c72e2c9bb6ecf234518372910f733bca453d72a2b72c4ee469cb55
|
7
|
+
data.tar.gz: 12d3a6215c2fc337aa03af6d0818b916879aebe77cb96a21aed65d47ca7385a68eb096cc81dc22ff39ca3e187260ffd0a0f55060a4fb38db948eeb5a673a0e87
|
data/lib/gourami/coercer.rb
CHANGED
@@ -12,9 +12,7 @@ module Gourami
|
|
12
12
|
def setter_filter(attribute_name, value, options)
|
13
13
|
type = options[:type]
|
14
14
|
coercer_method_name = :"coerce_#{type}"
|
15
|
-
if type
|
16
|
-
value = send(coercer_method_name, value, options)
|
17
|
-
end
|
15
|
+
value = send(coercer_method_name, value, options) if type
|
18
16
|
|
19
17
|
super(attribute_name, value, options)
|
20
18
|
end
|
@@ -23,12 +21,17 @@ module Gourami
|
|
23
21
|
#
|
24
22
|
# @param value [Object]
|
25
23
|
# @option allow_nil [Boolean] (true)
|
24
|
+
# @option nil_when_empty [Boolean] (true)
|
26
25
|
# @option upcase [Boolean] (false)
|
27
26
|
# @option strip [Boolean] (true)
|
28
27
|
#
|
29
28
|
# @return [String, nil]
|
30
29
|
def coerce_string(value, options = {})
|
31
|
-
|
30
|
+
if options.fetch(:allow_nil, true)
|
31
|
+
return if value.nil?
|
32
|
+
value = value.to_s
|
33
|
+
return if value.empty? && options.fetch(:nil_when_empty, false)
|
34
|
+
end
|
32
35
|
|
33
36
|
value = value.to_s.dup.force_encoding(Encoding::UTF_8)
|
34
37
|
value.strip! if options.fetch(:strip, true)
|
@@ -44,9 +47,9 @@ module Gourami
|
|
44
47
|
#
|
45
48
|
# @return [Boolean, nil]
|
46
49
|
def coerce_boolean(value, options = {})
|
47
|
-
return
|
50
|
+
return if options[:allow_nil] && (value.nil? || value == "")
|
48
51
|
return false if value.to_s.strip == "false"
|
49
|
-
!!value && !coerce_string(value).strip.empty?
|
52
|
+
!!value && !coerce_string(value, :allow_nil => false).strip.empty?
|
50
53
|
end
|
51
54
|
|
52
55
|
# Coerce the value into an Array.
|
@@ -63,31 +66,29 @@ module Gourami
|
|
63
66
|
# @return [Array]
|
64
67
|
# The coerced Array.
|
65
68
|
def coerce_array(value, options = {})
|
69
|
+
return if options[:allow_nil] && value.nil?
|
70
|
+
|
66
71
|
element_type = options[:element_type]
|
67
|
-
value = value.values if value.
|
72
|
+
value = value.values if value.is_a?(Hash)
|
68
73
|
|
69
|
-
if options[:split_by] && value.
|
74
|
+
if options[:split_by] && value.is_a?(String)
|
70
75
|
value = value.split(options[:split_by]).map(&:strip)
|
71
76
|
end
|
72
77
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
element_type_options = {}
|
80
|
-
end
|
81
|
-
|
82
|
-
coercer_method_name = :"coerce_#{element_type}"
|
83
|
-
value.map do |array_element|
|
84
|
-
send(coercer_method_name, array_element, element_type_options)
|
85
|
-
end
|
86
|
-
else
|
87
|
-
value
|
88
|
-
end
|
78
|
+
return [] unless value.is_a?(Array)
|
79
|
+
return value unless element_type
|
80
|
+
|
81
|
+
if element_type.is_a?(Hash)
|
82
|
+
element_type_options = element_type
|
83
|
+
element_type = element_type[:type]
|
89
84
|
else
|
90
|
-
|
85
|
+
element_type_options = {}
|
86
|
+
end
|
87
|
+
|
88
|
+
coercer_method_name = :"coerce_#{element_type}"
|
89
|
+
|
90
|
+
value.map do |array_element|
|
91
|
+
send(coercer_method_name, array_element, element_type_options)
|
91
92
|
end
|
92
93
|
end
|
93
94
|
|
@@ -96,7 +97,7 @@ module Gourami
|
|
96
97
|
# @param value [Object]
|
97
98
|
#
|
98
99
|
# @return [Float, nil]
|
99
|
-
def coerce_float(value,
|
100
|
+
def coerce_float(value, _options = {})
|
100
101
|
Float(value) rescue nil
|
101
102
|
end
|
102
103
|
|
@@ -107,7 +108,8 @@ module Gourami
|
|
107
108
|
#
|
108
109
|
# @return [String, nil]
|
109
110
|
def coerce_phone(value, options = {})
|
110
|
-
value
|
111
|
+
value = coerce_string(value, options)
|
112
|
+
value ? value.upcase.gsub(/[^+0-9A-Z]/, "") : nil
|
111
113
|
end
|
112
114
|
|
113
115
|
# Coerce the value into a Hash.
|
@@ -124,17 +126,16 @@ module Gourami
|
|
124
126
|
def coerce_hash(value, options = {})
|
125
127
|
hash_key_type = options[:key_type]
|
126
128
|
hash_value_type = options[:value_type]
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
{}
|
129
|
+
|
130
|
+
return {} unless value.is_a?(Hash) || (defined?(Sequel::Postgres::JSONHash) && value.is_a?(Sequel::Postgres::JSONHash))
|
131
|
+
|
132
|
+
value.each_with_object({}) do |(key, value), coerced_hash|
|
133
|
+
key_type = hash_key_type.respond_to?(:call) ? hash_key_type.call(key, value) : hash_key_type
|
134
|
+
key = send("coerce_#{key_type}", key) if key_type
|
135
|
+
|
136
|
+
value_type = hash_value_type.respond_to?(:call) ? hash_value_type.call(key, value) : hash_value_type
|
137
|
+
value = send("coerce_#{value_type}", value) if value_type
|
138
|
+
coerced_hash[key] = value
|
138
139
|
end
|
139
140
|
end
|
140
141
|
|
@@ -145,13 +146,11 @@ module Gourami
|
|
145
146
|
#
|
146
147
|
# @return [Integer, nil]
|
147
148
|
# An Integer if the value can be coerced or nil otherwise.
|
148
|
-
def coerce_integer(value,
|
149
|
+
def coerce_integer(value, _options = {})
|
149
150
|
value = value.to_s
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
nil
|
154
|
-
end
|
151
|
+
return unless value =~ /\A0|[1-9]\d*\z/
|
152
|
+
|
153
|
+
value.to_i
|
155
154
|
end
|
156
155
|
|
157
156
|
# Coerce the value into a Date.
|
@@ -163,7 +162,7 @@ module Gourami
|
|
163
162
|
# A Date if the value can be coerced or nil otherwise.
|
164
163
|
def coerce_date(value, options = {})
|
165
164
|
value = coerce_string(value, options)
|
166
|
-
return
|
165
|
+
return if value.nil?
|
167
166
|
begin
|
168
167
|
Date.strptime(value, "%Y-%m-%d")
|
169
168
|
rescue ArgumentError
|
@@ -180,7 +179,7 @@ module Gourami
|
|
180
179
|
# A Time if the value can be coerced or nil otherwise.
|
181
180
|
def coerce_time(value, options = {})
|
182
181
|
value = coerce_string(value, options)
|
183
|
-
return
|
182
|
+
return if !value || value.empty?
|
184
183
|
|
185
184
|
begin
|
186
185
|
Time.parse(value).utc
|
@@ -198,13 +197,13 @@ module Gourami
|
|
198
197
|
# @return [Hash, nil]
|
199
198
|
# The hash will contain the file String :filename and the File :tempfile,
|
200
199
|
# or nil otherwise.
|
201
|
-
def coerce_file(value,
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
200
|
+
def coerce_file(value, _options = {})
|
201
|
+
return unless value.is_a?(Hash) && !value[:filename].to_s.empty?
|
202
|
+
|
203
|
+
tempfile = value[:tempfile]
|
204
|
+
return unless tempfile.is_a?(File) || tempfile.is_a?(Tempfile)
|
205
|
+
|
206
|
+
value
|
208
207
|
end
|
209
208
|
|
210
209
|
end
|
data/lib/gourami/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gourami
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TSMMark
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-10-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
requirements: []
|
121
121
|
rubyforge_project:
|
122
|
-
rubygems_version: 2.
|
122
|
+
rubygems_version: 2.7.7
|
123
123
|
signing_key:
|
124
124
|
specification_version: 4
|
125
125
|
summary: Keep your Routes, Controllers and Models thin.
|