pax_global_header00006660000000000000000000000064125217126630014517gustar00rootroot0000000000000052 comment=bd5f588c1138a6ddd9ecbb1cf5bc3d5dc3234ca0 powerpack-0.1.1/000077500000000000000000000000001252171266300135115ustar00rootroot00000000000000powerpack-0.1.1/.gitignore000066400000000000000000000002321252171266300154760ustar00rootroot00000000000000*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp powerpack-0.1.1/.rspec000066400000000000000000000000071252171266300146230ustar00rootroot00000000000000--colorpowerpack-0.1.1/.travis.yml000066400000000000000000000001551252171266300156230ustar00rootroot00000000000000language: ruby rvm: - 1.9.3 - 2.0.0 - 2.1 - 2.2 - jruby-19mode - rbx-2 script: bundle exec rspec powerpack-0.1.1/CHANGELOG.md000066400000000000000000000014131252171266300153210ustar00rootroot00000000000000# Changelog ## master (unreleased) ## 0.1.1 (04/05/2015) No user-visible changes. ## 0.1.0 (25/01/2015) * Added `String#remove_prefix` * Added `String#remove_prefix!` * Added `String#remove_suffix` * Added `String#remove_suffix!` ## 0.0.9 (22/10/2013) * Added `String#remove` * Added `String#remove!` * Added `String#ascii_only` * Added `String#ascii_only!` ## 0.0.8 (13/09/2013) ### New features * Added `Enumerable#exactly?` * Added `Enumerable#average` * Added `Numeric#hundred` * Added `Numeric#thousand` * Added `Numeric#million` * Added `Numeric#billion` * Added `Numeric#trillion` * Added `Numeric#quadrillion` ### Changes * Renamed `Array#tail` to `Array#butfirst` * `Enumerable#take_last_while` returns the result in the same order as `Enumerable#take_last` powerpack-0.1.1/Gemfile000066400000000000000000000001361252171266300150040ustar00rootroot00000000000000source 'https://rubygems.org' # Specify your gem's dependencies in powerpack.gemspec gemspec powerpack-0.1.1/LICENSE.txt000066400000000000000000000020601252171266300153320ustar00rootroot00000000000000Copyright (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. powerpack-0.1.1/README.md000066400000000000000000000114101252171266300147650ustar00rootroot00000000000000[![Gem Version](https://badge.fury.io/rb/powerpack.png)](http://badge.fury.io/rb/powerpack) [![Build Status](https://travis-ci.org/bbatsov/powerpack.png?branch=master)](https://travis-ci.org/bbatsov/powerpack) # Powerpack Powerpack offers some useful extensions to the standard Ruby classes (kind of like `ActiveSupport`, but less ambitious and more modular). ## Installation Add this line to your application's `Gemfile` (if you're using `bundler`, that is): gem 'powerpack' And then execute: $ bundle Or install it yourself as: $ gem install powerpack ## Extensions * [Array](http://rdoc.info/github/bbatsov/powerpack/Array) * [#butfirst](http://rdoc.info/github/bbatsov/powerpack/Array#butfirst-instance_method) * [#butlast](http://rdoc.info/github/bbatsov/powerpack/Array#butlast-instance_method) * [Enumerable](http://rdoc.info/github/bbatsov/powerpack/Enumerable) * [#average](http://rdoc.info/github/bbatsov/powerpack/Enumerable#average-instance_method) * [#drop_last](http://rdoc.info/github/bbatsov/powerpack/Enumerable#drop_last-instance_method) * [#drop_last_while](http://rdoc.info/github/bbatsov/powerpack/Enumerable#drop_last_while-instance_method) * [#exactly?](http://rdoc.info/github/bbatsov/powerpack/Enumerable#exactly?-instance_method) * [#frequencies](http://rdoc.info/github/bbatsov/powerpack/Enumerable#frequencies-instance_method) * [#several?](http://rdoc.info/github/bbatsov/powerpack/Enumerable#several?-instance_method) * [#sum](http://rdoc.info/github/bbatsov/powerpack/Enumerable#sum-instance_method) * [#take_last](http://rdoc.info/github/bbatsov/powerpack/Enumerable#take_last-instance_method) * [#take_last_while](http://rdoc.info/github/bbatsov/powerpack/Enumerable#take_last_while-instance_method) * [Hash](http://rdoc.info/github/bbatsov/powerpack/Hash) * [#symbolize_keys](http://rdoc.info/github/bbatsov/powerpack/Hash#symbolize_keys-instance_method) * [Numeric](http://rdoc.info/github/bbatsov/powerpack/Numeric) * [#pos?](http://rdoc.info/github/bbatsov/powerpack/Numeric#pos?-instance_method) * [#neg?](http://rdoc.info/github/bbatsov/powerpack/Numeric#neg?-instance_method) * [#hundred](http://rdoc.info/github/bbatsov/powerpack/Numeric#hundred-instance_method) * [#thousand](http://rdoc.info/github/bbatsov/powerpack/Numeric#thousand-instance_method) * [#million](http://rdoc.info/github/bbatsov/powerpack/Numeric#million-instance_method) * [#billion](http://rdoc.info/github/bbatsov/powerpack/Numeric#billion-instance_method) * [#trillion](http://rdoc.info/github/bbatsov/powerpack/Numeric#trillion-instance_method) * [#quadrillion](http://rdoc.info/github/bbatsov/powerpack/Numeric#quadrillion-instance_method) * [String](http://rdoc.info/github/bbatsov/powerpack/String) * [#ascii_only](http://rdoc.info/github/bbatsov/powerpack/String#ascii_only-instance_method) * [#ascii_only!](http://rdoc.info/github/bbatsov/powerpack/String#ascii_only!-instance_method) * [#blank?](http://rdoc.info/github/bbatsov/powerpack/String#blank?-instance_method) * [#format](http://rdoc.info/github/bbatsov/powerpack/String#format-instance_method) * [#remove](http://rdoc.info/github/bbatsov/powerpack/String#remove-instance_method) * [#remove!](http://rdoc.info/github/bbatsov/powerpack/String#remove!-instance_method) * [#remove_prefix](http://rdoc.info/github/bbatsov/powerpack/String#remove_prefix-instance_method) * [#remove_prefix!](http://rdoc.info/github/bbatsov/powerpack/String#remove_prefix!-instance_method) * [#remove_suffix](http://rdoc.info/github/bbatsov/powerpack/String#remove_suffix-instance_method) * [#remove_suffix!](http://rdoc.info/github/bbatsov/powerpack/String#remove_suffix!-instance_method) * [#strip_indent](http://rdoc.info/github/bbatsov/powerpack/String#strip_indent-instance_method) * [#strip_margin](http://rdoc.info/github/bbatsov/powerpack/String#strip_margin-instance_method) * [#squish](http://rdoc.info/github/bbatsov/powerpack/String#squish-instance_method) * [#squish!](http://rdoc.info/github/bbatsov/powerpack/String#squish!-instance_method) ## Documentation A listing of the extensions provided by Powerpack is available [here](http://rdoc.info/github/bbatsov/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' ``` ## Changelog Powerpack's changelog is available [here](CHANGELOG.md). ## 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 powerpack-0.1.1/Rakefile000066400000000000000000000003321252171266300151540ustar00rootroot00000000000000require 'bundler/gem_tasks' require 'rspec/core/rake_task' require 'yard' desc "Run specs" task :default => :spec RSpec::Core::RakeTask.new(:spec) YARD::Rake::YardocTask.new do |t| t.files = ['lib/**/*.rb'] end powerpack-0.1.1/lib/000077500000000000000000000000001252171266300142575ustar00rootroot00000000000000powerpack-0.1.1/lib/powerpack.rb000066400000000000000000000002521252171266300165760ustar00rootroot00000000000000require 'powerpack/version' require 'powerpack/enumerable' require 'powerpack/hash' require 'powerpack/numeric' require 'powerpack/string' require 'powerpack/array' powerpack-0.1.1/lib/powerpack/000077500000000000000000000000001252171266300162525ustar00rootroot00000000000000powerpack-0.1.1/lib/powerpack/array.rb000066400000000000000000000001031252171266300177070ustar00rootroot00000000000000require_relative 'array/butlast' require_relative 'array/butfirst' powerpack-0.1.1/lib/powerpack/array/000077500000000000000000000000001252171266300173705ustar00rootroot00000000000000powerpack-0.1.1/lib/powerpack/array/butfirst.rb000066400000000000000000000005561252171266300215650ustar00rootroot00000000000000unless Array.method_defined? :butfirst class Array # Returns a new array rejecting the current one's first element. # # @return [Array] a new array without the first element or nil if this # array is empty # # @example # [1, 2, 3].butfirst #=> [2, 3] # [].butfirst #=> nil def butfirst self[1..-1] end end end powerpack-0.1.1/lib/powerpack/array/butlast.rb000066400000000000000000000006001252171266300213670ustar00rootroot00000000000000unless Array.method_defined? :butlast class Array # Returns a new array that has all the elements of the current but the last. # # @return [Array] a new array without the last element or an empty array if this # array is empty # # @example # [1, 2, 3].butlast #=> [1, 2] # [].butlast #=> [] def butlast self[0...-1] end end end powerpack-0.1.1/lib/powerpack/enumerable.rb000066400000000000000000000005521252171266300207200ustar00rootroot00000000000000require_relative 'enumerable/average' require_relative 'enumerable/drop_last' require_relative 'enumerable/drop_last_while' require_relative 'enumerable/exactly' require_relative 'enumerable/frequencies' require_relative 'enumerable/several' require_relative 'enumerable/sum' require_relative 'enumerable/take_last' require_relative 'enumerable/take_last_while' powerpack-0.1.1/lib/powerpack/enumerable/000077500000000000000000000000001252171266300203715ustar00rootroot00000000000000powerpack-0.1.1/lib/powerpack/enumerable/average.rb000066400000000000000000000011551252171266300223320ustar00rootroot00000000000000unless Enumerable.method_defined? :average module Enumerable # Calculates the average of a numeric collection. # # @param default [Object] an optional default return value if there are no elements. # It's nil by default. # @return The average of the elements or the default value if there are no # elements. # # @example # [1, 2, 3].average #=> 2 # [1, 2, 3, 4].average #=> 2.5 # [].average #=> nil # [].average(0) #=> 0 def average(default = nil) coll_size = to_a.size coll_size > 0 ? reduce(&:+) / coll_size.to_f : default end end end powerpack-0.1.1/lib/powerpack/enumerable/drop_last.rb000066400000000000000000000010121252171266300226770ustar00rootroot00000000000000unless Enumerable.method_defined? :drop_last module Enumerable # Drops the last n elements of an enumerable. # # @param n [Fixnum] the number of elements to drop # @return [Array] an array containing the remaining elements # # @example # [1, 2, 3].drop_last(1) #=> [1, 2] # [].drop_last(5) #=> [] def drop_last(n) fail ArgumentError, 'attempt to drop negative size' if n < 0 ary = to_a return [] if n > ary.size ary[0...(ary.size - n)] end end end powerpack-0.1.1/lib/powerpack/enumerable/drop_last_while.rb000066400000000000000000000010311252171266300240700ustar00rootroot00000000000000unless Enumerable.method_defined? :drop_last_while module Enumerable # Drops the last elements of an enumerable meeting a predicate. # # @return [Array] an array containing the remaining elements # # @example # [1, 2, 3].drop_last_while(&:odd?) #=> [1, 2] def drop_last_while return to_enum(:drop_last_while) unless block_given? result = [] dropping = true reverse_each do |obj| result.unshift(obj) unless dropping &&= yield(obj) end result end end end powerpack-0.1.1/lib/powerpack/enumerable/exactly.rb000066400000000000000000000022371252171266300223730ustar00rootroot00000000000000unless Enumerable.method_defined? :exactly? module Enumerable # Checks if exactly n elements meet a certain predicate. # # @param n [Fixnum] the number of matches required # @return [Boolean] true if we get exactly n matches, false otherwise # # @example # [1, 2, 3, 4].exactly?(1) { |n| n > 3 } #=> true # [1, 2, 3, 4].exactly?(2, &:even?) #=> true # [1, 1, 3, 3].exactly?(2, &:even?) #=> false # # Without a block uses the identify of the elements as default predicate. # This means that nil and false elements will be ignored. # # @example # [1, false, nil].exactly?(3) #=> false # [1, false, nil].exactly?(1) #=> true # [false, nil].exactly?(0) #=> true # [1, 2, 3].exactly?(3) #=>true def exactly?(n) found_count = 0 if block_given? each do |*o| if yield(*o) found_count += 1 return false if found_count > n end end else each do |o| if o found_count += 1 return false if found_count > n end end end n == found_count end end end powerpack-0.1.1/lib/powerpack/enumerable/frequencies.rb000066400000000000000000000007241252171266300232320ustar00rootroot00000000000000unless Enumerable.method_defined? :frequencies module Enumerable # Counts the number of occurrence of items in the enumerable. # # @return [Hash] in the format value => count # # @example # [].frequencies # => {} # [1, :symbol, 'string', 3, :symbol, 1].frequencies # #=> { 1 => 2, :symbol => 2, 'string' => 1, 3 => 1 } # # def frequencies each_with_object(Hash.new(0)) { |e, a| a[e] += 1 } end end end powerpack-0.1.1/lib/powerpack/enumerable/several.rb000066400000000000000000000015571252171266300223670ustar00rootroot00000000000000unless Enumerable.method_defined? :several? module Enumerable # Checks if two or more elements meet a certain predicate. # # @example # [1, 2, 3, 4].several?(&:even?) #=> true # [1, 1, 3, 3].several?(&:even?) #=> false # # Without a block uses the identify of the elements as default predicate. # This means that nil and false elements will be ignored. # # @example # [1, false, nil].several? #=> false # [1, 2, 3].several? #=>true def several? found_count = 0 if block_given? each do |*o| if yield(*o) found_count += 1 return true if found_count > 1 end end else each do |o| if o found_count += 1 return true if found_count > 1 end end end false end end end powerpack-0.1.1/lib/powerpack/enumerable/sum.rb000066400000000000000000000011751252171266300215260ustar00rootroot00000000000000unless Enumerable.method_defined? :sum module Enumerable # Sums up elements of a collection by invoking their `+` method. # Most useful for summing up numbers. # # @param default [Object] an optional default return value if there are no elements. # It's nil by default. # @return The sum of the elements or the default value if there are no # elements. # # @example # [1, 2, 3].sum #=> 6 # ["a", "b", "c"].sum #=> "abc" # [[1], [2], [3]].sum #=> [1, 2, 3] # [].sum #=> nil # [].sum(0) #=> 0 def sum(default = nil) reduce(&:+) || default end end end powerpack-0.1.1/lib/powerpack/enumerable/take_last.rb000066400000000000000000000010121252171266300226570ustar00rootroot00000000000000unless Enumerable.method_defined? :take_last module Enumerable # Take the last n elements of an enumerable. # # @param n [Fixnum] the number of elements to take # @return [Array] an array containing the requested elements # # @example # [1, 2, 3].take_last(2) #=> [2, 3] # [].take_last(5) #=> [] def take_last(n) fail ArgumentError, 'attempt to take negative size' if n < 0 ary = to_a return ary if n > ary.size ary[(ary.size - n)..-1] end end end powerpack-0.1.1/lib/powerpack/enumerable/take_last_while.rb000066400000000000000000000007671252171266300240670ustar00rootroot00000000000000unless Enumerable.method_defined? :take_last_while? module Enumerable # Take the last n elements of an enumerable meeting a certain predicate. # # @return [Array] an array containing the matching elements # # @example # [1, 2, 3, 5].take_last_while(&:odd?) #=> [3, 5] def take_last_while return to_enum(:take_last_while) unless block_given? result = [] reverse_each { |elem| yield(elem) ? result.unshift(elem) : break } result end end end powerpack-0.1.1/lib/powerpack/hash.rb000066400000000000000000000000471252171266300175230ustar00rootroot00000000000000require_relative 'hash/symbolize_keys' powerpack-0.1.1/lib/powerpack/hash/000077500000000000000000000000001252171266300171755ustar00rootroot00000000000000powerpack-0.1.1/lib/powerpack/hash/symbolize_keys.rb000066400000000000000000000007671252171266300226040ustar00rootroot00000000000000unless Hash.method_defined? :symbolize_keys class Hash # Converts the keys of the hash to symbols. # # @return [Hash] a copy of the original hash with its keys converted to symbols. Leave nonconvertible keys untouched when symbolizing keys. # # @example # { 'one' => 1, 'two' => 2 }.symbolize_keys #=> { :one => 1, :two => 2 } # { 1 => "a" }.symbolize_keys #=> { 1 => "a" } def symbolize_keys Hash[map { |(k, v)| [(k.to_sym rescue k), v] }] end end end powerpack-0.1.1/lib/powerpack/numeric.rb000066400000000000000000000001371252171266300202420ustar00rootroot00000000000000require_relative 'numeric/neg' require_relative 'numeric/pos' require_relative 'numeric/scale' powerpack-0.1.1/lib/powerpack/numeric/000077500000000000000000000000001252171266300177145ustar00rootroot00000000000000powerpack-0.1.1/lib/powerpack/numeric/neg.rb000066400000000000000000000005501252171266300210120ustar00rootroot00000000000000unless Numeric.method_defined? :neg? class Numeric # Checks whether a number is negative. # # @return [Boolean] true is the number is negative, false otherwise # # @example # 5.neg? #=> false # # @example # -0.5.neg? #=> true # # @example # 0.neg? #=> false def neg? self < 0 end end end powerpack-0.1.1/lib/powerpack/numeric/pos.rb000066400000000000000000000005501252171266300210420ustar00rootroot00000000000000unless Numeric.method_defined? :pos? class Numeric # Checks whether a number is positive. # # @return [Boolean] true is the number is positive, false otherwise # # @example # 5.pos? #=> true # # @example # -0.5.pos? #=> false # # @example # 0.pos? #=> false def pos? self > 0 end end end powerpack-0.1.1/lib/powerpack/numeric/scale.rb000066400000000000000000000013521252171266300213310ustar00rootroot00000000000000class Numeric HUNDRED = 10 ** 2 THOUSAND = 10 ** 3 MILLION = 10 ** 6 BILLION = 10 ** 9 TRILLION = 10 ** 12 QUADRILLION = 10 ** 15 unless Numeric.method_defined? :hundred def hundred self * HUNDRED end end unless Numeric.method_defined? :thousand def thousand self * THOUSAND end end unless Numeric.method_defined? :million def million self * MILLION end end unless Numeric.method_defined? :billion def billion self * BILLION end end unless Numeric.method_defined? :trillion def trillion self * TRILLION end end unless Numeric.method_defined? :quadrillion def quadrillion self * QUADRILLION end end end powerpack-0.1.1/lib/powerpack/string.rb000066400000000000000000000005061252171266300201060ustar00rootroot00000000000000require_relative 'string/ascii_only' require_relative 'string/blank' require_relative 'string/format' require_relative 'string/remove' require_relative 'string/remove_prefix' require_relative 'string/remove_suffix' require_relative 'string/strip_indent' require_relative 'string/strip_margin' require_relative 'string/squish' powerpack-0.1.1/lib/powerpack/string/000077500000000000000000000000001252171266300175605ustar00rootroot00000000000000powerpack-0.1.1/lib/powerpack/string/ascii_only.rb000066400000000000000000000024751252171266300222460ustar00rootroot00000000000000# encoding: utf-8 class String # Return a copy of string with ASCII characters only # Source: http://stackoverflow.com/questions/1268289/how-to-get-rid-of-non-ascii-characters-in-ruby # # @return [String] a copy of string with ASCII characters only # # @example # 'abc'.ascii_only #=> 'abc' # # @example # '中文123'.ascii_only #=> '123' unless String.method_defined? :ascii_only def ascii_only dup.ascii_only! end end # Modify self and keep ASCII characters only # Returns the string even if no changes were made. # Source: http://stackoverflow.com/questions/1268289/how-to-get-rid-of-non-ascii-characters-in-ruby # # @return [String] The result string # # @example # 'abc'.ascii_only! #=> 'abc' # # @example # '中文123'.ascii_only! #=> '123' unless String.method_defined? :ascii_only! def ascii_only! encoding_options = { :invalid => :replace, # Replace invalid byte sequences :undef => :replace, # Replace anything not defined in ASCII :replace => '', # Use a blank for those replacements :UNIVERSAL_NEWLINE_DECORATOR => true # Always break lines with \n } self.encode! Encoding.find('ASCII'), encoding_options end end end powerpack-0.1.1/lib/powerpack/string/blank.rb000066400000000000000000000007421252171266300211770ustar00rootroot00000000000000unless String.method_defined? :blank? class String # Checks whether a string is blank. A string is considered blank if it # is either empty or contains only whitespace characters. # # @return [Boolean] true is the string is blank, false otherwise # # @example # ''.blank? #=> true # # @example # ' '.blank? #=> true # # @example # ' test'.blank? #=> false def blank? empty? || strip.empty? end end end powerpack-0.1.1/lib/powerpack/string/format.rb000066400000000000000000000010151252171266300213720ustar00rootroot00000000000000unless String.method_defined? :format class String # A nicer alternative to Kernel#sprintf and String#%. # # @return [String] the formatted string # # @example # 'This is %s!'.format('Sparta') #=> 'This is Sparta!' # # @example # 'My name is %{fname} %{lname}.'.format(fname: 'Bruce', lname: 'Wayne') # #=> 'My name is Bruce Wayne.' # # @example # '%d + %d'.format([1, 2]) #=> '1 + 2' def format(*args) super(self, *(args.flatten(1))) end end end powerpack-0.1.1/lib/powerpack/string/remove.rb000066400000000000000000000007071252171266300214060ustar00rootroot00000000000000unless String.method_defined? :remove class String # Removes all occurrences of a pattern in a string. # # @return [String] a new string without any occurrences of the pattern. def remove(pattern) dup.remove!(pattern) end # Removes all occurrences of a pattern in a string. # # @return [String] the string without any occurrences of the pattern. def remove!(pattern) gsub!(pattern, '') end end end powerpack-0.1.1/lib/powerpack/string/remove_prefix.rb000066400000000000000000000010751252171266300227620ustar00rootroot00000000000000unless String.method_defined? :remove_prefix class String # Removes a prefix in a string. # # @return [String] a new string without the prefix. # # @example # 'Ladies Night'.remove_prefix('Ladies ') #=> 'Night' def remove_prefix(pattern) dup.remove_prefix!(pattern) end # Removes a prefix in a string. # # @return [String] the string without the prefix. # # @example # 'Ladies Night'.remove_prefix!('Ladies ') #=> 'Night' def remove_prefix!(pattern) gsub!(/\A#{pattern}/, '') end end end powerpack-0.1.1/lib/powerpack/string/remove_suffix.rb000066400000000000000000000010751252171266300227710ustar00rootroot00000000000000unless String.method_defined? :remove_suffix class String # Removes a suffix in a string. # # @return [String] a new string without the suffix. # # @example # 'Ladies Night'.remove_suffix(' Night') #=> 'Ladies' def remove_suffix(pattern) dup.remove_suffix!(pattern) end # Removes a suffix in a string. # # @return [String] the string without the suffix. # # @example # 'Ladies Night'.remove_suffix!(' Night') #=> 'Ladies' def remove_suffix!(pattern) gsub!(/#{pattern}\z/, '') end end end powerpack-0.1.1/lib/powerpack/string/squish.rb000066400000000000000000000013711252171266300214230ustar00rootroot00000000000000unless String.method_defined? :squish class String # Strips leading and trailing whitespace and squashes internal whitespace. # # @return [String] a new string with no leading and trailing # whitespace and no consecutive whitespace characters inside it # # @example # ' Peter Parker'.squish #=> 'Peter Parker' def squish dup.squish! end # Strips leading and trailing whitespace and squashes internal whitespace. # # @return [String] the string with no leading and trailing whitespace and no # consecutive whitespace characters inside it # # @example # ' Peter Parker'.squish #=> 'Peter Parker' def squish! strip! gsub!(/\s+/, ' ') self end end end powerpack-0.1.1/lib/powerpack/string/strip_indent.rb000066400000000000000000000011051252171266300226040ustar00rootroot00000000000000unless 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 powerpack-0.1.1/lib/powerpack/string/strip_margin.rb000066400000000000000000000010541252171266300226030ustar00rootroot00000000000000unless 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_characters) margin = Regexp.quote(margin_characters) gsub(/^\s+#{margin}/, '') end end end powerpack-0.1.1/lib/powerpack/version.rb000066400000000000000000000000511252171266300202600ustar00rootroot00000000000000module Powerpack VERSION = '0.1.1' end powerpack-0.1.1/powerpack.gemspec000066400000000000000000000015701252171266300170540ustar00rootroot00000000000000# 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') spec.add_development_dependency('yard', '~> 0.8') end powerpack-0.1.1/spec/000077500000000000000000000000001252171266300144435ustar00rootroot00000000000000powerpack-0.1.1/spec/powerpack/000077500000000000000000000000001252171266300164365ustar00rootroot00000000000000powerpack-0.1.1/spec/powerpack/array/000077500000000000000000000000001252171266300175545ustar00rootroot00000000000000powerpack-0.1.1/spec/powerpack/array/butfirst_spec.rb000066400000000000000000000003571252171266300227620ustar00rootroot00000000000000require 'spec_helper' describe 'Array#butfirst' do it 'returns an array without the first element' do expect([1, 2, 3].butfirst).to eq([2, 3]) end it 'returns nil for empty lists' do expect([].butfirst).to be_nil end end powerpack-0.1.1/spec/powerpack/array/butlast_spec.rb000066400000000000000000000003541252171266300225730ustar00rootroot00000000000000require 'spec_helper' describe 'Array#butlast' do it 'returns an array without the last element' do expect([1, 2, 3].butlast).to eq([1, 2]) end it 'returns [] for empty lists' do expect([].butlast).to be_empty end end powerpack-0.1.1/spec/powerpack/enumerable/000077500000000000000000000000001252171266300205555ustar00rootroot00000000000000powerpack-0.1.1/spec/powerpack/enumerable/average_spec.rb000066400000000000000000000007041252171266300235270ustar00rootroot00000000000000require 'spec_helper' describe 'Enumerable#average' do it 'calculates the average of an enum' do expect((1..3).average).to eq(2) end it 'calculates the average of an array' do expect([1, 2, 3, 4].average).to eq(2.5) end it 'returns nil when invoked on an empty collection' do expect([].average).to be_nil end it 'returns default value when invoked on an empty collection' do expect([].average(0)).to be_zero end end powerpack-0.1.1/spec/powerpack/enumerable/drop_last_spec.rb000066400000000000000000000006221252171266300241030ustar00rootroot00000000000000require 'spec_helper' describe 'Enumerable#drop_last' do it 'drops the last n elements' do expect([1, 2, 3, 4].drop_last(2)).to eq([1, 2]) end it 'returns an empty array if you request to drop too many elems' do expect((1..10).drop_last(100)).to eq([]) end it 'does not accept negative argument' do expect { [1, 2, 3, 4].drop_last(-1) }.to raise_error(ArgumentError) end end powerpack-0.1.1/spec/powerpack/enumerable/drop_last_while_spec.rb000066400000000000000000000003071252171266300252730ustar00rootroot00000000000000require 'spec_helper' describe 'Enumerable#drop_last_while' do it 'drops the last elements while a pred is true' do expect([1, 2, 3, 4, 6].drop_last_while(&:even?)).to eq([1, 2, 3]) end end powerpack-0.1.1/spec/powerpack/enumerable/exactly_spec.rb000066400000000000000000000023461252171266300235720ustar00rootroot00000000000000require 'spec_helper' describe 'Enumerable#exactly' do context 'with block' do it 'returns true for exact number of matches' do expect([1, 2, 3, 4].exactly?(2, &:even?)).to be_truthy end it 'returns false for less matches' do expect([1, 3, 4].exactly?(2, &:even?)).to be_falsey end it 'returns false for more matches' do expect([1, 3, 4, 6, 8].exactly?(2, &:even?)).to be_falsey end end context 'without block' do it 'returns true for exact number of non nil/false elements in absence of nil/false elements' do expect([1, 2, 3, 4].exactly?(4)).to be_truthy end it 'returns true for exact number of non nil/false elements in presence of nil/false elements' do expect([1, 2, nil, false].exactly?(2)).to be_truthy end it 'returns true for exact number of nil/false elements' do expect([nil, false].exactly?(0)).to be_truthy end it 'returns false if there are less non nil/false elements in absence of nil/false elements' do expect([1, 2, 3].exactly?(4)).to be_falsey end it 'returns false if there are less non nil/false elements in presence of nil/false elements' do expect([1, nil, false].exactly?(4)).to be_falsey end end end powerpack-0.1.1/spec/powerpack/enumerable/frequencies_spec.rb000066400000000000000000000007701252171266300244310ustar00rootroot00000000000000require 'spec_helper' describe 'Enumerable#frequencies' do context 'empty collection' do it 'evaluates to an empty hash' do expect([].frequencies).to eql({}) end end context 'populated collection' do it 'counts the number of ocurrences and returns a hash in the form value => count' do expect([1, 1, :symbol, 3, 1, :symbol, 'string'].frequencies).to eql( 1 => 3, 3 => 1, 'string' => 1, :symbol => 2 ) end end end powerpack-0.1.1/spec/powerpack/enumerable/several_spec.rb000066400000000000000000000014161252171266300235570ustar00rootroot00000000000000require 'spec_helper' describe 'Enumerable#several' do context 'with block' do it 'returns true if more than 1 element matches the predicate' do expect([1, 2, 3, 4].several?(&:even?)).to be_truthy end it 'returns false if just 1 element matches the predicate' do expect([1, 3, 4].several?(&:even?)).to be_falsey end it 'returns false if no elements match the predicate' do expect([1, 3, 4].several?(&:even?)).to be_falsey end end context 'without block' do it 'returns true if there are 2 or more non nil/false elements' do expect([1, 2, 3, 4].several?).to be_truthy end it 'returns false if there are less than 2 non nil/false elements' do expect([1, nil, false].several?).to be_falsey end end end powerpack-0.1.1/spec/powerpack/enumerable/sum_spec.rb000066400000000000000000000005211252171266300227160ustar00rootroot00000000000000require 'spec_helper' describe 'Enumerable#sum' do it 'sums up the numbers of an enum' do expect((1..3).sum).to eq(6) end it 'returns nil when invoked on an empty collection' do expect([].sum).to be_nil end it 'returns default value when invoked on an empty collection' do expect([].sum(0)).to be_zero end end powerpack-0.1.1/spec/powerpack/enumerable/take_last_spec.rb000066400000000000000000000006301252171266300240620ustar00rootroot00000000000000require 'spec_helper' describe 'Enumerable#take_last' do it 'takes the last n elements' do expect([1, 2, 3, 4].take_last(2)).to eq([3, 4]) end it 'returns everything if you request to drop too many elems' do expect((1..10).take_last(100)).to eq((1..10).to_a) end it 'does not accept negative argument' do expect { [1, 2, 3, 4].take_last(-1) }.to raise_error(ArgumentError) end end powerpack-0.1.1/spec/powerpack/enumerable/take_last_while_spec.rb000066400000000000000000000003041252171266300252500ustar00rootroot00000000000000require 'spec_helper' describe 'Enumerable#take_last_while' do it 'takes the last elements while a pred is true' do expect([1, 2, 3, 4, 6].take_last_while(&:even?)).to eq([4, 6]) end end powerpack-0.1.1/spec/powerpack/hash/000077500000000000000000000000001252171266300173615ustar00rootroot00000000000000powerpack-0.1.1/spec/powerpack/hash/symbolize_keys_spec.rb000066400000000000000000000005711252171266300237730ustar00rootroot00000000000000require '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 it 'leaves nonconvertible keys untouched' do hash = { 1 => 'one', [1, 1] => 'ones' } expect(hash.symbolize_keys).to eq({ 1 => 'one', [1, 1] => 'ones' }) end end powerpack-0.1.1/spec/powerpack/numeric/000077500000000000000000000000001252171266300201005ustar00rootroot00000000000000powerpack-0.1.1/spec/powerpack/numeric/neg_spec.rb000066400000000000000000000007551252171266300222170ustar00rootroot00000000000000require 'spec_helper' describe 'Numeric#neg?' do it 'returns false for positive integer' do expect(1.neg?).to be_falsey end it 'returns false for positive float' do expect(0.1.neg?).to be_falsey end it 'returns true for negative integer' do expect(-1.neg?).to be_truthy end it 'returns true for negative float' do expect(-0.01.neg?).to be_truthy end it 'returns false for 0' do expect(0.neg?).to be_falsey expect(0.0.neg?).to be_falsey end end powerpack-0.1.1/spec/powerpack/numeric/pos_spec.rb000066400000000000000000000007551252171266300222470ustar00rootroot00000000000000require 'spec_helper' describe 'Numeric#pos?' do it 'returns true for positive integer' do expect(1.pos?).to be_truthy end it 'returns true for positive float' do expect(0.1.pos?).to be_truthy end it 'returns false for negative integer' do expect(-1.pos?).to be_falsey end it 'returns false for negative float' do expect(-0.01.pos?).to be_falsey end it 'returns false for 0' do expect(0.pos?).to be_falsey expect(0.0.pos?).to be_falsey end end powerpack-0.1.1/spec/powerpack/numeric/scale_spec.rb000066400000000000000000000061761252171266300225400ustar00rootroot00000000000000require 'spec_helper' describe 'Numeric#hundred' do it 'returns positive integer for positive integer' do expect(1.hundred).to eql 100 end it 'returns positive float for positive float' do expect(0.1.hundred).to eql 10.0 end it 'returns false for negative integer' do expect(-1.hundred).to eql -100 end it 'returns false for negative float' do expect(-0.1.hundred).to eql -10.0 end it 'returns 0 for 0' do expect(0.hundred).to eql 0 expect(0.0.hundred).to eql 0.0 end end describe 'Numeric#thousand' do it 'returns positive integer for positive integer' do expect(1.thousand).to eql 1000 end it 'returns positive float for positive float' do expect(0.1.thousand).to eql 100.0 end it 'returns false for negative integer' do expect(-1.thousand).to eql -1000 end it 'returns false for negative float' do expect(-0.1.thousand).to eql -100.0 end it 'returns 0 for 0' do expect(0.thousand).to eql 0 expect(0.0.thousand).to eql 0.0 end end describe 'Numeric#million' do it 'returns positive integer for positive integer' do expect(1.million).to eql 1000000 end it 'returns positive float for positive float' do expect(0.1.million).to eql 100000.0 end it 'returns false for negative integer' do expect(-1.million).to eql -1000000 end it 'returns false for negative float' do expect(-0.1.million).to eql -100000.0 end it 'returns 0 for 0' do expect(0.million).to eql 0 expect(0.0.million).to eql 0.0 end end describe 'Numeric#billion' do it 'returns positive integer for positive integer' do expect(1.billion).to eql 1000000000 end it 'returns positive float for positive float' do expect(0.1.billion).to eql 100000000.0 end it 'returns false for negative integer' do expect(-1.billion).to eql -1000000000 end it 'returns false for negative float' do expect(-0.1.billion).to eql -100000000.0 end it 'returns 0 for 0' do expect(0.billion).to eql 0 expect(0.0.billion).to eql 0.0 end end describe 'Numeric#trillion' do it 'returns positive integer for positive integer' do expect(1.trillion).to eql 1000000000000 end it 'returns positive float for positive float' do expect(0.1.trillion).to eql 100000000000.0 end it 'returns false for negative integer' do expect(-1.trillion).to eql -1000000000000 end it 'returns false for negative float' do expect(-0.1.trillion).to eql -100000000000.0 end it 'returns 0 for 0' do expect(0.trillion).to eql 0 expect(0.0.trillion).to eql 0.0 end end describe 'Numeric#quadrillion' do it 'returns positive integer for positive integer' do expect(1.quadrillion).to eql 1000000000000000 end it 'returns positive float for positive float' do expect(0.1.quadrillion).to eql 100000000000000.0 end it 'returns false for negative integer' do expect(-1.quadrillion).to eql -1000000000000000 end it 'returns false for negative float' do expect(-0.1.quadrillion).to eql -100000000000000.0 end it 'returns 0 for 0' do expect(0.quadrillion).to eql 0 expect(0.0.quadrillion).to eql 0.0 end end powerpack-0.1.1/spec/powerpack/string/000077500000000000000000000000001252171266300177445ustar00rootroot00000000000000powerpack-0.1.1/spec/powerpack/string/ascii_only_spec.rb000066400000000000000000000025041252171266300234350ustar00rootroot00000000000000# encoding: utf-8 require 'spec_helper' unless defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby' describe 'String#ascii_only' do it 'returns same value for string with ASCII chars only' do expect('abc'.ascii_only).to eq 'abc' end it 'returns string without non-ASCII chars' do expect("abc\u{6666}".force_encoding("UTF-8").ascii_only).to eq 'abc' end it 'returns string without non-ASCII chars and with ASCII chars' do expect("\u{6666}".force_encoding("UTF-8").ascii_only).to eq '' end it 'does not change the original string' do string = "abc\u{6666}".force_encoding("UTF-8") string.ascii_only expect(string).to eq "abc\u{6666}".force_encoding("UTF-8") end end describe 'String#ascii_only!' do it 'returns same value for string with ASCII chars only' do expect('abc'.ascii_only!).to eq 'abc' end it 'returns string without non-ASCII chars' do expect("abc\u{6666}".force_encoding("UTF-8").ascii_only!).to eq 'abc' end it 'returns string without non-ASCII chars and with ASCII chars' do expect("\u{6666}".force_encoding("UTF-8").ascii_only!).to eq '' end it 'changes the original string' do string = "abc\u{6666}".force_encoding("UTF-8") string.ascii_only! expect(string).to eq 'abc' end end end powerpack-0.1.1/spec/powerpack/string/blank_spec.rb000066400000000000000000000005571252171266300224010ustar00rootroot00000000000000require 'spec_helper' describe 'String#blank?' do it 'returns true for an empty string' do expect(''.blank?).to be_truthy end it 'returns true for a string with only whitespace in it' do expect(' '.blank?).to be_truthy end it 'returns false for a string with non-whitespace chars in it' do expect(' test'.blank?).to be_falsey end end powerpack-0.1.1/spec/powerpack/string/format_spec.rb000066400000000000000000000004561252171266300226000ustar00rootroot00000000000000require '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 powerpack-0.1.1/spec/powerpack/string/remove_prefix_spec.rb000066400000000000000000000005101252171266300241510ustar00rootroot00000000000000require 'spec_helper' describe 'String#remove_prefix' do it 'removes a prefix in a string' do expect('Ladies Night'.remove_prefix('Ladies ')).to eq('Night') end end describe 'String#remove_prefix!' do it 'removes a prefix in a string' do expect('Ladies Night'.remove_prefix!('Ladies ')).to eq('Night') end end powerpack-0.1.1/spec/powerpack/string/remove_spec.rb000066400000000000000000000004731252171266300226040ustar00rootroot00000000000000require 'spec_helper' describe 'String#remove' do it 'removes all occurrences of a pattern' do expect('Ladies Night'.remove(/Ladies /)).to eq('Night') end end describe 'String#remove!' do it 'removes all occurrences of a pattern' do expect('Ladies Night'.remove(/Ladies /)).to eq('Night') end end powerpack-0.1.1/spec/powerpack/string/remove_suffix_spec.rb000066400000000000000000000005101252171266300241600ustar00rootroot00000000000000require 'spec_helper' describe 'String#remove_suffix' do it 'removes a suffix in a string' do expect('Ladies Night'.remove_suffix(' Night')).to eq('Ladies') end end describe 'String#remove_suffix!' do it 'removes a suffix in a string' do expect('Ladies Night'.remove_suffix!(' Night')).to eq('Ladies') end end powerpack-0.1.1/spec/powerpack/string/squish_spec.rb000066400000000000000000000010031252171266300226110ustar00rootroot00000000000000require 'spec_helper' describe 'String#squish' do it 'strips leading and trailing whitespace' do expect(' Peter '.squish).to eq('Peter') end it 'compacts internal whitespace' do expect("Peter\r\n \t Parker".squish).to eq('Peter Parker') end end describe 'String#squish!' do it 'strips leading and trailing whitespace' do expect(' Peter '.squish!).to eq('Peter') end it 'compacts internal whitespace' do expect("Peter\r\n \t Parker".squish!).to eq('Peter Parker') end end powerpack-0.1.1/spec/powerpack/string/strip_indent_spec.rb000066400000000000000000000003611252171266300240050ustar00rootroot00000000000000require '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 powerpack-0.1.1/spec/powerpack/string/strip_margin_spec.rb000066400000000000000000000010451252171266300240010ustar00rootroot00000000000000require '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 it 'strips special characters margin from every line' do code = <<-END {{1}}def test {{1}} some_method {{1}}end END expect(code.strip_margin('{{1}}')) .to eq("def test\n some_method\nend\n") end end powerpack-0.1.1/spec/spec_helper.rb000066400000000000000000000006731252171266300172670ustar00rootroot00000000000000$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'rspec' require 'powerpack' RSpec.configure do |config| 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 }