core_refinements 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2d59d91107058453403d3b53369385195b07f50
4
+ data.tar.gz: 07b1f6f9b5e175eb9284d0daa2f3bef9366638de
5
+ SHA512:
6
+ metadata.gz: 4d2998316ccb4f5b27f4b32fa9fd39613821a7dea6865951f5a64c7febf2cf918f54edf721658c221b36c75fb0d4b42ea333844b7ae5120edf03f4763e696305
7
+ data.tar.gz: 0dfff658f84de2c4553a487d8030f94f4b98d99aee811000278092a70ec10436097bbd71afe4c650dad70357005ce30c349c0a557e1a02e44c7ada06288d45a7
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ .idea
2
+
3
+ *.gem
4
+ *.rbc
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ *.bundle
21
+ *.so
22
+ *.o
23
+ *.a
24
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - "2.0.0"
4
+ - "2.1.0"
5
+ branches:
6
+ only:
7
+ - master
8
+ - development
9
+ script: CODECLIMATE_REPO_TOKEN=69c1e6e298325a41b32512316f03a1e83a36a5707073cfdbe174d4362d7a237c bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in core_augmentations.gemspec
4
+ gemspec
5
+ group :development do
6
+ gem 'rspec'
7
+ gem 'guard', require:false
8
+ gem 'guard-rspec', require:false
9
+ gem 'guard-bundler', require:false
10
+ gem 'pry', require:false
11
+ gem "codeclimate-test-reporter", group: :test, require: nil
12
+ end
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard :bundler do
2
+ watch('Gemfile')
3
+ watch(/^.+\.gemspec/)
4
+ end
5
+
6
+ guard :rspec, cmd: 'rspec' do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ernie Brodeur
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # CoreAugmentations
2
+
3
+ **Master** [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/erniebrodeur/core_refinements) [![Build Status](https://travis-ci.org/erniebrodeur/core_refinements.png?branch=master)](https://travis-ci.org/erniebrodeur/core_refinements) [![Dependency Status](https://gemnasium.com/erniebrodeur/core_refinements.png)](https://gemnasium.com/erniebrodeur/core_refinements)
4
+
5
+ **Development** [![Build Status](https://travis-ci.org/erniebrodeur/core_refinements.png?branch=development)](https://travis-ci.org/erniebrodeur/core_refinements)
6
+
7
+
8
+ A collection of (somewhat) simple additions to the base ruby classes.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'core_refinements'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install core_refinements
23
+
24
+ ## Usage
25
+
26
+ Each method is contained in it's own file. This allows us to extend only the methods we need as we need them.
27
+
28
+ `require 'core_refinements/String/to_human'`
29
+
30
+ All the methods have spec's, they don't have any documentation. For now just browse the source directory to see
31
+ what is available.
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/core_refinements/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ begin
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ rescue LoadError
6
+ end
data/WHATSNEW.md ADDED
@@ -0,0 +1,18 @@
1
+ ## (unreleased)
2
+
3
+ ## 0.1.5
4
+ * renamed and converted to refinements instead of monkey patching.
5
+
6
+ ## 0.1.4
7
+ * Updated Array#collapse_ranges to take a 'step' and a 'split' parameter (step size and a pre-split array).
8
+
9
+ ## 0.1.3
10
+ * Added Object#add_option, a 'dsl' like method to add named options to an object.
11
+ * Added Array#collapse_ranges, collapse a large array of numeric's into an array with optimal ranging.
12
+
13
+ ## 0.1.2
14
+ * Added code climate coverage.
15
+
16
+ ## 0.1.1
17
+ * Added travis-ci. gemnasium, and code climate. All green (it's 12 lines of code so far).
18
+ * Spec'ed everything I've included so far.
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'core_refinements/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "core_refinements"
8
+ spec.version = CoreRefinements::VERSION
9
+ spec.authors = ["Ernie Brodeur"]
10
+ spec.email = ["ebrodeur@ujami.net"]
11
+ spec.summary = "Methods to augment the core classes in ruby."
12
+ spec.description = "A collection of methods that can be used to extend the basic ruby classes."
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,3 @@
1
+ module CoreAugmentations
2
+ VERSION = "0.1.4"
3
+ end
File without changes
@@ -0,0 +1,47 @@
1
+ module CoreRefinements
2
+ module Array
3
+ module CollapseRanges
4
+ refine ::Array do
5
+ # collapse a given array into an array with ranges (and singular) entries.
6
+ def collapse_ranges(split: false, step: 1)
7
+ ungapped_array = []
8
+ i = 0
9
+ contiguous = false
10
+ contiguous_start = nil
11
+ contiguous_end = nil
12
+ while (i < self.count)
13
+ current_n = self[i]
14
+ return nil unless current_n.kind_of? ::Numeric
15
+
16
+ next_n = self[i+1]
17
+ if next_n && current_n != next_n && ::Range.new(current_n, current_n + step).include?(next_n)
18
+ # in a sequence and we know this is a part of the range since we have a start marker
19
+ contiguous_start = i unless contiguous_start
20
+ else
21
+ if contiguous_start
22
+ # mark our end
23
+ contiguous_end = i
24
+ # turn it into a range
25
+ range = self[contiguous_start]..self[contiguous_end]
26
+ ungapped_array.push(range)
27
+ # clean everything up for the next range
28
+ contiguous_start = nil
29
+ contiguous_end = nil
30
+ else
31
+ # normal number, just add to the output array
32
+ ungapped_array.push current_n
33
+ end
34
+ end
35
+ i += 1
36
+ end
37
+
38
+ if split && ungapped_array.any?
39
+ return [ungapped_array.select {|i| i.class == ::Range},
40
+ ungapped_array.select {|i| i.kind_of? ::Numeric}]
41
+ end
42
+ return ungapped_array
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,20 @@
1
+ module CoreRefinements
2
+ module Array
3
+ module IndexBy
4
+ refine ::Array do
5
+ def index_by(position)
6
+ index = {}
7
+ self.each do |entry|
8
+ if entry.kind_of?(::Array) || entry.kind_of?(::Hash)
9
+ return nil unless entry[position]
10
+ return nil if index[entry[position]]
11
+ index[entry[position]] = entry
12
+ end
13
+ end
14
+
15
+ return index
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ module CoreRefinements
2
+ module Numeric
3
+ module PercentageOf
4
+ class ::Numeric
5
+ # return the percentage of a number, rounded to 2 places by default.
6
+ def percentage_of(n, round = 2)
7
+ return (self.to_f / n.to_f * 100.0).round(round)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ module CoreRefinements
2
+ module Object
3
+ module Options
4
+ refine ::Object do
5
+ # Add a trackable option to an object. Creates a method for the key name.
6
+ def add_option(key = nil)
7
+ @_added_options ||= []
8
+ @_added_options.push(key.to_sym) unless @_added_options.include?(key.to_sym)
9
+ define_singleton_method(key) do |args = nil|
10
+ if !args
11
+ instance_variable_get("@#{key}")
12
+ else
13
+ instance_variable_set("@#{key}", args)
14
+ end
15
+ end
16
+ end
17
+
18
+ def list_options
19
+ return @_added_options ||= nil
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
File without changes
@@ -0,0 +1,14 @@
1
+ module CoreRefinements
2
+ module String
3
+ module ToHuman
4
+ class ::String
5
+ # take a CamelCase or under_scored string and convert it to a human readable string (useful for output).
6
+ def to_human
7
+ return self.split("_").map {|word| word[0] + word[1..-1]}.join(" ") if self.include?("_")
8
+
9
+ return self.split(/(?=[A-Z])/).join(" ").downcase
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module CoreRefinements
2
+ VERSION = "0.1.6"
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper.rb'
2
+ require 'core_refinements/Array/collapse_ranges.rb'
3
+ using CoreRefinements::Array::CollapseRanges
4
+
5
+ describe Array do
6
+ describe "#collapse_ranges" do
7
+ it "Will return a collapsed range" do
8
+ expect([1,2,3,4,5,6,7,9,11,13,14,15].collapse_ranges).to eq [1..7,9,11,13..15]
9
+ end
10
+ it "Will not lose duplicates" do
11
+ expect([1,1,2,3,3,3,4,5].collapse_ranges).to eq [1,1..3,3,3..5]
12
+ end
13
+ it "will not need to be sorted" do
14
+ expect([4,5,6,1,2,3].collapse_ranges).to eq [4..6,1..3]
15
+ end
16
+ it "will return nil if it gets a non Numeric argument" do
17
+ expect([1,2,3,'a'].collapse_ranges).to be nil
18
+ end
19
+ it "Will 'split' the return into two parts (range,singles) with the parameter split." do
20
+ expect([1,2,3,5,7].collapse_ranges(split:true)).to eq([[1..3], [5, 7]])
21
+ end
22
+ it "Will allow a step parameter to include missing values in the ranges." do
23
+ complex_range = [13,1,2,3,4,6,8,11,14,17,20,25,30,35,2]
24
+ complex_2 = [13,1..8,11,14,17,20,25,30,35,2]
25
+ complex_5 = [13,1..35,2]
26
+ expect(complex_range.collapse_ranges(step:1)).to_not eq complex_2
27
+ expect(complex_range.collapse_ranges(step:2)).to eq complex_2
28
+ expect(complex_range.collapse_ranges(step:3)).to_not eq complex_2
29
+ expect(complex_range.collapse_ranges(step:4)).to_not eq complex_2
30
+ expect(complex_range.collapse_ranges(step:5)).to_not eq complex_2
31
+ expect(complex_range.collapse_ranges(step:5)).to eq complex_5
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper.rb'
2
+ require 'core_refinements/Array/index_by'
3
+ using CoreRefinements::Array::IndexBy
4
+ describe CoreRefinements do
5
+ describe Array do
6
+ describe "#index_by" do
7
+ before (:all) do
8
+ @array = [
9
+ [1,2,3],
10
+ [4,5,6],
11
+ ['a','b','c'],
12
+ ['d','e','f'],
13
+ ['cat', 'dog', 'bird']
14
+ ]
15
+ @hash = [
16
+ {id:11, x:2, y:3},
17
+ {id:22, x:2, y:5},
18
+ {id:33, x:4, y:4}
19
+ ]
20
+ end
21
+ describe Hash do
22
+ it "Will index an array of hashes by key." do
23
+ new_hash = @hash.index_by :id
24
+ expect(new_hash[11]).to eq @hash.first
25
+ end
26
+ it "Will return nil if it can't find the key." do
27
+ expect(@hash.index_by :not_real).to be nil
28
+ end
29
+ end
30
+ describe Array do
31
+ it "Will index an array of hashes by position." do
32
+ new_array = @array.index_by 0
33
+ expect(new_array[@array.last.first]).to eq @array.last
34
+ end
35
+
36
+ it "Will return nil if it can't find the position." do
37
+ expect(@array.index_by @array.count+1).to be nil
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper.rb'
2
+ require 'core_refinements/Numeric/percentage_of'
3
+ using CoreRefinements::Numeric::PercentageOf
4
+
5
+ describe Numeric do
6
+ describe "#percentage_of" do
7
+ # Goddamn, some days. I really need to figure out how to phrase this better.
8
+ it "Given an argument, return the percentage of the number in self." do
9
+ expect(10.percentage_of(100)).to eq 10
10
+ expect(10.percentage_of(990)).to eq 1.01
11
+ end
12
+ it "Will round given a round: parameter." do
13
+ expect(10.percentage_of(667281,5)).to eq 0.0015
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper.rb'
2
+ require 'core_refinements/Object/options.rb'
3
+ using CoreRefinements::Object::Options
4
+
5
+ describe CoreRefinements do
6
+ describe Object do
7
+ before (:each) do
8
+ end
9
+ describe '#add_option' do
10
+ it 'should add method by name or symbol' do
11
+ @test_object = Hash.new
12
+ @test_object.add_option('test_string')
13
+ expect(@test_object.methods.include?(:test_string)).to be true
14
+ @test_object = Hash.new
15
+ @test_object.add_option(:test_symbol)
16
+ expect(@test_object.methods.include?(:test_symbol)).to be true
17
+ end
18
+ it 'should return the argument passed to it' do
19
+ @test_object = Array.new
20
+ @test_object.add_option('test')
21
+ tests = [[1,2,3], 'this', 'that', :those, 5]
22
+ tests.each do |t|
23
+ @test_object.test t
24
+ expect(@test_object.test).to eq t
25
+ end
26
+ end
27
+ end
28
+ describe '#list_options' do
29
+ it "should return the list of options as an array when given no argument" do
30
+ @test_object = Array.new
31
+ @test_object.add_option('test')
32
+ @test_object.add_option(:test2)
33
+ expect(@test_object.list_options).to eq [:test, :test2]
34
+ end
35
+ end
36
+ end
37
+ end
File without changes
@@ -0,0 +1,14 @@
1
+ require 'spec_helper.rb'
2
+ require 'core_refinements/String/to_human'
3
+ using CoreRefinements::String::ToHuman
4
+
5
+ describe String do
6
+ describe "#to_human" do
7
+ it "Will turn a CamelCase word into words with spaces." do
8
+ expect("CamelCasedWord".to_human).to eq "camel cased word"
9
+ end
10
+ it "Will transform underscored words into words with spaces." do
11
+ expect("underscored_words".to_human).to eq "underscored words"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,2 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: core_refinements
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ platform: ruby
6
+ authors:
7
+ - Ernie Brodeur
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A collection of methods that can be used to extend the basic ruby classes.
42
+ email:
43
+ - ebrodeur@ujami.net
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - .travis.yml
51
+ - Gemfile
52
+ - Guardfile
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - WHATSNEW.md
57
+ - core_refinements.gemspec
58
+ - lib/core_augmentations/version.rb
59
+ - lib/core_refinements.rb
60
+ - lib/core_refinements/Array/collapse_ranges.rb
61
+ - lib/core_refinements/Array/index_by.rb
62
+ - lib/core_refinements/Numeric/percentage_of.rb
63
+ - lib/core_refinements/Object/options.rb
64
+ - lib/core_refinements/Object/save.rb
65
+ - lib/core_refinements/String/to_human.rb
66
+ - lib/core_refinements/version.rb
67
+ - spec/lib/core_refinements/Array/collapse_ranges_spec.rb
68
+ - spec/lib/core_refinements/Array/index_by_spec.rb
69
+ - spec/lib/core_refinements/Numeric/percentage_of_spec.rb
70
+ - spec/lib/core_refinements/Object/options_spec.rb
71
+ - spec/lib/core_refinements/Object/save_spec.rb
72
+ - spec/lib/core_refinements/String/to_human_spec.rb
73
+ - spec/spec_helper.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.14
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Methods to augment the core classes in ruby.
98
+ test_files:
99
+ - spec/lib/core_refinements/Array/collapse_ranges_spec.rb
100
+ - spec/lib/core_refinements/Array/index_by_spec.rb
101
+ - spec/lib/core_refinements/Numeric/percentage_of_spec.rb
102
+ - spec/lib/core_refinements/Object/options_spec.rb
103
+ - spec/lib/core_refinements/Object/save_spec.rb
104
+ - spec/lib/core_refinements/String/to_human_spec.rb
105
+ - spec/spec_helper.rb