powerpack 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/Rakefile +6 -0
- data/lib/powerpack.rb +2 -0
- data/lib/powerpack/array.rb +2 -0
- data/lib/powerpack/array/init.rb +15 -0
- data/lib/powerpack/array/tail.rb +15 -0
- data/lib/powerpack/hash/symbolize_keys.rb +1 -1
- data/lib/powerpack/string/format.rb +4 -0
- data/lib/powerpack/string/squish.rb +16 -3
- data/lib/powerpack/string/strip_margin.rb +2 -2
- data/lib/powerpack/version.rb +1 -1
- data/spec/powerpack/array/init_spec.rb +11 -0
- data/spec/powerpack/array/tail_spec.rb +11 -0
- data/spec/powerpack/hash/symbolize_keys_spec.rb +6 -0
- data/spec/powerpack/string/squish_spec.rb +10 -0
- data/spec/powerpack/string/strip_margin_spec.rb +11 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3390d88d1c5a0cfe6f53e3ae8ad15c4e60646d6
|
4
|
+
data.tar.gz: 80a7c32146541840c31b1a3eee54b1167a07795e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29545c9631dd461cf8688c963cc0aebc2f626a05f2f6bc183997a6c1d7d2fa7c8aa80b5cd795cdf64d8ded5b77f2a804927d830622667c4bbf3c0eadc6c918ea
|
7
|
+
data.tar.gz: 567070c2792eaa200c9844e5872a2207e6b86fd49d478c363f0d1e016defc3c6b465c9057611fcabd561c0bf587acfc8e5767c5584c0f8929c82233e871b8fd8
|
data/README.md
CHANGED
@@ -7,7 +7,7 @@ Powerpack offers some useful extensions to the standard Ruby classes (kind of li
|
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
10
|
-
Add this line to your application's Gemfile:
|
10
|
+
Add this line to your application's `Gemfile` (if you're using `bundler`, that is):
|
11
11
|
|
12
12
|
gem 'powerpack'
|
13
13
|
|
@@ -21,6 +21,9 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
## Extensions
|
23
23
|
|
24
|
+
* [Array](http://rdoc.info/github/bbatsov/powerpack/Array)
|
25
|
+
* [#init](http://rdoc.info/github/bbatsov/powerpack/Array#init-instance_method)
|
26
|
+
* [#tail](http://rdoc.info/github/bbatsov/powerpack/Array#tail-instance_method)
|
24
27
|
* [Hash](http://rdoc.info/github/bbatsov/powerpack/Hash)
|
25
28
|
* [#symbolize_keys](http://rdoc.info/github/bbatsov/powerpack/Hash#symbolize_keys-instance_method)
|
26
29
|
* [Numeric](http://rdoc.info/github/bbatsov/powerpack/Numeric)
|
@@ -32,6 +35,7 @@ Or install it yourself as:
|
|
32
35
|
* [#strip_indent](http://rdoc.info/github/bbatsov/powerpack/String#strip_indent-instance_method)
|
33
36
|
* [#strip_margin](http://rdoc.info/github/bbatsov/powerpack/String#strip_margin-instance_method)
|
34
37
|
* [#squish](http://rdoc.info/github/bbatsov/powerpack/String#squish-instance_method)
|
38
|
+
* [#squish!](http://rdoc.info/github/bbatsov/powerpack/String#squish!-instance_method)
|
35
39
|
|
36
40
|
## Documentation
|
37
41
|
|
data/Rakefile
CHANGED
data/lib/powerpack.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
unless Array.method_defined? :init
|
2
|
+
class Array
|
3
|
+
# Returns a new array that has all the elements of the current but the last.
|
4
|
+
#
|
5
|
+
# @return [Array] a new array without the last element or empty array if this
|
6
|
+
# array is empty
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# [1, 2, 3].init #=> [1, 2]
|
10
|
+
# [].init #=> []
|
11
|
+
def init
|
12
|
+
self[0...-1]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
unless Array.method_defined? :tail
|
2
|
+
class Array
|
3
|
+
# Returns a new array rejecting the current one's first element.
|
4
|
+
#
|
5
|
+
# @return [Array] a new array without the first element or nil if this
|
6
|
+
# array is empty
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# [1, 2, 3].tail #=> [2, 3]
|
10
|
+
# [].tail #=> nil
|
11
|
+
def tail
|
12
|
+
self[1..-1]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -8,6 +8,10 @@ unless String.method_defined? :format
|
|
8
8
|
# 'This is %s!'.format('Sparta') #=> 'This is Sparta!'
|
9
9
|
#
|
10
10
|
# @example
|
11
|
+
# 'My name is %{fname} %{lname}.'.format(fname: 'Bruce', lname: 'Wayne')
|
12
|
+
# #=> 'My name is Bruce Wayne.'
|
13
|
+
#
|
14
|
+
# @example
|
11
15
|
# '%d + %d'.format([1, 2]) #=> '1 + 2'
|
12
16
|
def format(*args)
|
13
17
|
super(self, *(args.flatten(1)))
|
@@ -2,13 +2,26 @@ unless String.method_defined? :squish
|
|
2
2
|
class String
|
3
3
|
# Strips leading and trailing whitespace and squashes internal whitespace.
|
4
4
|
#
|
5
|
-
# @return [String] a string with no leading and
|
6
|
-
# consecutive whitespace characters inside it
|
5
|
+
# @return [String] a new string with no leading and trailing
|
6
|
+
# whitespace and no consecutive whitespace characters inside it
|
7
7
|
#
|
8
8
|
# @example
|
9
9
|
# ' Peter Parker'.squish #=> 'Peter Parker'
|
10
10
|
def squish
|
11
|
-
|
11
|
+
dup.squish!
|
12
|
+
end
|
13
|
+
|
14
|
+
# Strips leading and trailing whitespace and squashes internal whitespace.
|
15
|
+
#
|
16
|
+
# @return [String] the string with no leading and trailing whitespace and no
|
17
|
+
# consecutive whitespace characters inside it
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
# ' Peter Parker'.squish #=> 'Peter Parker'
|
21
|
+
def squish!
|
22
|
+
strip!
|
23
|
+
gsub!(/\s+/, ' ')
|
24
|
+
self
|
12
25
|
end
|
13
26
|
end
|
14
27
|
end
|
@@ -13,8 +13,8 @@ unless String.method_defined? :strip_margin
|
|
13
13
|
# END
|
14
14
|
#
|
15
15
|
# #=> "def\n some_method\n \nother_method\nend"
|
16
|
-
def strip_margin(
|
17
|
-
margin =
|
16
|
+
def strip_margin(margin_characters)
|
17
|
+
margin = Regexp.quote(margin_characters)
|
18
18
|
gsub(/^\s+#{margin}/, '')
|
19
19
|
end
|
20
20
|
end
|
data/lib/powerpack/version.rb
CHANGED
@@ -6,4 +6,10 @@ describe 'Hash#symbolize_keys' do
|
|
6
6
|
|
7
7
|
expect(hash.symbolize_keys).to eq({ one: 1, two: 2 })
|
8
8
|
end
|
9
|
+
|
10
|
+
it 'leaves nonconvertible keys untouched' do
|
11
|
+
hash = { 1 => 'one', [1, 1] => 'ones' }
|
12
|
+
|
13
|
+
expect(hash.symbolize_keys).to eq({ 1 => 'one', [1, 1] => 'ones' })
|
14
|
+
end
|
9
15
|
end
|
@@ -9,3 +9,13 @@ describe 'String#squish' do
|
|
9
9
|
expect("Peter\r\n \t Parker".squish).to eq('Peter Parker')
|
10
10
|
end
|
11
11
|
end
|
12
|
+
|
13
|
+
describe 'String#squish!' do
|
14
|
+
it 'strips leading and trailing whitespace' do
|
15
|
+
expect(' Peter '.squish!).to eq('Peter')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'compacts internal whitespace' do
|
19
|
+
expect("Peter\r\n \t Parker".squish!).to eq('Peter Parker')
|
20
|
+
end
|
21
|
+
end
|
@@ -12,4 +12,15 @@ describe 'String#strip_margin' do
|
|
12
12
|
expect(code.strip_margin('|'))
|
13
13
|
.to eq("def test\n some_method\n other_method\nend\n")
|
14
14
|
end
|
15
|
+
|
16
|
+
it 'strips special characters margin from every line' do
|
17
|
+
code = <<-END
|
18
|
+
{{1}}def test
|
19
|
+
{{1}} some_method
|
20
|
+
{{1}}end
|
21
|
+
END
|
22
|
+
|
23
|
+
expect(code.strip_margin('{{1}}'))
|
24
|
+
.to eq("def test\n some_method\nend\n")
|
25
|
+
end
|
15
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: powerpack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bozhidar Batsov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -81,6 +81,9 @@ files:
|
|
81
81
|
- README.md
|
82
82
|
- Rakefile
|
83
83
|
- lib/powerpack.rb
|
84
|
+
- lib/powerpack/array.rb
|
85
|
+
- lib/powerpack/array/init.rb
|
86
|
+
- lib/powerpack/array/tail.rb
|
84
87
|
- lib/powerpack/hash.rb
|
85
88
|
- lib/powerpack/hash/symbolize_keys.rb
|
86
89
|
- lib/powerpack/numeric.rb
|
@@ -94,6 +97,8 @@ files:
|
|
94
97
|
- lib/powerpack/string/strip_margin.rb
|
95
98
|
- lib/powerpack/version.rb
|
96
99
|
- powerpack.gemspec
|
100
|
+
- spec/powerpack/array/init_spec.rb
|
101
|
+
- spec/powerpack/array/tail_spec.rb
|
97
102
|
- spec/powerpack/hash/symbolize_keys_spec.rb
|
98
103
|
- spec/powerpack/numeric/neg_spec.rb
|
99
104
|
- spec/powerpack/numeric/pos_spec.rb
|
@@ -128,6 +133,8 @@ signing_key:
|
|
128
133
|
specification_version: 4
|
129
134
|
summary: A few useful extensions to core Ruby classes.
|
130
135
|
test_files:
|
136
|
+
- spec/powerpack/array/init_spec.rb
|
137
|
+
- spec/powerpack/array/tail_spec.rb
|
131
138
|
- spec/powerpack/hash/symbolize_keys_spec.rb
|
132
139
|
- spec/powerpack/numeric/neg_spec.rb
|
133
140
|
- spec/powerpack/numeric/pos_spec.rb
|