ruby_quiz_2 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b06d825cebcf0e40fecbab28882b023c9688a90
4
+ data.tar.gz: bec1af255fef4fc8bf104a36dd4b27921bf6550e
5
+ SHA512:
6
+ metadata.gz: bf184f3a087da9fe7193443041f75d28cb4ab2fcd4e6caedadb3ded4e4da9c9c966d3e13c577b4c68822fe0ea679848237a4e9baac305ff3d34964166bf64e3f
7
+ data.tar.gz: 148b36d09671877572b08f578230c0e37a2e7013b6841c327279eb685c05614bdf594881a6d3b0c3d16fe0de448a9274a6b74bb76892a0afdd1afbf713f8f23b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby_quiz_2.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Sean Devine
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,38 @@
1
+ # Ruby Quiz #2 Solution
2
+
3
+ A solution to the [second Ruby quiz](http://rubyquiz.com/quiz2.html).
4
+
5
+ This command line utility selects secret santas for a list of people.
6
+
7
+ The original quiz requested that the santas be emailed, but I just made a list.
8
+
9
+ ```bash
10
+ gem install 'ruby_quiz_2'
11
+ ```
12
+
13
+ `feasible.txt`
14
+ ```txt
15
+ Luke Skywalker <luke@theforce.net>
16
+ Leia Skywalker <leia@therebellion.org>
17
+ Toula Portokalos <toula@manhunter.org>
18
+ Gus Portokalos <gus@weareallfruit.net>
19
+ Bruce Wayne <bruce@imbatman.com>
20
+ Virgil Brigman <virgil@rigworkersunion.org>
21
+ Lindsey Brigman <lindsey@iseealiens.net>
22
+ ```
23
+
24
+ ```bash
25
+ barelyknown$ ruby_quiz_2 secret_santas feasible.txt
26
+
27
+ SECRET SANTA SELECTIONS
28
+
29
+ Santa Giftee
30
+ ------------------------- -------------------------
31
+ Luke Skywalker Lindsey Brigman
32
+ Leia Skywalker Gus Portokalos
33
+ Toula Portokalos Bruce Wayne
34
+ Gus Portokalos Leia Skywalker
35
+ Bruce Wayne Virgil Brigman
36
+ Virgil Brigman Toula Portokalos
37
+ Lindsey Brigman Luke Skywalker
38
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/ruby_quiz_2 ADDED
@@ -0,0 +1,18 @@
1
+ require "thor"
2
+
3
+ require "ruby_quiz_2"
4
+
5
+ module RubyQuiz2
6
+ class SecretSantaCLI < Thor
7
+
8
+ desc "secret_santas PATH", "select secret santas"
9
+ def secret_santas(path)
10
+ people_list = PeopleList.new(File.read(path))
11
+ selection_party = SelectionParty.new(people_list)
12
+ selection_party.make_selections
13
+ SelectionsPrinter.new.print(selection_party.selections)
14
+ end
15
+
16
+ end
17
+ SecretSantaCLI.start(ARGV)
18
+ end
@@ -0,0 +1,12 @@
1
+ require "ruby_quiz_2/version"
2
+
3
+ require "ruby_quiz_2/person"
4
+ require "ruby_quiz_2/people_list"
5
+ require "ruby_quiz_2/selection_rules"
6
+ require "ruby_quiz_2/selection"
7
+ require "ruby_quiz_2/selection_party"
8
+ require "ruby_quiz_2/selections_printer"
9
+
10
+ module RubyQuiz2
11
+
12
+ end
@@ -0,0 +1,33 @@
1
+ module RubyQuiz2
2
+ class PeopleList
3
+ include Enumerable
4
+
5
+ def initialize(list=nil)
6
+ import list if list
7
+ end
8
+
9
+ def people
10
+ @people ||= []
11
+ end
12
+
13
+ def length
14
+ people.length
15
+ end
16
+
17
+ def each
18
+ people.each { |person| yield person }
19
+ end
20
+
21
+ private
22
+
23
+ def import(list)
24
+ list.split("\n").each do |line|
25
+ first_name, family_name, email = line.split(" ")
26
+ name = [first_name, family_name].join(" ")
27
+ email = email.gsub(/[<>]/, "")
28
+ people << Person.new("#{first_name} #{family_name}", email)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ module RubyQuiz2
2
+ class Person
3
+
4
+ attr_reader :name, :email
5
+
6
+ def initialize(name, email)
7
+ @name, @email = name, email
8
+ end
9
+
10
+ def family_name
11
+ name.split(" ")[1]
12
+ end
13
+
14
+ def related_to?(person)
15
+ family_name == person.family_name
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,11 @@
1
+ module RubyQuiz2
2
+ class Selection
3
+
4
+ attr_reader :santa, :giftee
5
+
6
+ def initialize(santa, giftee)
7
+ @santa, @giftee = santa, giftee
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,56 @@
1
+ module RubyQuiz2
2
+ class SelectionParty
3
+
4
+ attr_reader :people_list, :attendees
5
+
6
+ def initialize(people_list)
7
+ @people_list = people_list
8
+ end
9
+
10
+ def selections
11
+ @selections ||= []
12
+ end
13
+
14
+ def selection_rules
15
+ @selection_rules ||= SelectionRules.new
16
+ end
17
+
18
+ def make_selections
19
+ while selections.empty? && possible_selections.any?
20
+ candidate_selections = possible_selections.shift
21
+ if selection_rules.valid_set?(candidate_selections)
22
+ self.selections.replace(candidate_selections)
23
+ end
24
+ end
25
+ unless selections.any?
26
+ raise ArgumentError, "there are no valid combinations of selections"
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def valid_selections_by_santa
33
+ @valid_selections_by_santa ||= begin
34
+ Hash.new { |h,k| h[k] = [] }.tap do |h|
35
+ people_list.each do |santa|
36
+ people_list.people.shuffle.each do |giftee|
37
+ if selection_rules.valid?(santa, giftee)
38
+ h[santa] << Selection.new(santa, giftee)
39
+ end
40
+ end
41
+ end
42
+ unless h.length == people_list.length
43
+ raise ArgumentError, "the rules give some people no options"
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ def possible_selections
50
+ @possible_selections ||= begin
51
+ valid_selections_by_santa.shift[1].product(*valid_selections_by_santa.values)
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,16 @@
1
+ module RubyQuiz2
2
+ class SelectionRules
3
+
4
+ def valid?(santa, giftee)
5
+ return false if santa == giftee
6
+ return false if santa.related_to?(giftee)
7
+ true
8
+ end
9
+
10
+ def valid_set?(selections)
11
+ return false unless selections.collect(&:giftee).uniq.size == selections.size
12
+ true
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module RubyQuiz2
2
+ class SelectionsPrinter
3
+
4
+ def print(selections)
5
+ puts
6
+ puts "SECRET SANTA SELECTIONS"
7
+ puts
8
+ w = 25
9
+ puts "Santa".ljust(w, " ") + " " + "Giftee"
10
+ puts "-----".ljust(w, "-") + " " + "------".ljust(w, "-")
11
+ selections.each do |selection|
12
+ puts selection.santa.name.ljust(w, " ") + " " + selection.giftee.name
13
+ end
14
+ puts
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module RubyQuiz2
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruby_quiz_2/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby_quiz_2"
8
+ spec.version = RubyQuiz2::VERSION
9
+ spec.authors = ["Sean Devine"]
10
+ spec.email = ["barelyknown@icloud.com"]
11
+ spec.summary = %q{Solution to ruby quiz #1 @ http://rubyquiz.com/quiz2.html}
12
+ spec.description = spec.summary
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_dependency "thor"
22
+ spec.executables << "ruby_quiz_2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.5"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "rspec-context-private"
28
+ end
@@ -0,0 +1,7 @@
1
+ Luke Skywalker <luke@theforce.net>
2
+ Leia Skywalker <leia@therebellion.org>
3
+ Toula Portokalos <toula@manhunter.org>
4
+ Gus Portokalos <gus@weareallfruit.net>
5
+ Bruce Wayne <bruce@imbatman.com>
6
+ Virgil Brigman <virgil@rigworkersunion.org>
7
+ Lindsey Brigman <lindsey@iseealiens.net>
@@ -0,0 +1,2 @@
1
+ Luke Skywalker <luke@theforce.net>
2
+ Leia Skywalker <leia@therebellion.org>
@@ -0,0 +1,26 @@
1
+ module RubyQuiz2
2
+ describe PeopleList do
3
+
4
+ let :list do
5
+ File.read(File.expand_path("../../../fixtures/people_lists/feasible.txt", __FILE__))
6
+ end
7
+
8
+ context "when initialized with the list" do
9
+ subject do
10
+ described_class.new list
11
+ end
12
+ it "has the right number of people" do
13
+ expect(subject.length).to eq list.split("\n").size
14
+ end
15
+ end
16
+
17
+ it "has a people array" do
18
+ expect(subject.people).to be_empty
19
+ end
20
+
21
+ it "implements enumerable" do
22
+ expect(subject).to respond_to :each
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ module RubyQuiz2
2
+ describe Person do
3
+
4
+ let :name do
5
+ "Luke Skywalker"
6
+ end
7
+
8
+ let :email do
9
+ "luke@theforce.net"
10
+ end
11
+
12
+ let :related_person do
13
+
14
+ end
15
+
16
+ subject do
17
+ described_class.new name, email
18
+ end
19
+
20
+ it "sets the name" do
21
+ expect(subject.name).to eq name
22
+ end
23
+
24
+ it "sets the email" do
25
+ expect(subject.email).to eq email
26
+ end
27
+
28
+ it "gets the family name" do
29
+ expect(subject.family_name).to eq "Skywalker"
30
+ end
31
+
32
+ it "is in the same family as the other people with the same last name" do
33
+ family_member = described_class.new "Leia Skywalker", "leia@therebellion.org"
34
+ expect(subject.related_to?(family_member)).to eq true
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ module RubyQuiz2
2
+ describe SelectionParty do
3
+
4
+ subject do
5
+ described_class.new people_list
6
+ end
7
+
8
+ context "with a feasible people list" do
9
+ let :people_list do
10
+ PeopleList.new(File.read(File.expand_path("../../../fixtures/people_lists/feasible.txt", __FILE__)))
11
+ end
12
+
13
+ it "can make selections" do
14
+ subject.make_selections
15
+ expect(subject.selections.size).to eq people_list.length
16
+ end
17
+ end
18
+
19
+ context "with an infeasible people list" do
20
+ let :people_list do
21
+ PeopleList.new(File.read(File.expand_path("../../../fixtures/people_lists/infeasible.txt", __FILE__)))
22
+ end
23
+
24
+ it "cannot make selections" do
25
+ expect{subject.make_selections}.to raise_error ArgumentError
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,37 @@
1
+ module RubyQuiz2
2
+ describe SelectionRules do
3
+
4
+ it "does not let a person be their own santa" do
5
+ p1 = double(Person)
6
+ p2 = double(Person)
7
+ expect(p1).to receive(:==).with(p2).and_return(true)
8
+ expect(subject.valid?(p1, p2)).to eq false
9
+ end
10
+
11
+ it "does not let a family members select each other" do
12
+ santa = double(Person)
13
+ giftee = double(Person)
14
+ expect(santa).to receive(:related_to?).with(giftee).and_return(true)
15
+ expect(subject.valid?(santa, giftee)).to eq false
16
+ end
17
+
18
+ describe "#valid_set?" do
19
+ it "does not multiple people to have the same giftee" do
20
+ s1 = double(Person)
21
+ s2 = double(Person)
22
+ g1 = double(Person)
23
+ sel1 = Selection.new(s1, g1)
24
+ sel2 = Selection.new(s2, g1)
25
+ expect(subject.valid_set?([sel1, sel2])).to eq false
26
+ end
27
+ it "is valid in the basic case" do
28
+ p1 = double(Person)
29
+ p2 = double(Person)
30
+ sel1 = Selection.new(p1, p2)
31
+ sel2 = Selection.new(p2, p1)
32
+ expect(subject.valid_set?([sel1, sel2])).to eq true
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ module RubyQuiz2
2
+ describe Selection do
3
+
4
+ let :santa do
5
+ double(Person)
6
+ end
7
+
8
+ let :giftee do
9
+ double(Person)
10
+ end
11
+
12
+ subject do
13
+ described_class.new santa, giftee
14
+ end
15
+
16
+ context "when the santa and giftee are valid for a selection" do
17
+ it "sets the santa" do
18
+ expect(subject.santa).to eq santa
19
+ end
20
+
21
+ it "sets the giftee" do
22
+ expect(subject.giftee).to eq giftee
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module RubyQuiz2
2
+ describe SelectionsPrinter do
3
+
4
+ let :p1 do
5
+ Person.new "Luke Skywalker", "luke@theforce.net"
6
+ end
7
+
8
+ let :p2 do
9
+ Person.new "Toula Portokalos", "toula@manhunter.org"
10
+ end
11
+
12
+ let :selections do
13
+ [
14
+ Selection.new(p1, p2),
15
+ Selection.new(p2, p1)
16
+ ]
17
+ end
18
+
19
+ it "prints the selections" do
20
+ # expect{subject.print(selections)}
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ require "rspec/context/private"
2
+
3
+ require "ruby_quiz_2"
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_quiz_2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sean Devine
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-context-private
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: 'Solution to ruby quiz #1 @ http://rubyquiz.com/quiz2.html'
84
+ email:
85
+ - barelyknown@icloud.com
86
+ executables:
87
+ - ruby_quiz_2
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/ruby_quiz_2
98
+ - lib/ruby_quiz_2.rb
99
+ - lib/ruby_quiz_2/people_list.rb
100
+ - lib/ruby_quiz_2/person.rb
101
+ - lib/ruby_quiz_2/selection.rb
102
+ - lib/ruby_quiz_2/selection_party.rb
103
+ - lib/ruby_quiz_2/selection_rules.rb
104
+ - lib/ruby_quiz_2/selections_printer.rb
105
+ - lib/ruby_quiz_2/version.rb
106
+ - ruby_quiz_2.gemspec
107
+ - spec/fixtures/people_lists/feasible.txt
108
+ - spec/fixtures/people_lists/infeasible.txt
109
+ - spec/lib/ruby_quiz_2/people_list_spec.rb
110
+ - spec/lib/ruby_quiz_2/person_spec.rb
111
+ - spec/lib/ruby_quiz_2/selection_party_spec.rb
112
+ - spec/lib/ruby_quiz_2/selection_rules_spec.rb
113
+ - spec/lib/ruby_quiz_2/selection_spec.rb
114
+ - spec/lib/ruby_quiz_2/selections_printer_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: ''
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: 'Solution to ruby quiz #1 @ http://rubyquiz.com/quiz2.html'
140
+ test_files:
141
+ - spec/fixtures/people_lists/feasible.txt
142
+ - spec/fixtures/people_lists/infeasible.txt
143
+ - spec/lib/ruby_quiz_2/people_list_spec.rb
144
+ - spec/lib/ruby_quiz_2/person_spec.rb
145
+ - spec/lib/ruby_quiz_2/selection_party_spec.rb
146
+ - spec/lib/ruby_quiz_2/selection_rules_spec.rb
147
+ - spec/lib/ruby_quiz_2/selection_spec.rb
148
+ - spec/lib/ruby_quiz_2/selections_printer_spec.rb
149
+ - spec/spec_helper.rb
150
+ has_rdoc: