blank_empty_nil_filters 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 64e75e1a9ee5dcae8ca6f3e8b538765f8350634b
4
- data.tar.gz: 152de1254ee4cb1e9a5874e8d8f7203afa325539
3
+ metadata.gz: 31f1a60dad0e7ba230e21358acf7a044d79852ac
4
+ data.tar.gz: 7ace2d848b525742ba194b7300e21ef1716a6366
5
5
  SHA512:
6
- metadata.gz: e3168af58aac78538ff99afad4d81e56ce78bcf6c23e2c3999d18a78971870512bb057cd2560972c6ddf4d0c6e3d1fc8e9577ff9cce21e4f6f110b883607673b
7
- data.tar.gz: eccbcf9941d4a2f043503043e941094b08b3b960a28cf2c2073ac495b337a7fe65ff020a600a7281ce28a6624a36a0ab9f8dd0a2d8f67f210b44fd2d010c2fd8
6
+ metadata.gz: 99a10a6017b9dbbeafcff7acdd8ef9d15fca64dbc638dabbf479113017533fb3ca219351b47f2f38332deec2d16de6eea2f9b9ed822e5a86d8277925ad3c65fc
7
+ data.tar.gz: bf611e7b505e56e571e4bc78cd2ff61c48b49a23fe993db5773c5f46e5cb41bd16b9006f016f0d07daa6e6e6604afbb7057d02b6816935e0b00b2c799f71c0c1
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- blank_empty_nil_filters (0.1.0)
4
+ blank_empty_nil_filters (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -82,6 +82,18 @@ aoh.reject_values(:condition_method) # reject items matching :condition_method
82
82
  aoh.select_values(:condition_method) # select items matching :condition_method
83
83
  ```
84
84
 
85
+ There are two `Object` instance methods of `no_empty_value` and `no_blank_value` that can be used as `String` or
86
+ other class instance filters to return the instance or a nil. This enables the use of `||` conditions to provide
87
+ alternative values:
88
+
89
+ ```ruby
90
+ obj.no_empty_value || 'default'
91
+ obj.no_blank_value || 'default'
92
+ ```
93
+
94
+ These two methods are like `ActiveSupport`'s `presence` method but with finer-grain control.
95
+
96
+ ### Condition Methods
85
97
 
86
98
  The methods below are used with the general purpose methods to form the special-case filters.
87
99
 
@@ -98,6 +110,7 @@ obj.non_nil? # !object.nil?
98
110
  For _hash_ or _array_ objects, `is_empty?` and `is_blank?` work recursively on the object
99
111
  elements, and the result is true only if true at every level.
100
112
 
113
+ ### Hash key and value filters
101
114
  There are also convenience methods to select the keys from filtered hash items:
102
115
 
103
116
  ```ruby
@@ -190,10 +203,24 @@ To filter out _blank_ values from an array:
190
203
  [:1 " " " " nil ['' ' '] :2].no_blank_values => [:1 :2]
191
204
  ```
192
205
 
206
+ ### String Filters
207
+
208
+ The `no_empty_value` and `no_blank_value` methods on the `Object` class can be applied to any object that supports the `is_blank?` and `is_empty?` condition methods, including the `String` class.
209
+
210
+ ```ruby
211
+ ' no fun '.no_empty_value || 'other' ==> ' no fun '
212
+ ''. no_empty_value || 'other' ==> 'other'
213
+ ' '. no_empty_value || 'other' ==> ' '
214
+ ' answer '.no_blank_value || 'no' ==> ' answer '
215
+ ' '. no_blank_value || 'no' ==> 'no'
216
+ '\n'. no_blank_value || 'no' ==> 'no'
217
+ ```
218
+
219
+
220
+
193
221
  ## Testing
194
222
 
195
- There is an `rspec`-style test in the `spec` directory, the perusal of which will also provide
196
- some examples of usage.
223
+ There is an `rspec`-style test in the `spec` directory, the perusal of which will also provide some examples of usage.
197
224
 
198
225
  The rspec-style tests are normally run under the `fuubar` formatter gem which shows an incremental summary on a single line. The
199
226
  tests can be run with `rake` or with `rspec`:
@@ -151,6 +151,14 @@ module BlankEmptyNilFilters
151
151
  end
152
152
 
153
153
  module ObjectExtensions
154
+ def no_blank_value
155
+ is_blank? ? nil : self
156
+ end
157
+
158
+ def no_empty_value
159
+ is_empty? ? nil : self
160
+ end
161
+
154
162
  def is_empty?
155
163
  if nil?
156
164
  true
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlankEmptyNilFilters
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blank_empty_nil_filters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alan Stebbens
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-12 00:00:00.000000000 Z
11
+ date: 2019-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler