drawy 0.0.2

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: e8b0b6bd2c7117dfeae351bc9c26f2925f10cddc
4
+ data.tar.gz: dea46241155f3594e67a864e0ba5352a23f231f2
5
+ SHA512:
6
+ metadata.gz: 79ee0b06b46993b85b4532830c2b791ad5e56d8e96d727e90a3b28d3074ad98fdebdc064ac56f570bf6f5f69df5eb0048d741ffe070019b25651b777570b41b6
7
+ data.tar.gz: be679cb029c8ff2201c6d6333f4075dd94328b30ccf394f17064963723cdf0c1e50c76e91bcb0b01e7964cd3cf2039232de7ad75bb6af950d08f138c5d6045d0
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 2.1.3@drawy --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in drawy.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Neil Rosenstech
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,43 @@
1
+ # Drawy
2
+
3
+ Just a CLI application written in Ruby
4
+
5
+ ## Installation
6
+
7
+ Just execute:
8
+
9
+ $ gem install drawy
10
+
11
+ ## Usage
12
+
13
+ Require the gem:
14
+
15
+ ```ruby
16
+ require 'drawy'
17
+ ```
18
+
19
+ And run Drawy:
20
+
21
+ ```ruby
22
+ Drawy.run
23
+ ```
24
+
25
+ ## Running the tests
26
+
27
+ Just clone this repo and execute:
28
+
29
+ $ rake test
30
+
31
+ ## Alternative installation/usage
32
+
33
+ Clone the repo and execute:
34
+
35
+ $ bundle
36
+
37
+ Make drawy executable:
38
+
39
+ $ chmod a+x bin/drawy.rb
40
+
41
+ Then run it:
42
+
43
+ $ ruby -Ilib ./bin/drawy
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.test_files = FileList['test/test*.rb']
7
+ t.verbose = true
8
+ end
9
+
data/bin/drawy ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'drawy'
4
+
5
+ Drawy.run
data/drawy-0.0.1.gem ADDED
Binary file
data/drawy.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'drawy/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "drawy"
8
+ spec.version = Drawy::VERSION
9
+ spec.authors = ["Neil Rosenstech"]
10
+ spec.email = ["tohellandback@hotmail.fr"]
11
+ spec.summary = %q{Drawy makes it easy to setup a "drawing lot" for christmas or any particular occasion}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/Raindal/drawy"
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest", "~> 5.4.3"
24
+ end
@@ -0,0 +1,68 @@
1
+ module Drawy
2
+ class Application
3
+
4
+ # To expand the application, just add an entry in the menu
5
+ # and write the corresponding method
6
+ # The entry is of form ['Text to display', :method_to_route_to]
7
+
8
+ MENU = [
9
+ ['Register someone for the drawing', :register],
10
+ ['Proceed to drawing', :draw],
11
+ ['Check someone\'s result', :check],
12
+ ['Exit', :exit]
13
+ ]
14
+
15
+ class << self
16
+ def start
17
+ @menu_numbers = *(1..MENU.size)
18
+ route_for(menu)
19
+ start
20
+ end
21
+
22
+ def menu
23
+ Drawy::IO.show 'What would you like to do?'
24
+ MENU.each_with_index { |a, i| Drawy::IO.show "#{i+1}. #{a.first}" }
25
+ Drawy::IO.accept :integer, "Enter the number corresponding to the action you want to perform: #{@menu_numbers}"
26
+ end
27
+
28
+ def route_for(number = nil)
29
+ return send(MENU[number-1][1]) if @menu_numbers.include?(number)
30
+ raise Drawy::InputError, "Please choose a number in #{@menu_numbers}"
31
+ end
32
+
33
+ def register
34
+ name = Drawy::IO.accept :string, 'Enter the name of the person you want to register:'
35
+ partner_name = Drawy::IO.accept :string, 'Enter the name of his/her partner (leave blank if the person is single):'
36
+ Drawy::Validator.validate_uniqueness_of(name, partner_name)
37
+ Drawy::Engine.add_people [name, partner_name]
38
+ end
39
+
40
+ def draw
41
+ Drawy::Validator.validate_people_format
42
+ Drawy::IO.show 'Drawing...'
43
+ Drawy::Engine.draw
44
+ Drawy::IO.show 'Done! You can now check everyone\'s result.'
45
+ end
46
+
47
+ def check
48
+ if Drawy::Engine.result.nil?
49
+ raise Drawy::InputError, 'You must proceed to drawing before checking someone'
50
+ end
51
+
52
+ name = Drawy::IO.accept :string, 'Enter the name of the person you want to check:'
53
+
54
+ if name.empty?
55
+ raise Drawy::InputError, 'Entered name is empty... ignoring.'
56
+ end
57
+
58
+ result = Drawy::Engine.result[name.downcase]
59
+ Drawy::IO.show "#{name.capitalize} should buy #{result.capitalize} a gift."
60
+ end
61
+
62
+ def exit
63
+ Drawy::IO.show 'See you soon'
64
+ Kernel.exit
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,72 @@
1
+ module Drawy
2
+ class Engine
3
+ class << self
4
+ attr_accessor :people, :result, :couples
5
+
6
+ def add_people(names)
7
+ self.people ||= []
8
+ self.couples ||= {}
9
+
10
+ names.delete_if(&:empty?)
11
+ names.map!(&:downcase)
12
+
13
+ couples[names.first] = names.last if names.size == 2
14
+ self.people += names
15
+ end
16
+
17
+ def draw(options = {})
18
+ # shuffle: false option is used for testing purposes
19
+ people.shuffle! unless options[:shuffle] == false
20
+ list = build_list
21
+
22
+ # Here build_list returns a list like so: %w(john dan anna camille)
23
+ # Now we want to have direct access to a person's associated person so that
24
+ # looking up the result for "john" returns "dan" without having to go through the array for every requested name
25
+ # Therefore we need result to be { 'john' => 'dan', 'dan' => 'anna', 'anna' => 'camille', 'camille' => 'john' }
26
+ self.result = Hash[list.zip(list.rotate)]
27
+ end
28
+
29
+ # This probably deserves a bit of insight on what's going on
30
+ # Basically we just build a list from people names keeping the order (note that people array has been shuffled)
31
+ # If we encounter a person following its partner we just take the next person first and add the partner after
32
+ #
33
+ # Example:
34
+ # We have %w(john anna dan camille henrik) in names and we have { 'john' => 'anna' } in couples
35
+ # Step 1: remove john and put him in the list => list = %w(john)
36
+ # Step 2: remove anna, she's in couple with john so remove dan and put him in the list => list = %w(john dan)
37
+ # Step 3: now anna is not in couple with dan so add her to the list => list = %w(john dan anna)
38
+ # Step 4: remove camille and put her in the list => list = %w(john dan anna camille)
39
+ # Step 5: remove henrik and put him in the list => list = %w(john dan anna camille henrik)
40
+ # An extra step is sometimes needed if the last person is in couple with the previous one because
41
+ # there's nobody left to add in between the two so we just arbitrarily put him at position 1
42
+ #
43
+ # The complexity is O(n), n representing the number of people
44
+
45
+ def build_list
46
+ list = []
47
+ names = people.dup
48
+
49
+ while names.any?
50
+ if is_couple?(list[-1], names[0])
51
+ aside = names.shift
52
+ break if names.empty?
53
+ list << names.shift
54
+ list << aside
55
+ aside = nil
56
+ else
57
+ list << names.shift
58
+ end
59
+ end
60
+
61
+ list.insert(1, aside) if aside
62
+
63
+ list
64
+ end
65
+
66
+ def is_couple?(x, y)
67
+ return false if [x, y].include?(nil)
68
+ couples[x] == y || couples[y] == x
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ module Drawy
2
+ class Error < StandardError; end
3
+ class InputError < Error; end
4
+ class ValidationError < Error; end
5
+ end
data/lib/drawy/io.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Drawy
2
+ class IO
3
+ class << self
4
+ def show(string)
5
+ puts "\n#{string}"
6
+ end
7
+
8
+ def accept(type, string)
9
+ print "\n#{string} "
10
+ return $stdin.gets.to_i if type == :integer
11
+ $stdin.gets.chomp
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module Drawy
2
+ class Validator
3
+ class << self
4
+ def validate_uniqueness_of(*args)
5
+ return if Drawy::Engine.people.nil?
6
+ faulty_names = Drawy::Engine.people & args
7
+ return if faulty_names.empty?
8
+ raise Drawy::ValidationError, "The following name(s) already exist(s): #{faulty_names}"
9
+ end
10
+
11
+ def validate_people_format
12
+ people = Drawy::Engine.people
13
+ if people.nil? || people.empty? || people.size < 2
14
+ raise Drawy::ValidationError, 'You must register at least 2 people or couples before drawing'
15
+ # A couple plus one person creates a triangle making the distribution impossible given the rules
16
+ # We either end up with one person in couple buying a gift for its partner or the 3rd
17
+ # person receiving 2 gifts and someone receiving none
18
+ elsif couple_plus_one?(people, Drawy::Engine.couples)
19
+ raise Drawy::ValidationError, 'The current pool only contains a couple and one person, please add more people'
20
+ end
21
+ end
22
+
23
+ def couple_plus_one?(people, couples)
24
+ return true if people.size == 3 && couples.keys.size == 1
25
+ false
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Drawy
2
+ VERSION = "0.0.2"
3
+ end
data/lib/drawy.rb ADDED
@@ -0,0 +1,23 @@
1
+ require 'optparse'
2
+ require 'drawy/version'
3
+ require 'drawy/application'
4
+
5
+ module Drawy
6
+ autoload :Engine, 'drawy/engine'
7
+ autoload :IO, 'drawy/io'
8
+ autoload :Validator, 'drawy/validator'
9
+ autoload :Error, 'drawy/error'
10
+ autoload :InputError, 'drawy/error'
11
+ autoload :ValidationError, 'drawy/error'
12
+
13
+ class << self
14
+ def run
15
+ Drawy::Application.start
16
+ rescue Drawy::Error => e
17
+ $stderr.puts "#------------ #{e.message} ------------#"
18
+ run
19
+ rescue StandardError => e
20
+ abort "#{e.backtrace.join("\n")}\n\n#{e.class} - #{e.message}\n\nSomething went wrong."
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,95 @@
1
+ require 'test_helper'
2
+
3
+ class TestEngine < Minitest::Test
4
+ def setup
5
+ Drawy::Engine.people = nil
6
+ Drawy::Engine.couples = nil
7
+ Drawy::Engine.result = nil
8
+ end
9
+
10
+ def test_add_people
11
+ x, y, z = ['john', 'anna'], ['dan'], ['camille']
12
+
13
+ Drawy::Engine.add_people(x)
14
+ Drawy::Engine.add_people(y)
15
+ Drawy::Engine.add_people(z)
16
+
17
+ assert_equal x + y + z, Drawy::Engine.people
18
+ end
19
+
20
+ def test_add_people_builds_couples
21
+ x, y = ['john', 'anna'], ['dan', 'camille']
22
+
23
+ Drawy::Engine.add_people(x)
24
+ Drawy::Engine.add_people(y)
25
+
26
+ # Couples should be of form { 'john' => 'anna', 'dan' => 'camille' }
27
+ assert_equal 'anna', Drawy::Engine.couples['john']
28
+ assert_equal 'camille', Drawy::Engine.couples['dan']
29
+ end
30
+
31
+ def test_add_people_downcases_names
32
+ x = ['John']
33
+
34
+ Drawy::Engine.add_people(x)
35
+
36
+ assert_equal 'john', Drawy::Engine.people.first
37
+ end
38
+
39
+ def test_add_people_deletes_empty_names
40
+ x = ['john', '']
41
+
42
+ Drawy::Engine.add_people(x)
43
+
44
+ assert_equal ['john'], Drawy::Engine.people
45
+ end
46
+
47
+ def test_draw
48
+ Drawy::Engine.people = %w(john anna dan camille)
49
+ Drawy::Engine.couples = { 'john' => 'anna' }
50
+
51
+ Drawy::Engine.draw(shuffle: false)
52
+
53
+ assert_equal({ 'john' => 'dan', 'dan' => 'anna', 'anna' => 'camille', 'camille' => 'john' }, Drawy::Engine.result)
54
+ end
55
+
56
+ def test_build_list
57
+ scenarii.each do |people, couples, expectation|
58
+ Drawy::Engine.people = people
59
+ Drawy::Engine.couples = couples
60
+
61
+ # Go to build_list method definition for a more detailed explanation
62
+ # of where those expectations are coming from
63
+ assert_equal expectation, Drawy::Engine.build_list
64
+ end
65
+ end
66
+
67
+ def test_is_couple?
68
+ Drawy::Engine.couples = { 'john' => 'anna' }
69
+
70
+ assert_equal true, Drawy::Engine.is_couple?('john', 'anna')
71
+ assert_equal true, Drawy::Engine.is_couple?('anna', 'john')
72
+ assert_equal false, Drawy::Engine.is_couple?('dan', 'camille')
73
+ assert_equal false, Drawy::Engine.is_couple?('john', nil)
74
+ end
75
+
76
+ private
77
+
78
+ # Different scenarii to test edge cases
79
+ # Each case is of form [people, couples, expectation]
80
+
81
+ def scenarii
82
+ [
83
+ # couple at the beginning
84
+ [%w(john anna dan camille), { 'john' => 'anna' }, %w(john dan anna camille)],
85
+ # couple at the end
86
+ [%w(john anna dan camille), { 'dan' => 'camille' }, %w(john camille anna dan)],
87
+ # 2 couples only
88
+ [%w(john anna dan camille), { 'john' => 'anna', 'dan' => 'camille' }, %w(john dan anna camille)],
89
+ # 3 couples only
90
+ [%w(john anna dan camille henrik luna), { 'john' => 'anna', 'dan' => 'camille', 'henrik' => 'luna' }, %w(john luna dan anna camille henrik)],
91
+ # no couple
92
+ [%w(john anna dan camille henrik luna), {}, %w(john anna dan camille henrik luna)]
93
+ ]
94
+ end
95
+ end
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/unit'
3
+ require 'minitest/pride'
4
+ require 'drawy'
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ class TestValidator < Minitest::Test
4
+ def setup
5
+ Drawy::Engine.people = nil
6
+ end
7
+
8
+ def test_validate_uniqueness_of_unique_names
9
+ assert_nil Drawy::Validator.validate_uniqueness_of
10
+ assert_nil Drawy::Validator.validate_uniqueness_of('john', 'anna')
11
+
12
+ Drawy::Engine.people = %w(dan camille henrik)
13
+
14
+ assert_nil Drawy::Validator.validate_uniqueness_of('john', 'anna')
15
+ end
16
+
17
+ def test_validate_uniqueness_of_used_names
18
+ Drawy::Engine.people = ['john']
19
+
20
+ assert_raises Drawy::ValidationError do
21
+ Drawy::Validator.validate_uniqueness_of('john', 'anna')
22
+ end
23
+ end
24
+
25
+ def test_validate_people_format
26
+ [nil, [], ['john']].each do |people_value|
27
+ Drawy::Engine.people = people_value
28
+
29
+ assert_raises Drawy::ValidationError do
30
+ Drawy::Validator.validate_people_format
31
+ end
32
+ end
33
+
34
+ Drawy::Engine.people = %w(john anna dan)
35
+ Drawy::Engine.couples = { 'john' => 'anna' }
36
+
37
+ assert_raises Drawy::ValidationError do
38
+ Drawy::Validator.validate_people_format
39
+ end
40
+ end
41
+
42
+ def test_couple_plus_one?
43
+ people = %w(john anna dan)
44
+ couples = { 'john' => 'anna' }
45
+
46
+ assert_equal true, Drawy::Validator.couple_plus_one?(people, couples)
47
+
48
+ people = %w(john anna dan camille)
49
+ couples = { 'john' => 'anna' }
50
+
51
+ assert_equal false, Drawy::Validator.couple_plus_one?(people, couples)
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drawy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Neil Rosenstech
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-17 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.4.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.4.3
55
+ description: Drawy makes it easy to setup a "drawing lot" for christmas or any particular
56
+ occasion
57
+ email:
58
+ - tohellandback@hotmail.fr
59
+ executables:
60
+ - drawy
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rvmrc"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/drawy
71
+ - drawy-0.0.1.gem
72
+ - drawy.gemspec
73
+ - lib/drawy.rb
74
+ - lib/drawy/application.rb
75
+ - lib/drawy/engine.rb
76
+ - lib/drawy/error.rb
77
+ - lib/drawy/io.rb
78
+ - lib/drawy/validator.rb
79
+ - lib/drawy/version.rb
80
+ - test/test_engine.rb
81
+ - test/test_helper.rb
82
+ - test/test_validator.rb
83
+ homepage: https://github.com/Raindal/drawy
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.2.2
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Drawy makes it easy to setup a "drawing lot" for christmas or any particular
107
+ occasion
108
+ test_files:
109
+ - test/test_engine.rb
110
+ - test/test_helper.rb
111
+ - test/test_validator.rb