augmented 0.2.2 → 0.2.7

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.
@@ -1,4 +1,5 @@
1
1
  require 'augmented/objects/iffy'
2
+ require 'augmented/objects/in'
2
3
  require 'augmented/objects/pickable'
3
4
  require 'augmented/objects/tackable'
4
5
  require 'augmented/objects/tappable'
@@ -7,6 +8,7 @@ require 'augmented/objects/thru'
7
8
  module Augmented
8
9
  module Objects
9
10
  include Iffy
11
+ include In
10
12
  include Pickable
11
13
  include Tackable
12
14
  include Tappable
@@ -0,0 +1,13 @@
1
+ module Augmented
2
+ module Objects
3
+ module In
4
+ refine Object do
5
+
6
+ def in? collection
7
+ collection.include?(self)
8
+ end
9
+
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require 'augmented/strings/blank'
2
+ require 'augmented/strings/squish'
3
+ require 'augmented/strings/truncatable'
4
+
5
+ module Augmented
6
+ module Strings
7
+ include Blank
8
+ include Squish
9
+ include Truncatable
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Augmented
2
+ module Strings
3
+ module Blank
4
+ REGEXP = /\A[[:space:]]*\z/
5
+
6
+ refine String do
7
+
8
+ def blank?
9
+ empty? || !!REGEXP.match(self)
10
+ end
11
+
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,40 @@
1
+ module Augmented
2
+ module Strings
3
+ module Squish
4
+ refine String do
5
+
6
+ def squish pattern = /\s+/, replacement = ' '
7
+ dup.squish!(pattern, replacement)
8
+ end
9
+
10
+ def squish! pattern = /\s+/, replacement = ' '
11
+ cursor = 0
12
+
13
+ while match = pattern.match(self, cursor)
14
+ slice_start = match.begin(0)
15
+ slice_end = match.end(0)
16
+ slice_length = slice_end - slice_start
17
+
18
+ slice!(slice_start, slice_length)
19
+
20
+ if slice_start == 0
21
+ # we're at the start of the string
22
+ # don't insert the replacement
23
+ # don't change the cursor
24
+ elsif slice_end >= self.size
25
+ # we're at the end of the string
26
+ # don't insert the replacement
27
+ break
28
+ else
29
+ insert(slice_start, replacement)
30
+ cursor = slice_start + replacement.size
31
+ end
32
+ end
33
+
34
+ self
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ module Augmented
2
+ module Strings
3
+ module Truncatable
4
+ refine String do
5
+
6
+ def truncate length
7
+ raise ArgumentError, 'length must be a non-negative integer' unless length && length.to_int >= 0
8
+
9
+ slice(0, length)
10
+ end
11
+
12
+ def truncate! length
13
+ replace(truncate(length))
14
+ self
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,3 +1,3 @@
1
1
  module Augmented
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: augmented
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
- - bruno
7
+ - brunze
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-10 00:00:00.000000000 Z
11
+ date: 2021-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,37 +16,37 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2.1'
19
+ version: '2'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2.1'
26
+ version: '2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 12.3.3
33
+ version: 13.0.3
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 12.3.3
40
+ version: 13.0.3
41
41
  description: Adds a few useful extra methods to some of Ruby's core types, available
42
42
  as refinements.
43
- email:
44
- - bruno@brunze.com
43
+ email:
45
44
  executables: []
46
45
  extensions: []
47
46
  extra_rdoc_files: []
48
47
  files:
49
48
  - ".gitignore"
49
+ - CHANGELOG.md
50
50
  - Gemfile
51
51
  - LICENSE.txt
52
52
  - README.md
@@ -57,6 +57,10 @@ files:
57
57
  - lib/augmented/arrays/tieable.rb
58
58
  - lib/augmented/enumerators.rb
59
59
  - lib/augmented/enumerators/indexing.rb
60
+ - lib/augmented/exceptions.rb
61
+ - lib/augmented/exceptions/chain.rb
62
+ - lib/augmented/exceptions/detailed.rb
63
+ - lib/augmented/exceptions/serializable.rb
60
64
  - lib/augmented/hashes.rb
61
65
  - lib/augmented/hashes/mappable.rb
62
66
  - lib/augmented/hashes/polymorphable.rb
