rearmed 1.3.0 → 1.3.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: 1a547fa7cd7259d77eee0a4707cd1cf9589e9aa2
4
- data.tar.gz: a0be2f3e5e61f6c6ffb8da8213bfd9dd7b767b6d
3
+ metadata.gz: 8cd8b9873096a342bb640e7bab688970350bf050
4
+ data.tar.gz: 84ac122091acba8d89b1c4f1b99bf2e9ff3896e2
5
5
  SHA512:
6
- metadata.gz: f4fff0c4782edd10fee028e471469fa73ab5f8a0b8c0fa6a91f6debe23a9451e2afe7e45fe09c34ef9c6ad3b38686d5cb5f9fee199108706c6f86766279f429b
7
- data.tar.gz: a6ba345629c2076c05c8fd219eddc4c4fe63772ebd683d81f967328759e1a88c1f862b636a27330f34df8dd0c5086901f74d0956ec68a7ce15cb8d98c9697183
6
+ metadata.gz: 704335a6f2403d194b54bfbca86c6660d49f3c6a5eab6eb2031d813032e9c7d62ea7b03f35d11dd6a11adf251a6b15c706e39046e2fb298f093d2904e313ddd7
7
+ data.tar.gz: d8d2552250bacd242eac8e83d492d6cde941a0177d4cbbc8888fececb7ffe568b7f336fa553bcc18746214dc02836744f3542aa34c998ba4694f8ea820e170fd
@@ -1,6 +1,10 @@
1
1
  CHANGELOG
2
2
  ---------
3
3
 
4
+ - **1.3.1 - Sept 2, 2017**
5
+ - Add `Enumerable#select_map`
6
+ - Add `String#casecmp?` for Ruby 2.3.x and below
7
+ - Fix ruby 1.9.3 lambda syntax cannot contain space between stab and parenthesis
4
8
  - **1.3.0 - Feb 28, 2017**
5
9
  - Remove Rails and Minitest methods. Those methods moved to https://github.com/westonganger/rearmed_rails
6
10
  - Change methods names of Rearmed namespaced hash methods to convey better meaning
data/README.md CHANGED
@@ -1,17 +1,18 @@
1
- # Rearmed Ruby
2
- <a href='https://ko-fi.com/A5071NK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>
1
+ # Rearmed Ruby <a href='https://travis-ci.org/westonganger/rearmed-rb' target='_blank'><img src='https://api.travis-ci.org/westonganger/rearmed-rb.svg?branch=master' border='0' alt='Build Status' /></a>
3
2
 
4
- A collection of helpful methods and monkey patches for Objects, Strings, Enumerables, Arrays, Hash, Dates
3
+ <a href='https://ko-fi.com/A5071NK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>
5
4
 
6
- I have recently extracted the Rails and Minitest monkey patches to another gem https://github.com/westonganger/rearmed_rails because the Rails methods are getting quite extensive.
5
+ A collection of helpful methods and monkey patches for Arrays, Hash, Enumerables, Strings, Objects & Dates in Ruby. [Rearmed is a collection of plugins](https://github.com/westonganger?utf8=%E2%9C%93&tab=repositories&q=rearmed) which are driven by making life easier & coding more natural.
7
6
 
8
7
  The difference between this library and others is that all monkey patching is performed in an opt-in way because you shouldnt be using methods you dont know about anyways.
9
8
 
10
9
  When possible I have placed the method implementations inside the Rearmed module so if you don't like monkey patching or are working on the project with a team then you can use these methods instead. You can then skip the config and see how to use each implementation below the relevant methods documentation.
11
10
 
12
- ```ruby
13
- # Gemfile
14
11
 
12
+ # Install
13
+
14
+ Add the following line to your gemfile:
15
+ ```ruby
15
16
  gem 'rearmed'
16
17
  ```
17
18
 
@@ -33,7 +34,8 @@ Rearmed.enabled_patches = {
33
34
  },
34
35
  enumerable: {
35
36
  natural_sort: false,
36
- natural_sort_by: false
37
+ natural_sort_by: false,
38
+ select_map: false
37
39
  },
38
40
  hash: {
39
41
  compact: false,
@@ -48,6 +50,7 @@ Rearmed.enabled_patches = {
48
50
  },
49
51
  string: {
50
52
  begins_with: false,
53
+ casecmp?: false
51
54
  ends_with: false,
52
55
  starts_with: false,
53
56
  to_bool: false,
@@ -72,7 +75,7 @@ puts array # => [4,1]
72
75
 
73
76
  array.not_empty? # => true
74
77
 
75
- # Only available on array and hash in Ruby 2.2.x or below
78
+ # Only monkey patched if using Ruby 2.2.x or below as this method was added to Ruby core in 2.3.0
76
79
  items = [{foo: ['foo','bar']}, {test: 'thing'}]
77
80
  items.dig(0, :foo, 1) # => 'bar'
78
81
  # or without monkey patch: Rearmed.dig(items, 0, :foo, 1)
@@ -88,6 +91,10 @@ items.natural_sort(reverse: true) # because natural_sort does not accept a block
88
91
  items = [{version: "1.1"}, {version: "1.11"}, {version: "1.2"}]
89
92
  items.natural_sort_by{|x| x[:version]}
90
93
  # or without monkey patch: Rearmed.natural_sort_by(items){|x| x[:version]}
94
+
95
+ items = [{version: "1.1"}, {version: nil}, {version: false}]
96
+ items.select_map{|x| x[:version]} #=> [{version: "1.1"}]
97
+ # or without monkey patch: Rearmed.select_map(items){|x| x[:version]}
91
98
  ```
92
99
 
93
100
  ## Date
@@ -108,12 +115,12 @@ hash.only!(:foo, :bar)
108
115
  hash.to_struct
109
116
  # or without monkey patch: Rearmed.hash_to_struct(hash)
110
117
 
111
- # Only available on array and hash in Ruby 2.2.x or below
118
+ # Only monkey patched if using Ruby 2.2.x or below as this method was added to Ruby core in 2.3.0
112
119
  items = [{foo: ['foo','bar']}, {test: 'thing'}]
113
120
  items.dig(0, :foo, 1) # => 'bar'
114
121
  # or without monkey patch: Rearmed.dig(items, 0, :foo, 1)
115
122
 
116
- # Only available on array and hash in Ruby 2.3.x or below
123
+ # Only monkey patched if using Ruby 2.3.x or below as this method was added to Ruby core in 2.4.0
117
124
  hash.compact
118
125
  # or without monkey patch: Rearmed.hash_compact(hash)
119
126
  hash.compact!
@@ -123,7 +130,7 @@ hash.compact!
123
130
  ```ruby
124
131
  my_var.not_nil?
125
132
 
126
- # In ActiveSupport / Rails this is not patched as this method is already defined there
133
+ # Only monkey patched if not using ActiveSupport / Rails as this method is already defined there
127
134
  my_var.in?([1,2,3])
128
135
  my_var.in?(1,2,3) # or with splat arguments
129
136
  ```
@@ -143,6 +150,11 @@ my_var.in?(1,2,3) # or with splat arguments
143
150
  'foo'.starts_with?('fo') # => true
144
151
  'foo'.begins_with?('fo') # => true
145
152
  'bar'.ends_with?('ar') # => true
153
+
154
+ # Only monkey patched if using Ruby 2.3.x or below as this method was added to Ruby core in 2.4.0
155
+ 'foo'.casecmp?('FOO') #=> true
156
+ 'foo'.casecmp?('FOOBAR') #=> false
157
+ # or without monkey patch: Rearmed.casecmp?('foo', 'FOO')
146
158
  ```
147
159
 
148
160
  # Contributing / Todo
@@ -154,6 +166,11 @@ If you want to contribute here are a couple of things you could do:
154
166
 
155
167
 
156
168
  # Credits
157
- Created by Weston Ganger - @westonganger
169
+ Created by Weston Ganger - [@westonganger](https://github.com/westonganger)
158
170
 
159
- <a href='https://ko-fi.com/A5071NK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>
171
+ For any consulting or contract work please contact me via my company website: [Solid Foundation Web Development](https://solidfoundationwebdev.com)
172
+
173
+ ## Similar Libraries Created By Me
174
+ - [Rearmed Rails](https://github.com/westonganger/rearmed_rails)
175
+ - [Rearmed-JS](https://github.com/westonganger/rearmed-js)
176
+ - [Rearmed-CSS](https://github.com/westonganger/rearmed-css)
@@ -9,7 +9,8 @@
9
9
  },
10
10
  enumerable: {
11
11
  natural_sort: false,
12
- natural_sort_by: false
12
+ natural_sort_by: false,
13
+ select_map: false
13
14
  },
14
15
  hash: {
15
16
  compact: false,
@@ -24,6 +25,7 @@
24
25
  },
25
26
  string: {
26
27
  begins_with: false,
28
+ casecmp?: false,
27
29
  ends_with: false,
28
30
  starts_with: false,
29
31
  to_bool: false,
@@ -1,5 +1,11 @@
1
1
  module Rearmed
2
2
 
3
+ def self.casecmp?(str1, str2)
4
+ if str1.is_a?(String) && str2.is_a?(String)
5
+ str1.casecmp(str2) == 0
6
+ end
7
+ end
8
+
3
9
  def self.dig(collection, *values)
4
10
  current_val = nil
5
11
  current_collection = collection
@@ -35,11 +41,13 @@ module Rearmed
35
41
  return current_val
36
42
  end
37
43
 
44
+ def self.hash_compact(hash)
45
+ hash.reject{|_, value| value.nil?}
46
+ end
47
+
38
48
  def self.hash_join(hash, delimiter=', ', &block)
39
49
  unless block_given?
40
- block = -> (k,v) {
41
- "#{k}: #{v}"
42
- }
50
+ block = ->(k,v){ "#{k}: #{v}" }
43
51
  end
44
52
 
45
53
  str = ""
@@ -59,15 +67,24 @@ module Rearmed
59
67
  return str
60
68
  end
61
69
 
62
- def self.natural_sort_by(array)
63
- array.sort_by{|x| self.naturalize_str(yield(x))}
70
+ def self.hash_only(hash, *keys)
71
+ keys.map!{|key| hash.convert_key(key)} if hash.respond_to?(:convert_key, true)
72
+ keys.each_with_object(hash.class.new){|k, new_hash| new_hash[k] = hash[k] if hash.has_key?(k)}
73
+ end
74
+
75
+ def self.hash_to_struct(hash)
76
+ Struct.new(*hash.keys).new(*hash.values)
77
+ end
78
+
79
+ def self.natural_sort_by(collection)
80
+ collection.sort_by{|x| self.naturalize_str(yield(x))}
64
81
  end
65
82
 
66
- def self.natural_sort(array, options={})
83
+ def self.natural_sort(collection, options={})
67
84
  if block_given?
68
85
  Rearmed::Exceptions::BlockFoundError
69
86
  else
70
- array.sort do |a,b|
87
+ collection.sort do |a,b|
71
88
  if options[:reverse] == true
72
89
  self.naturalize_str(b.to_s) <=> self.naturalize_str(a.to_s)
73
90
  else
@@ -76,14 +93,13 @@ module Rearmed
76
93
  end
77
94
  end
78
95
  end
79
-
80
- def self.hash_compact(hash)
81
- hash.reject{|_, value| value.nil?}
82
- end
83
96
 
84
- def self.hash_only(hash, *keys)
85
- keys.map!{|key| hash.convert_key(key)} if hash.respond_to?(:convert_key, true)
86
- keys.each_with_object(hash.class.new){|k, new_hash| new_hash[k] = hash[k] if hash.has_key?(k)}
97
+ def self.select_map(collection)
98
+ if block_given?
99
+ collection.select{|x| yield(x) }.map{|x| yield(x) }
100
+ else
101
+ collection.select.map
102
+ end
87
103
  end
88
104
 
89
105
  def self.to_bool(str)
@@ -104,10 +120,6 @@ module Rearmed
104
120
  str =~ /(^(\d+)(\.)?(\d+)?$)|(^(\d+)?(\.)(\d+)$)/ ? true : false
105
121
  end
106
122
 
107
- def self.hash_to_struct(hash)
108
- Struct.new(*hash.keys).new(*hash.values)
109
- end
110
-
111
123
  private
112
124
 
113
125
  def self.naturalize_str(str)
@@ -1,9 +1,13 @@
1
- date_enabled = Rearmed.enabled_patches[:date] == true
1
+ if Rearmed.enabled_patches[:date] == true || Rearmed.dig(Rearmed.enabled_patches, :date, :now)
2
+
3
+ require 'date'
4
+
5
+ Date.class_eval do
2
6
 
3
- Date.class_eval do
4
- if date_enabled || Rearmed.dig(Rearmed.enabled_patches, :date, :now)
5
7
  def self.now
6
8
  DateTime.now.to_date
7
9
  end
10
+
8
11
  end
12
+
9
13
  end
@@ -16,4 +16,14 @@ Enumerable.module_eval do
16
16
  end
17
17
  end
18
18
  end
19
+
20
+ if enumerable_enabled || Rearmed.dig(Rearmed.enabled_patches, :enumerable, :select_map)
21
+ def select_map
22
+ if block_given?
23
+ Rearmed.select_map(self){|x| yield(x)}
24
+ else
25
+ Rearmed.select_map(self)
26
+ end
27
+ end
28
+ end
19
29
  end
@@ -1,6 +1,18 @@
1
1
  string_enabled = Rearmed.enabled_patches[:string] == true
2
2
 
3
3
  String.class_eval do
4
+ if !''.respond_to?(:casecmp?) && (string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :casecmp?))
5
+ def casecmp?(str)
6
+ Rearmed.casecmp?(self, str)
7
+ end
8
+ end
9
+
10
+ if string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :to_bool)
11
+ def to_bool
12
+ Rearmed.to_bool(self)
13
+ end
14
+ end
15
+
4
16
  if string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :valid_integer)
5
17
  def valid_integer?
6
18
  Rearmed.valid_integer?(self)
@@ -13,12 +25,6 @@ String.class_eval do
13
25
  end
14
26
  end
15
27
 
16
- if string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :to_bool)
17
- def to_bool
18
- Rearmed.to_bool(self)
19
- end
20
- end
21
-
22
28
  if string_enabled || Rearmed.dig(Rearmed.enabled_patches, :string, :starts_with)
23
29
  alias_method :starts_with?, :start_with?
24
30
  end
@@ -1,3 +1,3 @@
1
1
  module Rearmed
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
@@ -42,6 +42,10 @@ class TestRearmed < MiniTest::Test
42
42
  str = '32a'
43
43
  eql(str.valid_integer?, false)
44
44
 
45
+
46
+ str = '12'
47
+ eql(Rearmed.valid_integer?(str), true)
48
+
45
49
  str = '132.2'
46
50
  eql(str.valid_float?, true)
47
51
 
@@ -51,6 +55,10 @@ class TestRearmed < MiniTest::Test
51
55
  str = '12.1a'
52
56
  eql(str.valid_float?, false)
53
57
 
58
+ str = '1.2'
59
+ eql(Rearmed.valid_float?(str), true)
60
+
61
+
54
62
  str = 'true'
55
63
  eql(str.to_bool, true)
56
64
 
@@ -60,15 +68,29 @@ class TestRearmed < MiniTest::Test
60
68
  str = 'not true'
61
69
  assert_nil(str.to_bool)
62
70
 
63
-
64
71
  str = 'true'
65
72
  eql(Rearmed.to_bool(str), true)
66
73
 
67
- str = '12'
68
- eql(Rearmed.valid_integer?(str), true)
69
74
 
70
- str = '1.2'
71
- eql(Rearmed.valid_float?(str), true)
75
+ str1 = 'foo'
76
+ str2 = 'foo'
77
+ eql(str1.casecmp?(str2), true)
78
+
79
+ str1 = 'foo'
80
+ str2 = 'FOO'
81
+ eql(str1.casecmp?(str2), true)
82
+
83
+ str1 = 'foo'
84
+ str2 = 'foobar'
85
+ eql(str1.casecmp?(str2), false)
86
+
87
+ str1 = 'foo'
88
+ str2 = 'fo'
89
+ eql(str1.casecmp?(str2), false)
90
+
91
+ str1 = 'foo'
92
+ str2 = 'foo'
93
+ eql(Rearmed.casecmp?(str1, str2), true)
72
94
  end
73
95
 
74
96
  def test_date
@@ -77,36 +99,21 @@ class TestRearmed < MiniTest::Test
77
99
 
78
100
  def test_enumerable
79
101
  items = ['1.1', '1.11', '1.2']
80
-
81
102
  eql(items.natural_sort, ['1.1','1.2','1.11'])
82
-
83
103
  eql(items.natural_sort(reverse: true), ['1.11','1.2','1.1'])
84
-
85
104
  eql(Rearmed.natural_sort(items), ['1.1','1.2','1.11'])
86
-
87
105
  eql(Rearmed.natural_sort(items, reverse: true), ['1.11','1.2','1.1'])
88
106
 
89
-
90
107
  items = [{version: "1.1"}, {version: "1.11"}, {version: "1.2"}]
91
-
92
108
  eql(items.natural_sort_by{|x| x[:version]}, [{version: "1.1"}, {version: "1.2"}, {version: "1.11"}])
93
-
94
109
  eql(Rearmed.natural_sort_by(items){|x| x[:version]}, [{version: "1.1"}, {version: "1.2"}, {version: "1.11"}])
95
110
 
111
+ items = [0, 1, 2, 3, nil, false]
112
+ eql(items.select_map{|x| x}, [0,1,2,3])
113
+ eql(Rearmed.select_map(items){|x| x}, [0,1,2,3])
96
114
 
97
- # Only available on array and hash in Ruby 2.2.x or below
98
- array = [{foo: ['foo','bar']}, {test: 'thing'}]
99
-
100
- eql(array.dig(0, :foo, 1), 'bar')
101
- assert_nil(array.dig(0, :foo, 2))
102
-
103
- eql(Rearmed.dig(array, 1, :test), 'thing')
104
- assert_nil(Rearmed.dig(array, 1, :bar))
105
-
106
- hash = {a: {foo: ['bar']}, b: {c: 'c'}}
107
-
108
- eql(hash.dig(:a, :foo, 0), 'bar')
109
- eql(Rearmed.dig(hash, :b, :c), 'c')
115
+ assert items.select_map.is_a?(Enumerator)
116
+ assert Rearmed.select_map(items).is_a?(Enumerator)
110
117
  end
111
118
 
112
119
  def test_array
@@ -123,6 +130,12 @@ class TestRearmed < MiniTest::Test
123
130
  eql(array, [1,2,1,3,4,1])
124
131
 
125
132
  eql(array.not_empty?, true)
133
+
134
+ array = [{foo: ['foo','bar']}, {test: 'thing'}]
135
+ eql(array.dig(0, :foo, 1), 'bar')
136
+ assert_nil(array.dig(0, :foo, 2))
137
+ eql(Rearmed.dig(array, 1, :test), 'thing')
138
+ assert_nil(Rearmed.dig(array, 1, :bar))
126
139
  end
127
140
 
128
141
  def test_hash
@@ -164,6 +177,10 @@ class TestRearmed < MiniTest::Test
164
177
  assert(struct.is_a?(Struct))
165
178
  eql(struct.foo, :bar)
166
179
  eql(struct.bar, 'foo')
180
+
181
+ hash = {a: {foo: ['bar']}, b: {c: 'c'}}
182
+ eql(hash.dig(:a, :foo, 0), 'bar')
183
+ eql(Rearmed.dig(hash, :b, :c), 'c')
167
184
  end
168
185
 
169
186
  def test_object
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rearmed
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Ganger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-28 00:00:00.000000000 Z
11
+ date: 2017-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake