matchy_matchy 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rubocop_todo.yml ADDED
@@ -0,0 +1 @@
1
+ # Nothing to do 🍸
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at fauxparse@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in matchy_matchy.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ matchy_matchy (0.1.0)
5
+ cry (~> 0.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ cry (0.1.0)
11
+ diff-lcs (1.3)
12
+ docile (1.3.1)
13
+ json (2.1.0)
14
+ rake (10.5.0)
15
+ rspec (3.8.0)
16
+ rspec-core (~> 3.8.0)
17
+ rspec-expectations (~> 3.8.0)
18
+ rspec-mocks (~> 3.8.0)
19
+ rspec-core (3.8.0)
20
+ rspec-support (~> 3.8.0)
21
+ rspec-expectations (3.8.1)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-mocks (3.8.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.8.0)
27
+ rspec-support (3.8.0)
28
+ rspec_junit_formatter (0.4.1)
29
+ rspec-core (>= 2, < 4, != 2.12.0)
30
+ simplecov (0.16.1)
31
+ docile (~> 1.1)
32
+ json (>= 1.8, < 3)
33
+ simplecov-html (~> 0.10.0)
34
+ simplecov-html (0.10.2)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ bundler (~> 1.16.a)
41
+ matchy_matchy!
42
+ rake (~> 10.0)
43
+ rspec (~> 3.0)
44
+ rspec_junit_formatter (~> 0.4.1)
45
+ simplecov
46
+
47
+ BUNDLED WITH
48
+ 1.16.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Matt Powell
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,118 @@
1
+ # MatchyMatchy
2
+
3
+ A cute little implementation of the [Stable Match
4
+ algorithm](http://www.nrmp.org/), built by and for
5
+ the [New Zealand Improv Festival](https://nzif.info).
6
+
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/e9b46ac5ed632d5c83f6/maintainability)](https://codeclimate.com/github/fauxparse/matchy_matchy/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/e9b46ac5ed632d5c83f6/test_coverage)](https://codeclimate.com/github/fauxparse/matchy_matchy/test_coverage)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'matchy_matchy'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install matchy_matchy
24
+
25
+ ## Usage
26
+
27
+ The `MatchMaker` takes two sets of data:
28
+
29
+ * a `Hash` of `candidates` and their preferred targets
30
+ * a `Hash` of `targets` to tuples containing their preferred targets and the
31
+ number of candidates they're allowed to select
32
+
33
+ Calling `perform` on a `MatchMaker` yields an object that can return the
34
+ assignments keyed either `by_candidate` or `by_target`.
35
+
36
+ ```ruby
37
+ candidates = {
38
+ 'Arthur' => %w[City],
39
+ 'Sunny' => %w[City Mercy],
40
+ 'Joseph' => %w[City General Mercy],
41
+ 'Latha' => %w[Mercy City General],
42
+ 'Darrius' => %w[City Mercy General],
43
+ }
44
+
45
+ targets = {
46
+ 'Mercy' => [%w[Darrius Joseph], 2],
47
+ 'City' => [%w[Darrius Arthur Sunny Latha Joseph], 2],
48
+ 'General' => [%w[Darrius Arthur Joseph Latha], 2],
49
+ }
50
+
51
+ results = MatchyMatchy::MatchMaker.perform(
52
+ targets: targets,
53
+ candidates: candidates
54
+ )
55
+
56
+ results.by_candidate
57
+ # {
58
+ # 'Arthur' => 'City',
59
+ # 'Sunny' => nil,
60
+ # 'Joseph' => 'General',
61
+ # 'Latha' => 'General',
62
+ # 'Darrius' => 'City',
63
+ # }
64
+
65
+ results.by_target
66
+ # {
67
+ # 'Mercy' => []
68
+ # 'City' => ['Darrius', 'Arthur'],
69
+ # 'General' => ['Joseph', 'Latha'],
70
+ # }
71
+ ```
72
+
73
+ The `candidates` and `targets` can be pretty much any type of object. They can
74
+ even clash, which makes it safe to use things like database IDs unambiguously:
75
+ equal objects that are referenced in both collections will be treated separately
76
+ by the `MatchMaker`.
77
+
78
+ You can also specify the `targets` without an explicit capacity, in which case a
79
+ default capacity of `1` will be assumed:
80
+
81
+ ```ruby
82
+ targets = {
83
+ 'Mercy' => %w[Darrius Joseph],
84
+ 'City' => %w[Darrius Arthur Sunny Latha Joseph],
85
+ 'General' => %w[Darrius Arthur Joseph Latha],
86
+ }
87
+ ```
88
+
89
+ ## Development
90
+
91
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
92
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
93
+ prompt that will allow you to experiment.
94
+
95
+ To install this gem onto your local machine, run `bundle exec rake install`. To
96
+ release a new version, update the version number in `version.rb`, and then run
97
+ `bundle exec rake release`, which will create a git tag for the version, push
98
+ git commits and tags, and push the `.gem` file to
99
+ [rubygems.org](https://rubygems.org).
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome
104
+ [on GitHub](https://github.com/fauxparse/matchy_matchy).
105
+ This project is intended to be a safe, welcoming space for collaboration, and
106
+ contributors are expected to adhere to the [Contributor
107
+ Covenant](http://contributor-covenant.org) code of conduct.
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT
112
+ License](https://opensource.org/licenses/MIT).
113
+
114
+ ## Code of Conduct
115
+
116
+ Everyone interacting in the MatchyMatchy project’s codebases, issue trackers,
117
+ chat rooms and mailing lists is expected to follow the [code of
118
+ conduct](https://github.com/fauxparse/matchy_matchy/blob/master/CODE_OF_CONDUCT.md).
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 'matchy_matchy'
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(__FILE__)
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,19 @@
1
+ require 'matchy_matchy/version'
2
+ require 'matchy_matchy/candidate'
3
+ require 'matchy_matchy/target'
4
+ require 'matchy_matchy/match'
5
+ require 'matchy_matchy/matchbook'
6
+ require 'matchy_matchy/match_list'
7
+ require 'matchy_matchy/match_results'
8
+ require 'matchy_matchy/matchmaker'
9
+
10
+ # An implementation of the Stable Match algorithm.
11
+ #
12
+ # @see MatchyMatchy::MatchMaker
13
+ module MatchyMatchy
14
+ private_constant :Candidate
15
+ private_constant :Match
16
+ private_constant :Matchbook
17
+ private_constant :MatchList
18
+ private_constant :Target
19
+ end
@@ -0,0 +1,66 @@
1
+ module MatchyMatchy
2
+ # Represents a candidate in the Stable Match algorithm.
3
+ # Since the concept of a wrapped object with an ordered list of preferences
4
+ # is common to both candidates and targets, +Target+ inherits directly
5
+ # from +Candidate+, so when reading this documentation in that context,
6
+ # the words ‘candidate’ and ‘target’ should be reversed.
7
+ class Candidate
8
+ # Returns the wrapped object being represented by this candidate
9
+ attr_reader :object
10
+
11
+ # Returns an ordered list of this candidate’s preferred targets
12
+ attr_reader :preferences
13
+
14
+ # @param object The object being represented by this candidate.
15
+ def initialize(object)
16
+ @object = object
17
+ @preferences = []
18
+ end
19
+
20
+ # Indicates an order of preference for this candidate’s matches.
21
+ # The given targets will be appended to any existing preferences.
22
+ # Returns +self+ to maintain the semantics of `<<`.
23
+ #
24
+ # @param targets [Array] One or more targets, in order of preference.
25
+ def prefer(*targets)
26
+ preferences.push(*targets)
27
+ self
28
+ end
29
+
30
+ alias << prefer
31
+
32
+ # Returns true if this candidate has expressed a preference for
33
+ # the given target, false otherwise.
34
+ #
35
+ # @param target A target entity
36
+ # @return [Boolean] True if the candidate prefers the target
37
+ def include?(target)
38
+ preferences.include?(target)
39
+ end
40
+
41
+ # Returns true if two candidates’ objects are equal, false otherwise.
42
+ #
43
+ # @param other [MatchyMatchy::Candidate] Another object for comparison
44
+ # @return [Boolean] True if the two candidates are equal, false otherwise
45
+ def eql?(other)
46
+ object.eql?(other.object)
47
+ end
48
+
49
+ # Determine the index of the given target within the candidate’s
50
+ # preferences.
51
+ #
52
+ # @param target [MatchyMatchy::Candidate] A target entity to search for
53
+ # @return [Integer] The target’s position, or +nil+
54
+ def index(target)
55
+ preferences.find_index(target)
56
+ end
57
+
58
+ # Returns a string representation of the candidate.
59
+ # Delegates to the wrapped object.
60
+ #
61
+ # @return [String] a string for display
62
+ def to_s
63
+ object.to_s
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,77 @@
1
+ require 'cry'
2
+
3
+ module MatchyMatchy
4
+ # Represents a proposed match in the Stable Match algorithm.
5
+ class Match
6
+ include Comparable
7
+ include Cry
8
+
9
+ # Returns the candidate being matched.
10
+ attr_reader :candidate
11
+
12
+ # Returns the (zero-based) index of this match in the candidate’s
13
+ # preferences.
14
+ attr_reader :index
15
+
16
+ # Initializes the match with a candidate and an index.
17
+ #
18
+ # @param candidate [MatchyMatchy::Candidate] A candidate
19
+ # @param index [Integer] (Zero-based) index of preference to try.
20
+ # Defaults to 0, indicating the candidate’s first choice.
21
+ def initialize(candidate:, index: 0)
22
+ raise ArgumentError, "candidate does not have #{i+1} choices" \
23
+ if index > candidate.preferences.size
24
+
25
+ @candidate = candidate
26
+ @index = index
27
+ end
28
+
29
+ # Returns the target of the match by looking it up in the candidate’s
30
+ # preferences
31
+ #
32
+ # @return [MatchyMatchy::Target] The target object
33
+ def target
34
+ candidate.preferences[index]
35
+ end
36
+
37
+ # Returns true if two matches are equal.
38
+ # True if the two matches’ candidates and targets are identical,
39
+ # respectively.
40
+ #
41
+ # @return [Boolean] True if `self` and `other` are equal, false otherwise.
42
+ def eql?(other)
43
+ target.eql?(other.target) && candidate.eql?(other.candidate)
44
+ end
45
+
46
+ # Compares two matches, in order of preference within the target.
47
+ # For comparison to be meaningful, the two matches must have the same
48
+ # target, and both matches’ candidates must be preferred by the target.
49
+ #
50
+ # @param other [MatchyMatchy::Match] A second match for comparison
51
+ # @return [Integer]
52
+ # * negative if `self < other`
53
+ # * 0 if `self == other`
54
+ # * positive if `self > other`
55
+ def <=>(other)
56
+ raise ArgumentError, "matches must have the same target" \
57
+ unless target == other.target
58
+ target.index(candidate) <=> target.index(other.candidate)
59
+ end
60
+
61
+ # Returns true if the target also prefers the candidate.
62
+ #
63
+ # @return True if target’s preferences include this match’s candidate,
64
+ # false otherwise.
65
+ def mutual?
66
+ target.include?(candidate)
67
+ end
68
+
69
+ # Rejects the match.
70
+ # This may be handled by attaching a block with Cry’s `on` semantics:
71
+ #
72
+ # match.on(:reject) { do_something }
73
+ def reject!
74
+ publish!(:reject)
75
+ end
76
+ end
77
+ end