Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 65f3a9412e198262d900ec35e21c988cf0e534a2 @bbatsov committed Jul 12, 2013
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
1 .rspec
@@ -0,0 +1 @@
+--color
@@ -0,0 +1,7 @@
+language: ruby
+rvm:
+ - 1.9.3
+ - 2.0.0
+ - jruby-19mode
+ - rbx-19mode
+script: bundle exec rspec
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in powerpack.gemspec
+gemspec
@@ -0,0 +1,22 @@
+Copyright (c) 2013 Bozhidar Batsov
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
+# Powerpack
+
+Powerpack offers some useful extensions to the standard Ruby classes (kind of like `ActiveSupport`).
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'powerpack'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install powerpack
+
+## Usage
+
+To load the entire `powerpack` do:
+
+```
+require 'powerpack'
+```
+
+To load only the `String` extensions do:
+
+```
+require 'powerpack/string'
+```
+
+To load only a specific extension like `String#format` do:
+
+```
+require 'powerpack/string/format'
+```
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Add some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
+require 'powerpack/version'
+
+require 'powerpack/hash'
+
+require 'powerpack/string'
@@ -0,0 +1 @@
+require_relative 'hash/symbolize_keys'
@@ -0,0 +1,9 @@
+unless Hash.method_defined? :symbolize_keys
+ class Hash
+ # The method behaves like turns the keys of the hash to symbols.
+ # It returns a copy of the original hash.
+ def symbolize_keys
+ Hash[map { |(k, v)| [k.to_sym, v] }]
+ end
+ end
+end
@@ -0,0 +1,3 @@
+require_relative 'string/format'
+require_relative 'string/strip_indent'
+require_relative 'string/strip_margin'
@@ -0,0 +1,8 @@
+unless String.method_defined? :format
+ class String
+ # The method behaves like `Kernel#sprintf` and `String#%`.
+ def format(*args)
+ super(self, *(args.flatten(1)))
+ end
+ end
+end
@@ -0,0 +1,22 @@
+unless String.method_defined? :strip_indent
+ class String
+ # The method strips the whitespace preceding the base indentation.
+ # Useful for HEREDOCs and other multi-line strings.
+ #
+ # @example
+ #
+ # code = <<-END.strip_indent
+ # def test
+ # some_method
+ # other_method
+ # end
+ # END
+ #
+ # => "def\n some_method\n \nother_method\nend"
+ def strip_indent
+ leading_space = scan(/^[ \t]*(?=\S)/).min
+ indent = leading_space ? leading_space.size : 0
+ gsub(/^[ \t]{#{indent}}/, '')
+ end
+ end
+end
@@ -0,0 +1,21 @@
+unless String.method_defined? :strip_margin
+ class String
+ # The method strips the characters preceding a special margin character.
+ # Useful for HEREDOCs and other multi-line strings.
+ #
+ # @example
+ #
+ # code = <<-END.strip_margin('|')
+ # |def test
+ # | some_method
+ # | other_method
+ # |end
+ # END
+ #
+ # => "def\n some_method\n \nother_method\nend"
+ def strip_margin(margin_character)
+ margin = '\\' + margin_character
+ gsub(/^\s+#{margin}/, '')
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module Powerpack
+ VERSION = '0.0.1'
+end
@@ -0,0 +1,24 @@
+# coding: utf-8
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'powerpack/version'
+
+Gem::Specification.new do |spec|
+ spec.name = 'powerpack'
+ spec.version = Powerpack::VERSION
+ spec.authors = ['Bozhidar Batsov']
+ spec.email = ['bozhidar@batsov.com']
+ spec.description = 'A few useful extensions to core Ruby classes.'
+ spec.summary = spec.description
+ spec.homepage = 'https://github.com/bbatsov/powerpack'
+ spec.license = 'MIT'
+
+ spec.files = `git ls-files`.split($/)
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
+ spec.require_paths = ['lib']
+
+ spec.add_development_dependency 'bundler', '~> 1.3'
+ spec.add_development_dependency 'rake'
+ spec.add_development_dependency('rspec', '~> 2.13')
+ spec.add_development_dependency('yard', '~> 0.8')
+end
@@ -0,0 +1,9 @@
+require 'spec_helper'
+
+describe 'Hash#symbolize_keys' do
+ it 'turn the hash keys into symbols' do
+ hash = { 'one' => 1, 'two' => 2 }
+
+ expect(hash.symbolize_keys).to eq({ one: 1, two: 2 })
+ end
+end
@@ -0,0 +1,12 @@
+require 'spec_helper'
+
+describe 'String#format' do
+ it 'behaves like String#%' do
+ expect('%s %s'.format %w(James Bond)).to eq('%s %s' % %w(James Bond))
+ end
+
+ it 'behaves like Kernel#sprintf' do
+ expect('%s %s'.format %w(James Bond))
+ .to eq(sprintf('%s %s', 'James', 'Bond'))
+ end
+end
@@ -0,0 +1,13 @@
+require 'spec_helper'
+
+describe 'String#strip_indent' do
+ it 'strips leading indent on every line of string' do
+ test = <<-END
+ test
+ test
+ test
+ END
+
+ expect(test.strip_indent).to eq("test\n test\n test\n")
+ end
+end
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe 'String#strip_margin' do
+ it 'strips margin on every line of string' do
+ code = <<-END
+ |def test
+ | some_method
+ | other_method
+ |end
+ END
+
+ expect(code.strip_margin('|'))
+ .to eq("def test\n some_method\n other_method\nend\n")
+ end
+end
@@ -0,0 +1,17 @@
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+
+require 'rspec'
+require 'powerpack'
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+
+ config.expect_with :rspec do |c|
+ c.syntax = :expect # disables `should`
+ end
+end
+
+# Requires supporting files with custom matchers and macros, etc,
+# in ./support/ and its subdirectories.
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

0 comments on commit 65f3a94

Please sign in to comment.