randsum 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.0
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
5
+ after_script: bundle exec codeclimate-test-reporter
6
+
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at alxjrvs@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in randsum.gemspec
4
+ gemspec
5
+ group :test do
6
+ gem "simplecov"
7
+ gem "codeclimate-test-reporter", "~> 1.0.0"
8
+ end
data/LICENSE.md ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2012 Alex Jarvis
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ 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.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Alex Jarvis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,181 @@
1
+ # Randsum
2
+
3
+ [![Build Status](https://travis-ci.org/RANDSUM/randsum.svg?branch=master)](https://travis-ci.org/RANDSUM/randsum)[![Code Climate](https://codeclimate.com/github/RANDSUM/randsum/badges/gpa.svg)](https://codeclimate.com/github/RANDSUM/randsum) [![Test Coverage](https://codeclimate.com/github/RANDSUM/randsum/badges/coverage.svg)](https://codeclimate.com/github/RANDSUM/randsum/coverage) [![Issue Count](https://codeclimate.com/github/RANDSUM/randsum/badges/issue_count.svg)](https://codeclimate.com/github/RANDSUM/randsum)
4
+ all hail the rectangles
5
+ ## Finally, a way to roll specific quantities of particular sides of dice.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'randsum'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install randsum
22
+
23
+ ## How to Get Rolling
24
+
25
+ ```
26
+ $ gem install randsum
27
+ $ irb
28
+ > require 'randsum'
29
+ #=> true
30
+ ```
31
+
32
+ ### The `Die` Class
33
+
34
+ `Randsum` lets you make `Die`, which sounds like an existential baby stringing together its first downer of a sentence.
35
+
36
+ A `Die` represents something more commonly referred to in the plural - Dice. Each `Die` takes an argument in its initializer, representing the number of sides of the die you want to make.
37
+
38
+ ```
39
+ > d6 = Randsum::Die.new(6)
40
+ ```
41
+
42
+ Once you've created this new random number object, you can use `.roll` to generate a random number from 1 to the size of the die.
43
+
44
+ ```
45
+ > d6.roll
46
+ #=> You rolled 1 d6, and got a 5. (Rolls: [5])
47
+ ```
48
+
49
+ ...aw, go ahead. Roll it a few more times. I won't blame you.
50
+
51
+ ```
52
+ > d6.roll
53
+ #=> You rolled 1 d6, and got a 6. (Rolls: [6])
54
+
55
+ > d6.roll
56
+ #=> You rolled 1 d6, and got a 3. (Rolls: [3])
57
+ ```
58
+
59
+ That's pretty neat, right?
60
+
61
+ `Randsum` comes pre-packaged with several shortcuts for popular Die sizes:
62
+
63
+ ```
64
+ #D20
65
+ > Randsum::D20.roll
66
+ #=> You rolled 1 d20, and got a 20. (Rolls: [20])
67
+
68
+ #D12
69
+ > Randsum::D12.roll
70
+ #=> You rolled 1 d12, and got a 2. (Rolls: [2])
71
+
72
+ #D2-6, D8, and D10!
73
+ ```
74
+
75
+ ### Rolling Multiple Dice
76
+
77
+ `Die#roll` also takes an optional argument that represents how many bones you want to roll. Check it:
78
+
79
+ ```
80
+ > Randsum::D6.roll(2)
81
+ #=> You rolled 2 d6, and got a 2. (Rolls: [1, 1])
82
+ ```
83
+
84
+ Snakeyes. Rough.
85
+
86
+
87
+ ### Making use of the rolls: the `RollReport`
88
+
89
+ `Die#roll` returns a `RollReport` object, which can teach you a lot about your rolls (but not, tragically, how to roll better.)
90
+
91
+ ```
92
+ > report = Randsum::D20.roll 5
93
+ #=> You rolled 5 d20, and got a 61. (Rolls: [20, 16, 9, 14, 1])
94
+ ```
95
+
96
+ The `#total` method represents the sum total of the dice that were rolled:
97
+
98
+ ```
99
+ > report.total
100
+ #=> 61
101
+ ```
102
+
103
+ The `#rolls` array, which reports the individual results of any dice rolled:
104
+
105
+ ```
106
+ > report.rolls
107
+ #=> [20, 17, 9, 14,1]
108
+ ```
109
+
110
+ `#quantity` tells you how many dice were rolled this time:
111
+
112
+ ```
113
+ > report.quantity
114
+ #=> 5
115
+ ```
116
+
117
+ `#sides` reports the number of sides on the dice rolled in this result:
118
+
119
+ ```
120
+ > report.sides
121
+ #=> 20
122
+ ```
123
+
124
+ And if you don't like that roll (hey we get it) you can use `#die` to get another of the same die to roll again!
125
+
126
+ ```
127
+ > report.die.roll
128
+ #=> You rolled 1 D20, and got 17. (Rolls: [17])
129
+ ```
130
+
131
+ #### Manipulating `RollReport`s
132
+
133
+ Roll results can be further manipulated after their original creation. To facilitate popular use-cases for Dice rolling, `RollReport`s also include public `#drop_lowest` and `#drop_highest`, and `#drop` methods.
134
+
135
+ `#drop_lowest` returns a new `RollReport` without the lowest numerical die roll.
136
+
137
+ ```
138
+ > report = Randsum::D6.roll 4
139
+ #=> You rolled 4 d6, and got 11. (Rolls: [3, 2, 2, 4])
140
+
141
+ > new_report = report.drop_lowest
142
+ #=> You rolled 3 d6, and got 9. (Rolls: [4, 3, 2])
143
+ ```
144
+
145
+ Similarly, `#drop_highest` will remove the highest number in the `rolls` array.
146
+
147
+ ```
148
+ > new_result = report.drop_highest
149
+ #=> You rolled 3 d6, and got 7. (Rolls: [2, 2, 3])
150
+ ```
151
+
152
+ Both `#drop_lowest` and `#drop_highest` can also take an optional integer argument.
153
+
154
+ ```
155
+ > result.drop_highest(2)
156
+ #=> You rolled 2 d6, and got 4. (Rolls: [2, 2])
157
+ ```
158
+
159
+ ### WHY IS THIS SO COMPLICATED
160
+
161
+ `Die#simple_roll` will just give you a random number, but where's the fun in that?
162
+
163
+ ### Why build this?
164
+ [Rollr](http://github.com/alxjrvs/rollr) was one of the first things I ever built. I think it's funny, and I'm not sure why.
165
+
166
+ I recently had the privilege of attending Sandi Metz' POODNyc workshop, which involved a continual practice of refactoring the same "simple" problem over and over again. I liked revisitng Rollr through the years, and Randsum reflects an attempt to use small refactoring techniques to continue to mull over the "simple" problem of random numbers.
167
+
168
+ ## Development
169
+
170
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
171
+
172
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
173
+
174
+ ## Contributing
175
+
176
+ Bug reports and pull requests are welcome on GitHub at https://github.com/alxjrvs/randsum. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
177
+
178
+
179
+ ## License
180
+
181
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "randsum"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/randsum ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'randsum'
4
+
5
+ a = ARGV[1]
6
+
7
+ if ARGV[1].include? "d"
8
+ a = ARGV[1].split(%r[d])[1].to_i
9
+ else
10
+ puts "Wrong Usage. Proper Usage: randsum [Integer] d[integer]"
11
+ end
12
+ puts Randsum::Die.new(a.to_i).roll(ARGV[0].to_i)
13
+
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,37 @@
1
+ require 'securerandom'
2
+
3
+ module Randsum
4
+ class Die
5
+ ZERO_INDEX_FIXER = 1
6
+ attr_accessor :sides, :randomizer
7
+
8
+ def initialize(sides, randomizer: SecureRandom)
9
+ @sides = sides
10
+ @randomizer = randomizer
11
+ end
12
+
13
+ def roll(quantity = 1)
14
+ Randsum::RollReport.new(
15
+ roll_for(quantity)
16
+ )
17
+ end
18
+
19
+ def to_s
20
+ "d#{sides}"
21
+ end
22
+
23
+ def simple_roll
24
+ randomizer.random_number(sides).to_i + ZERO_INDEX_FIXER
25
+ end
26
+
27
+ private
28
+
29
+ def roll_for(quantity)
30
+ Randsum::Roll.new(
31
+ quantity: quantity,
32
+ die: self
33
+ )
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ module Randsum
2
+ class Dropper < Filter
3
+
4
+ def self.dropper_for(quantity:,extremity:, rolls:)
5
+ Object.const_get(
6
+ "Randsum::#{extremity.to_s.gsub("est","").capitalize}Dropper"
7
+ ).new(quantity: quantity, rolls: rolls)
8
+ end
9
+
10
+ def filter
11
+ ordered.first(remainder)
12
+ end
13
+
14
+ def ordered
15
+ raise NotImplementedError
16
+ end
17
+
18
+ def remainder
19
+ rolls.length - quantity
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module Randsum
2
+ class HighDropper < Dropper
3
+ def ordered
4
+ rolls.sort { |a, b| a <=> b }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module Randsum
2
+ class LowDropper < Dropper
3
+ def ordered
4
+ rolls.sort { |a, b| b <=> a }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,14 @@
1
+ module Randsum
2
+ class Filter
3
+ attr_reader :quantity, :rolls
4
+
5
+ def initialize(quantity:, rolls:)
6
+ @quantity = quantity
7
+ @rolls = rolls
8
+ end
9
+
10
+ def filter
11
+ raise NotImplementedError
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ module Randsum
2
+ SUMMARY = "RNaaOOP"
3
+ DESCRIPTION = "Don't you hate it when you have to generate specific quantities of random numbers from a particular range while writing ruby? Look no further. "
4
+ HOMEPAGE = "https://api.randsum.io"
5
+ LICENSE = "MIT"
6
+ end
@@ -0,0 +1,39 @@
1
+ module Randsum
2
+ class Roll
3
+ attr_reader :die, :quantity, :sides, :result
4
+
5
+ def initialize(die:, quantity:, result: nil)
6
+ @die = die
7
+ @quantity = quantity
8
+ @sides = die.sides
9
+ @result = result || roll!
10
+ end
11
+
12
+ def to_s
13
+ "Rolls: #{result}"
14
+ end
15
+
16
+ def drop(quantity:,extremity:)
17
+ return new_roll_with(
18
+ result: Dropper.dropper_for(
19
+ quantity: quantity,
20
+ extremity: extremity,
21
+ rolls: result
22
+ ).filter
23
+ )
24
+ end
25
+
26
+ private
27
+ def new_roll_with(result: nil)
28
+ return Roll.new(
29
+ die: die,
30
+ quantity: quantity,
31
+ result: result
32
+ )
33
+ end
34
+
35
+ def roll!
36
+ (1..quantity).map { die.simple_roll }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,61 @@
1
+ module Randsum
2
+ class RollReport
3
+
4
+ def initialize(roll)
5
+ @roll = roll
6
+ end
7
+
8
+ def inspect
9
+ to_s
10
+ end
11
+
12
+ def to_s
13
+ "You rolled #{quantity} #{die}, and got #{total}. (#{roll})"
14
+ end
15
+
16
+ def to_i
17
+ total
18
+ end
19
+
20
+ def sides
21
+ die.sides
22
+ end
23
+
24
+ def die
25
+ roll.die
26
+ end
27
+
28
+ def rolls
29
+ @_rolls ||= roll.result
30
+ end
31
+
32
+ def total
33
+ @_total ||= rolls.inject(:+)
34
+ end
35
+
36
+ def quantity
37
+ @_quantity ||= rolls.count
38
+ end
39
+
40
+ def drop(quantity:, extremity:)
41
+ return RollReport.new(
42
+ roll.drop(quantity: quantity, extremity: extremity)
43
+ )
44
+ end
45
+
46
+ def drop_lowest(quantity = 1)
47
+ return RollReport.new(
48
+ roll.drop(quantity: quantity, extremity: :lowest)
49
+ )
50
+ end
51
+
52
+ def drop_highest(quantity = 1)
53
+ return RollReport.new(
54
+ roll.drop(quantity: quantity, extremity: :highest)
55
+ )
56
+ end
57
+
58
+ private
59
+ attr_reader :roll
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Randsum
2
+ VERSION = "1.0.0"
3
+ end
data/lib/randsum.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "randsum/version"
2
+ require "randsum/filters/filter"
3
+ require "randsum/filters/droppers/dropper"
4
+ require "randsum/filters/droppers/high_dropper"
5
+ require "randsum/filters/droppers/low_dropper"
6
+ require "randsum/roll"
7
+ require "randsum/die"
8
+ require "randsum/roll_report"
9
+
10
+ module Randsum
11
+
12
+ D2 = Die.new(2)
13
+ D3 = Die.new(3)
14
+ D4 = Die.new(4)
15
+ D6 = Die.new(6)
16
+ D8 = Die.new(8)
17
+ D10= Die.new(10)
18
+ D12 = Die.new(12)
19
+ D20 = Die.new(20)
20
+
21
+ end
data/randsum.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'randsum/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "randsum"
8
+ spec.version = Randsum::VERSION
9
+ spec.authors = ["Alex Jarvis"]
10
+ spec.email = ["alxjrvs@gmail.com"]
11
+ spec.summary = %q{Random Numbers, With Style.}
12
+ spec.description = %q{Randsum is a Gem that provides a more familiar interface for generating random numbers - Rolling Dice. Using Randsum, developers can generate psuedorandom numbers securely and succinctly.}
13
+ spec.homepage = "http://www.github.com/alxjrvs/randsum"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "pry", "~> 0.10.3"
25
+ end