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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d33e4476c5faccdafe8e8065366f11e4a65478e7
4
- data.tar.gz: 5c592ba39bcd2d5e57210e17e27138d245e4420e
3
+ metadata.gz: c3390d88d1c5a0cfe6f53e3ae8ad15c4e60646d6
4
+ data.tar.gz: 80a7c32146541840c31b1a3eee54b1167a07795e
5
5
  SHA512:
6
- metadata.gz: c7d594c9deaabc166e4f9030c7f2efd13d9598d810b3abe784b1ed63a673a3fc9c7da18a2c60f75aa0e3b5a1ea4f8d70bedfa3464a361b4b887239d4933d7e71
7
- data.tar.gz: d0c5b207e0111a33ad38d64551561b0dc65f7330246924119185348479bfdbe5d6cc31cd64d7a8adf9a35a3658dbafbe99573c06b4fe73d93ba84dbb50cadf2f
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
@@ -1,6 +1,12 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
2
3
  require 'yard'
3
4
 
5
+ desc "Run specs"
6
+ task :default => :spec
7
+
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
4
10
  YARD::Rake::YardocTask.new do |t|
5
11
  t.files = ['lib/**/*.rb']
6
12
  end
@@ -5,3 +5,5 @@ require 'powerpack/hash'
5
5
  require 'powerpack/numeric'
6
6
 
7
7
  require 'powerpack/string'
8
+
9
+ require 'powerpack/array'
@@ -0,0 +1,2 @@
1
+ require_relative 'array/init'
2
+ require_relative 'array/tail'
@@ -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
@@ -7,7 +7,7 @@ unless Hash.method_defined? :symbolize_keys
7
7
  # @example
8
8
  # { 'one' => 1, 'two' => 2 }.symbolize_keys #=> { :one => 1, :two => 2 }
9
9
  def symbolize_keys
10
- Hash[map { |(k, v)| [k.to_sym, v] }]
10
+ Hash[map { |(k, v)| [(k.to_sym rescue k), v] }]
11
11
  end
12
12
  end
13
13
  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 traling whitespace and no
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
- strip.gsub(/\s+/, ' ')
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(margin_character)
17
- margin = '\\' + margin_character
16
+ def strip_margin(margin_characters)
17
+ margin = Regexp.quote(margin_characters)
18
18
  gsub(/^\s+#{margin}/, '')
19
19
  end
20
20
  end
@@ -1,3 +1,3 @@
1
1
  module Powerpack
2
- VERSION = '0.0.4'
2
+ VERSION = '0.0.5'
3
3
  end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Array#init' do
4
+ it 'returns an array without the last element' do
5
+ expect([1, 2, 3].init).to eq([1, 2])
6
+ end
7
+
8
+ it 'returns [] for empty lists' do
9
+ expect([].init).to be_empty
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Array#tail' do
4
+ it 'returns an array without the first element' do
5
+ expect([1, 2, 3].tail).to eq([2, 3])
6
+ end
7
+
8
+ it 'returns nil for empty lists' do
9
+ expect([].tail).to be_nil
10
+ end
11
+ end
@@ -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
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-21 00:00:00.000000000 Z
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