@@ -65,6 +69,7 @@ files:
65
69
  - lib/augmented/modules/refined.rb
66
70
  - lib/augmented/objects.rb
67
71
  - lib/augmented/objects/iffy.rb
72
+ - lib/augmented/objects/in.rb
68
73
  - lib/augmented/objects/pickable.rb
69
74
  - lib/augmented/objects/tackable.rb
70
75
  - lib/augmented/objects/tappable.rb
@@ -72,29 +77,21 @@ files:
72
77
  - lib/augmented/procs.rb
73
78
  - lib/augmented/procs/chainable.rb
74
79
  - lib/augmented/procs/rescuable.rb
80
+ - lib/augmented/strings.rb
81
+ - lib/augmented/strings/blank.rb
82
+ - lib/augmented/strings/squish.rb
83
+ - lib/augmented/strings/truncatable.rb
75
84
  - lib/augmented/symbols.rb
76
85
  - lib/augmented/symbols/arguable.rb
77
86
  - lib/augmented/symbols/comparing.rb
78
87
  - lib/augmented/version.rb
79
- - test/augmented/arrays/tieable_test.rb
80
- - test/augmented/enumerators/indexing_test.rb
81
- - test/augmented/hashes/mappable_test.rb
82
- - test/augmented/hashes/polymorphable_test.rb
83
- - test/augmented/hashes/transformable_test.rb
84
- - test/augmented/modules/refined_test.rb
85
- - test/augmented/objects/iffy_test.rb
86
- - test/augmented/objects/pickable_test.rb
87
- - test/augmented/objects/tackable_test.rb
88
- - test/augmented/objects/tappable_test.rb
89
- - test/augmented/objects/thru_test.rb
90
- - test/augmented/procs/chainable_test.rb
91
- - test/augmented/procs/rescuable_test.rb
92
- - test/augmented/symbols/arguable_test.rb
93
- - test/augmented/symbols/comparing_test.rb
94
88
  homepage: https://github.com/brunze/augmented
95
89
  licenses:
96
90
  - MIT
97
- metadata: {}
91
+ metadata:
92
+ homepage_uri: https://github.com/brunze/augmented
93
+ source_code_uri: https://github.com/brunze/augmented
94
+ changelog_uri: https://github.com/brunze/augmented/CHANGELOG.md
98
95
  post_install_message:
99
96
  rdoc_options: []
100
97
  require_paths:
@@ -110,23 +107,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
107
  - !ruby/object:Gem::Version
111
108
  version: '0'
112
109
  requirements: []
113
- rubygems_version: 3.1.4
110
+ rubygems_version: 3.2.21
114
111
  signing_key:
115
112
  specification_version: 4
116
113
  summary: Useful extra methods for some Ruby core types.
117
- test_files:
118
- - test/augmented/arrays/tieable_test.rb
119
- - test/augmented/enumerators/indexing_test.rb
120
- - test/augmented/hashes/mappable_test.rb
121
- - test/augmented/hashes/polymorphable_test.rb
122
- - test/augmented/hashes/transformable_test.rb
123
- - test/augmented/modules/refined_test.rb
124
- - test/augmented/objects/iffy_test.rb
125
- - test/augmented/objects/pickable_test.rb
126
- - test/augmented/objects/tackable_test.rb
127
- - test/augmented/objects/tappable_test.rb
128
- - test/augmented/objects/thru_test.rb
129
- - test/augmented/procs/chainable_test.rb
130
- - test/augmented/procs/rescuable_test.rb
131
- - test/augmented/symbols/arguable_test.rb
132
- - test/augmented/symbols/comparing_test.rb
114
+ test_files: []
@@ -1,66 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/arrays/tieable'
3
-
4
- describe Augmented::Arrays::Tieable do
5
- using Augmented::Arrays::Tieable
6
-
7
- describe '#tie' do
8
-
9
- describe 'when supplied with an object' do
10
-
11
- it 'returns an array interweaved with the specified object' do
12
- object = Object.new
13
- weaved = %w(a b c).tie object
14
-
15
- assert_equal weaved, ['a', object, 'b', object, 'c']
16
- end
17
-
18
- it 'returns an empty array if the original array is empty' do
19
- assert_equal [].tie(1), []
20
- end
21
-
22
- it 'returns the original array if it has only one element' do
23
- assert_equal [42].tie(1), [42]
24
- end
25
-
26
- end
27
-
28
- describe 'when supplied with a block' do
29
-
30
- it 'returns an array interweaved with the result of invoking the block' do
31
- values = [10 ,20].each
32
- weaved = %w(a b c).tie{ values.next }
33
-
34
- assert_equal weaved, ['a', 10, 'b', 20, 'c']
35
- end
36
-
37
- it 'passes both neighbour values as arguments to the supplied block' do
38
- weaved = [1, 5, 12].tie{ |a, b| a + b }
39
-
40
- assert_equal weaved, [1, 6, 5, 17, 12]
41
- end
42
-
43
- it 'will weave nils if the block does not return anything else' do
44
- weaved = [1, 2, 3].tie{}
45
-
46
- assert_equal weaved, [1, nil, 2, nil, 3]
47
- end
48
-
49
- it 'returns an empty array if the original arrays is empty' do
50
- assert_equal [].tie{ 1 }, []
51
- end
52
-
53
- it 'returns the original array if it has only one element' do
54
- assert_equal [42].tie{ 1 }, [42]
55
- end
56
-
57
- end
58
-
59
- it 'raises an ArgumentError if not passed a non-nil object or block' do
60
- assert_raises(ArgumentError){ [].tie }
61
- assert_raises(ArgumentError){ [].tie nil }
62
- end
63
-
64
- end
65
-
66
- end
@@ -1,15 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/enumerators/indexing'
3
-
4
- describe Augmented::Enumerators::Indexing do
5
- using Augmented::Enumerators::Indexing
6
-
7
- describe '#index_by' do
8
-
9
- it 'returns a hash keyed by the results of invoking the criterion on every collection element and the values are the last element matching the criterion' do
10
- assert_equal ['a', 'bbb', 'c', 'dd'].to_enum.index_by(&:length), ({ 1 => 'c', 2 => 'dd', 3 => 'bbb' })
11
- end
12
-
13
- end
14
-
15
- end
@@ -1,37 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/hashes/mappable'
3
-
4
- describe Augmented::Hashes::Mappable do
5
- using Augmented::Hashes::Mappable
6
-
7
- describe '#map_values' do
8
-
9
- it 'returns a new hash with the same keys but transformed values' do
10
- assert_equal ({ aa: 11, bb: 22 }.map_values{ |i| i * 3 }), ({ aa: 33, bb: 66 })
11
- end
12
-
13
- it 'also provides the key and hash to the tranformer function as additional arguments' do
14
- hash = { aa: 11, bb: 22 }
15
- result = hash.map_values{ |i, key, h| [key, h.object_id] }
16
-
17
- assert_equal result.values, [[:aa, hash.object_id], [:bb, hash.object_id]]
18
- end
19
-
20
- end
21
-
22
- describe '#map_keys' do
23
-
24
- it 'returns a new hash with the same values but transformed keys' do
25
- assert_equal ({ aa: 11, bb: 22 }.map_keys{ |k| k.to_s[0] }), ({ 'a' => 11, 'b' => 22 })
26
- end
27
-
28
- it 'also provides the value and hash to the tranformer function as additional arguments' do
29
- hash = { aa: 11, bb: 22 }
30
- result = hash.map_keys{ |k, value, h| [value, h.object_id] }
31
-
32
- assert_equal result.keys, [[11, hash.object_id], [22, hash.object_id]]
33
- end
34
-
35
- end
36
-
37
- end
@@ -1,45 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/hashes/polymorphable'
3
- require 'ostruct'
4
-
5
- describe Augmented::Hashes::Polymorphable do
6
- using Augmented::Hashes::Polymorphable
7
-
8
- describe '#polymorph' do
9
-
10
- it 'returns an object of the class specified by the `:type` attribute, initialized with the hash itself' do
11
- object = { type: 'OpenStruct', speak: 'meeehh' }.polymorph
12
-
13
- assert_instance_of OpenStruct, object
14
- assert_equal object.speak, 'meeehh'
15
- end
16
-
17
- describe 'type attribute' do
18
-
19
- it 'can also be a string key in the hash' do
20
- assert_instance_of OpenStruct, { 'type' => 'OpenStruct' }.polymorph
21
- end
22
-
23
- it 'can be an arbitrary attribute in the hash' do
24
- assert_instance_of OpenStruct, { lorem_ipsum: 'OpenStruct' }.polymorph(:lorem_ipsum)
25
- assert_instance_of OpenStruct, { 'lorem_ipsum' => 'OpenStruct' }.polymorph(:lorem_ipsum)
26
- end
27
-
28
- it 'can be a class' do
29
- assert_instance_of OpenStruct, { type: OpenStruct }.polymorph()
30
- end
31
-
32
- it 'can be a class passed directly to the method, ignoring the type attribute in the hash' do
33
- assert_instance_of OpenStruct, { type: 'TotallyIgnoredClass' }.polymorph(OpenStruct)
34
- end
35
-
36
- end
37
-
38
- it 'raises an error if it cannot find a type class' do
39
- assert_raises(ArgumentError){ {}.polymorph }
40
- assert_raises(ArgumentError){ { type: nil }.polymorph }
41
- end
42
-
43
- end
44
-
45
- end
@@ -1,87 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'augmented/hashes/transformable'
3
- require 'ostruct'
4
-
5
- describe Augmented::Hashes::Transformable do
6
- using Augmented::Hashes::Transformable
7
-
8
- describe '#transform(!)' do
9
-
10
- it 'mutates the values of the given keys by applying their respective procables' do
11
- hash = { thing1: 100, thing2: OpenStruct.new(random_method_name: 900) }.freeze
12
-
13
- new_hash = hash.transform thing1: -> i { i * 3 }, thing2: :random_method_name
14
-
15
- assert_equal new_hash[:thing1], 300
16
- assert_equal new_hash[:thing2], 900
17
-
18
-
19
- # mutable version test:
20
- hash = { thing1: 100, thing2: OpenStruct.new(random_method_name: 900) }
21
-
22
- hash.transform! thing1: -> i { i * 3 }, thing2: :random_method_name
23
-
24
- assert_equal hash[:thing1], 300
25
- assert_equal hash[:thing2], 900
26
- end
27
-
28
- it 'applies procables recursively when given a hash' do
29
- hash = { a: { b: { c: 100 } } }.freeze
30
-
31
- new_hash = hash.transform({ a: { b: { c: -> i { i * 3 } } } })
32
-
33
- assert_equal new_hash[:a][:b][:c], 300
34
-
35
-
36
- # mutable version test:
37
- hash = { a: { b: { c: 100 } } }
38
-
39
- hash.transform!({ a: { b: { c: -> i { i * 3 } } } })
40
-
41
- assert_equal hash[:a][:b][:c], 300
42
- end
43
-
44
- it 'applies procables to all elements of a collection if a value is iterable (iterable MUST be a collection of hashes)' do
45
- hash = { a: [ { my_value: 10 }, { my_value: 20 } ] }.freeze
46
-
47
- new_hash = hash.transform(a: { my_value: -> i { i * 3 } })
48
-
49
- assert_equal new_hash[:a], [ { my_value: 30 }, { my_value: 60 } ]
50
-
51
-
52
- # mutable version test:
53
- hash = { a: [ { my_value: 10 }, { my_value: 20 } ] }
54
-
55
- hash.transform!(a: { my_value: -> i { i * 3 } })
56
-
57
- assert_equal hash[:a], [ { my_value: 30 }, { my_value: 60 } ]
58
- end
59
-
60
- it 'can apply several procables to a value, supplied in an array, executed from left to right' do
61
- add_ten = -> i { i + 10 }
62
- double = -> i { i * 2 }
63
-
64
- hash = { a: 2.5 }.freeze
65
-
66
- new_hash = hash.transform a: [ :to_i, add_ten, double ]
67
-
68
- assert_equal new_hash[:a], 24
69
-
70
-
71
- # mutable version test:
72
- hash = { a: 2.5 }
73
-
74
- hash.transform! a: [ :to_i, add_ten, double ]
75
-
76
- assert_equal hash[:a], 24
77
- end
78
-
79
- it 'returns itself (mutable version only)' do
80
- hash = {}
81
- same_hash = hash.transform! Hash.new
82
-
83
- assert_equal same_hash.object_id, hash.object_id
84
- end
85
-
86
- end
87
- end