gourami 0.4.0 → 0.5.0

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
- SHA1:
3
- metadata.gz: 603c7eab7f67f882417c3d2c3972b8632e50d420
4
- data.tar.gz: d9ddbf73b7e29f40443d3f2ff421f434c66ee073
2
+ SHA256:
3
+ metadata.gz: 98ec9c2d757ec710e65dd5aa0662530f0f92363b72d56edce9fecebe85d0d800
4
+ data.tar.gz: 80842660cd5e2ed28a244414ef8a8f8f091d111d8fd2ba614bcec52dc0efe807
5
5
  SHA512:
6
- metadata.gz: 2e98badb6ec80ba426d32da18e915944c7461e9154348efdc5c401cac9053224e4a82ed16ca78d89d81c6506cce4d6c940cf53e84e253e4ea87d1a7008634d1b
7
- data.tar.gz: dddd5426e7a1c497ccd0ff57c2536944d51077dd7317637fa3961943605afed939045047043017e2bdfe3bd2b8a8f22b93e11558a3f6e47206477b63cbceb57b
6
+ metadata.gz: 94897c3b427a7528020c6df8b657c193e88488b52610bf853d2e3fffda962ba1e0ba642ba7c72e2c9bb6ecf234518372910f733bca453d72a2b72c4ee469cb55
7
+ data.tar.gz: 12d3a6215c2fc337aa03af6d0818b916879aebe77cb96a21aed65d47ca7385a68eb096cc81dc22ff39ca3e187260ffd0a0f55060a4fb38db948eeb5a673a0e87
@@ -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
- return nil if value.nil? && options.fetch(:allow_nil, true)
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 nil if options[:allow_nil] && (value.nil? || value == "")
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.kind_of?(Hash)
72
+ value = value.values if value.is_a?(Hash)
68
73
 
69
- if options[:split_by] && value.kind_of?(String)
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
- if value.kind_of?(Array)
74
- if element_type
75
- if element_type.kind_of?(Hash)
76
- element_type_options = element_type
77
- element_type = element_type[:type]
78
- else
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, options = {})
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 ? coerce_string(value).upcase.gsub(/[^+0-9A-Z]/,"") : nil
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
- if value.kind_of?(Hash) || value.kind_of?(Sequel::Postgres::JSONHash)
128
- value.each_with_object({}) do |(key, value), coerced_hash|
129
- key_type = hash_key_type.respond_to?(:call) ? hash_key_type.call(key, value) : hash_key_type
130
- key = send("coerce_#{key_type}", key) if key_type
131
-
132
- value_type = hash_value_type.respond_to?(:call) ? hash_value_type.call(key, value) : hash_value_type
133
- value = send("coerce_#{value_type}", value) if value_type
134
- coerced_hash[key] = value
135
- end
136
- else
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, options = {})
149
+ def coerce_integer(value, _options = {})
149
150
  value = value.to_s
150
- if value.match(/\A0|[1-9]\d*\z/)
151
- value.to_i
152
- else
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 nil if value.nil?
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 nil if !value || value.empty?
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, options = {})
202
- if value.kind_of?(Hash) && !value[:filename].to_s.empty?
203
- tempfile = value[:tempfile]
204
- if tempfile.kind_of?(File) || tempfile.kind_of?(Tempfile)
205
- value
206
- end
207
- end
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
@@ -1,3 +1,3 @@
1
1
  module Gourami
2
- VERSION = "0.4.0".freeze
2
+ VERSION = "0.5.0".freeze
3
3
  end
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.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-02-22 00:00:00.000000000 Z
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.4.6
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.