split_into 1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 468575e8b7e8fe9573e7bdd423d4333c5edb67fe
4
+ data.tar.gz: 66dc96a99217e1fb536b87c941d2ef1f52971276
5
+ SHA512:
6
+ metadata.gz: 9f62b24b5b45c136ae536e8ba758c24112b2a26d850d8ca68136ec2174d0564ba53155c7e50214206a6047ccd874620ab3d84ca91828909874b8f8704e4095c7
7
+ data.tar.gz: a89ad5137c0ad47ebf4bd91a8c2437786b1708298ff99850da14e8d70d13006b20bbbded98432881b566da2b8cb74a6a7551e4cdf0dc07991d7c242cc8388802
@@ -0,0 +1,8 @@
1
+ pkg/*
2
+ doc/*
3
+ tmp/*
4
+ tags
5
+ *.gem
6
+ .bundle
7
+ Gemfile.lock
8
+ .yardoc
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Copyright AIMRED CC. All rights reserved.
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ THIS SOFTWARE IS PROVIDED BY AIMRED CC ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AIMRED CC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10
+
11
+ The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of AIMRED CC.
@@ -0,0 +1,42 @@
1
+ # SplitInto
2
+
3
+ ## Overview
4
+
5
+ SplitInto is a micro API whose sole purpose is to split an integer into equal or roughly equal parts. This is useful when dealing with objects that are not well suited to being split into fractional parts.
6
+
7
+ For instance 8 cents can not be split into 3 parts equally due to the lack of 1/2 cent coins, but can be split into roughly equal parts of 2c, 3c and 3c. Using SplitInto this would be written as:
8
+
9
+ SplitInto.split(8,3) # => [2, 3, 3]
10
+
11
+ 9 cents can be split equally
12
+
13
+ SplitInto.split(9,3) # => [3, 3, 3]
14
+
15
+ The part components will never differ by more than 1.
16
+
17
+ This was written partly to dogfood `simplecheck` and see what changes it requires in a real API.
18
+
19
+ ## Installation
20
+
21
+ gem install split_into
22
+
23
+ ## Usage
24
+
25
+ SplitInto can either act as a standalone module or as an extension of the Integer class.
26
+
27
+ As a standalone module:
28
+
29
+ require 'split_into'
30
+
31
+ SplitInto.split(10,4)
32
+
33
+ To extend integer include `split_into/extend_integer`.
34
+
35
+ require 'split_into/extend_integer'
36
+
37
+ 10.split_into(4)
38
+
39
+ ## License
40
+
41
+ SplitInto is distributed under the BSD License.
42
+
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test:lib'
5
+ t.pattern = "test/*_test.rb"
6
+ end
7
+
8
+ task default: :test
@@ -0,0 +1,22 @@
1
+ require 'simplecheck'
2
+ require 'split_into/split_error'
3
+
4
+ module SplitInto
5
+ class <<self
6
+ include Simplecheck
7
+ end
8
+
9
+ def self.split(dividend, divisor)
10
+ check(dividend, divisor, Integer, error_message: 'Dividend and divisor must be of type Integer')
11
+ check(divisor >= 0, error_message: 'Divisor is less than zero')
12
+ check(divisor <= dividend, error_message: 'Divisor is greater than the dividend')
13
+
14
+ return [] if divisor.zero?
15
+
16
+ parts = Array.new(divisor){ dividend.div(divisor) }
17
+ dividend.modulo(divisor).times { |i| parts[i] += 1 }
18
+ parts.reverse
19
+ rescue Simplecheck::CheckFailed => exception
20
+ raise SplitInto::SplitError, exception.message
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ require 'split_into'
2
+
3
+ class Integer
4
+ def split_into(divisor)
5
+ SplitInto.split(self,divisor)
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module SplitInto
2
+ class SplitError < StandardError;end
3
+ end
@@ -0,0 +1,3 @@
1
+ module SplitInto
2
+ VERSION = '1.1'
3
+ end
@@ -0,0 +1,20 @@
1
+ $:.unshift File.expand_path('../lib', __FILE__)
2
+ require 'split_into/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'split_into'
6
+ s.version = SplitInto::VERSION
7
+ s.licenses = ['BSD']
8
+ s.summary = "Split an integer into equally sized component integers"
9
+ s.description = "SplitInto is a micro API whose sole purpose is to split an integer into equal or roughly equal parts. This is useful when dealing with objects that are not well suited to being split into fractional parts."
10
+ s.author = "Farrel Lifson"
11
+ s.email = 'farrel.lifson@aimred.com'
12
+ s.files = %x{git ls-files}.split("\n")
13
+ s.test_files = %x{git ls-files -- {test,spec,features}/*}.split("\n")
14
+ s.require_paths = ['lib']
15
+
16
+ s.homepage = 'https://www.aimred.com/projects/split_into'
17
+
18
+ s.add_dependency 'simplecheck', '~> 2.0'
19
+ end
20
+
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class SplitIntoIncludedTest < Minitest::Test
4
+ def test_integer
5
+ assert_equal [], 10.split_into(0)
6
+ assert_equal [10], 10.split_into(1)
7
+ assert_equal [5,5], 10.split_into(2)
8
+ assert_equal [3,3,4], 10.split_into(3)
9
+ assert_equal [2,2,3,3], 10.split_into(4)
10
+ assert_equal [2,2,2,2,2], 10.split_into(5)
11
+ assert_equal [1,1,2,2,2,2], 10.split_into(6)
12
+ assert_equal [1,1,1,1,2,2,2], 10.split_into(7)
13
+ assert_equal [1,1,1,1,1,1,2,2], 10.split_into(8)
14
+ assert_equal [1,1,1,1,1,1,1,1,2], 10.split_into(9)
15
+ assert_equal [1,1,1,1,1,1,1,1,1,1], 10.split_into(10)
16
+ end
17
+
18
+ def test_exceptions
19
+ assert_raises(SplitInto::SplitError) { 10.split_into(-1) }
20
+ assert_raises(SplitInto::SplitError) { 10.split_into(11) }
21
+ assert_raises(SplitInto::SplitError) { 10.split_into(5.5) }
22
+ end
23
+ end
24
+
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+
3
+ class SplitIntoTest < Minitest::Test
4
+ def test_integer
5
+ assert_equal [], SplitInto.split(10,0)
6
+ assert_equal [10], SplitInto.split(10,1)
7
+ assert_equal [5,5], SplitInto.split(10,2)
8
+ assert_equal [3,3,4], SplitInto.split(10,3)
9
+ assert_equal [2,2,3,3], SplitInto.split(10,4)
10
+ assert_equal [2,2,2,2,2], SplitInto.split(10,5)
11
+ assert_equal [1,1,2,2,2,2], SplitInto.split(10,6)
12
+ assert_equal [1,1,1,1,2,2,2], SplitInto.split(10,7)
13
+ assert_equal [1,1,1,1,1,1,2,2], SplitInto.split(10,8)
14
+ assert_equal [1,1,1,1,1,1,1,1,2], SplitInto.split(10,9)
15
+ assert_equal [1,1,1,1,1,1,1,1,1,1], SplitInto.split(10,10)
16
+ end
17
+
18
+ def test_exceptions
19
+ assert_raises(SplitInto::SplitError) { SplitInto.split(10,-1) }
20
+ assert_raises(SplitInto::SplitError) { SplitInto.split(10,11) }
21
+ assert_raises(SplitInto::SplitError) { SplitInto.split(10.5,5) }
22
+ assert_raises(SplitInto::SplitError) { SplitInto.split(10,5.5) }
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'split_into'
3
+ require 'split_into/extend_integer'
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: split_into
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.1'
5
+ platform: ruby
6
+ authors:
7
+ - Farrel Lifson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simplecheck
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: SplitInto is a micro API whose sole purpose is to split an integer into
28
+ equal or roughly equal parts. This is useful when dealing with objects that are
29
+ not well suited to being split into fractional parts.
30
+ email: farrel.lifson@aimred.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - Gemfile
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - lib/split_into.rb
41
+ - lib/split_into/extend_integer.rb
42
+ - lib/split_into/split_error.rb
43
+ - lib/split_into/version.rb
44
+ - split_into.gemspec
45
+ - test/split_into_extend_integer_test.rb
46
+ - test/split_into_test.rb
47
+ - test/test_helper.rb
48
+ homepage: https://www.aimred.com/projects/split_into
49
+ licenses:
50
+ - BSD
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.2.0
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Split an integer into equally sized component integers
72
+ test_files:
73
+ - test/split_into_extend_integer_test.rb
74
+ - test/split_into_test.rb
75
+ - test/test_helper.rb
76
+ has_rdoc: