assigner 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG +0 -0
- data/LICENSE +19 -0
- data/README.md +0 -0
- data/Rakefile +8 -0
- data/assigner.gemspec +22 -0
- data/bin/assigner +37 -0
- data/lib/assigner.rb +38 -0
- data/lib/assigner/assignable.rb +42 -0
- data/test/test_assigner.rb +79 -0
- metadata +52 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 62e2b594896ccf0f2cf0bc4af71594b4ae5688cd
|
4
|
+
data.tar.gz: 436545b4c44de0e9db8b77c24f9512676be880c2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e5b0e4784cd6303139e4204d5cdff214d3553862af8c36f372f1851de05dc142c5652c2dc0667c3e8d5377b72d5e007a93203df55270a04652e88b0a97120237
|
7
|
+
data.tar.gz: 69919c7db1d4687be64ec1ea602b217f5a33ea528e08dad6c9f6634926a0680afc9cf59c7153beaa24807f3fe54aaae554ffbc4cace45e8353db812c929ecede
|
data/CHANGELOG
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2013 Agustin Pelliza
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
File without changes
|
data/Rakefile
ADDED
data/assigner.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'assigner'
|
3
|
+
s.version = '0.0.0'
|
4
|
+
s.executables << 'assigner'
|
5
|
+
s.date = '2013-05-29'
|
6
|
+
s.summary = "Assign!"
|
7
|
+
s.description = "A simple assigner gem"
|
8
|
+
s.authors = ["Agustin Pelliza"]
|
9
|
+
s.email = 'agpelliza@gmail.com'
|
10
|
+
s.homepage =
|
11
|
+
'http://rubygems.org/gems/assigner'
|
12
|
+
|
13
|
+
s.files = Dir[
|
14
|
+
"LICENSE",
|
15
|
+
"CHANGELOG",
|
16
|
+
"README.md",
|
17
|
+
"Rakefile",
|
18
|
+
"lib/**/*.rb",
|
19
|
+
"*.gemspec",
|
20
|
+
"test/*.*"
|
21
|
+
]
|
22
|
+
end
|
data/bin/assigner
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'assigner'
|
4
|
+
|
5
|
+
class Item
|
6
|
+
|
7
|
+
def initialize(attributes = {})
|
8
|
+
@properties = attributes
|
9
|
+
|
10
|
+
attributes.each do |key, value|
|
11
|
+
instance_variable_set "@#{key}".to_sym, value
|
12
|
+
end
|
13
|
+
|
14
|
+
self.class.send :define_method, 'to_original_input' do
|
15
|
+
output = ""
|
16
|
+
@properties.each do |key, value|
|
17
|
+
output << ", " unless output.empty?
|
18
|
+
output << "#{key}: #{value}"
|
19
|
+
end
|
20
|
+
output
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
input = File.open(ARGV[0])
|
26
|
+
|
27
|
+
items = []
|
28
|
+
input.each_line do |line|
|
29
|
+
line.strip!
|
30
|
+
items << Item.new(eval "{#{line}}") unless line.empty?
|
31
|
+
end
|
32
|
+
|
33
|
+
Assigner.assign_each_other!(items)
|
34
|
+
|
35
|
+
items.each do |item|
|
36
|
+
puts "#{item.to_original_input} >>>>> #{item.assignation.to_original_input}"
|
37
|
+
end
|
data/lib/assigner.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
class Assigner
|
2
|
+
|
3
|
+
def self.assign_each_other!(items, &block)
|
4
|
+
|
5
|
+
# Wrap Items to add assignation behavior
|
6
|
+
wrap(items, &block)
|
7
|
+
|
8
|
+
# Random assignation between items
|
9
|
+
items_copy = items.dup
|
10
|
+
items.each do |item|
|
11
|
+
item.assignation = items_copy.delete_at(rand(items_copy.size))
|
12
|
+
end
|
13
|
+
|
14
|
+
# Switch assignation if it's not allowed
|
15
|
+
items.each do |item|
|
16
|
+
unless item.assignation.can_be_assigned_to? item
|
17
|
+
candidates = items.select {|i| i.assignation.can_be_assigned_to?(item) && item.assignation.can_be_assigned_to?(i)}
|
18
|
+
raise if candidates.empty?
|
19
|
+
other = candidates[rand(candidates.size)]
|
20
|
+
temp = item.assignation
|
21
|
+
item.assignation = other.assignation
|
22
|
+
other.assignation = temp
|
23
|
+
finished = false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Wrap items to be assignable
|
29
|
+
def self.wrap(items, &block)
|
30
|
+
items.each do |item|
|
31
|
+
item.extend(Assigner::Assignable)
|
32
|
+
item.assign_conditions(&block) if block_given?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
private_class_method :wrap
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'assigner/assignable'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Assigner::Assignable
|
2
|
+
|
3
|
+
attr_accessor :assignation
|
4
|
+
|
5
|
+
def assign_conditions(&block)
|
6
|
+
instance_eval(&block)
|
7
|
+
end
|
8
|
+
|
9
|
+
def can_be_assigned_to?(other)
|
10
|
+
if have_conditions?
|
11
|
+
@assign_conditions.each do |condition|
|
12
|
+
return false unless send condition.to_sym, other
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
return true
|
17
|
+
end
|
18
|
+
|
19
|
+
def can_not_be_self
|
20
|
+
condition_name = 'different_object?'
|
21
|
+
self.class.send :define_method, condition_name do |data|
|
22
|
+
self != data
|
23
|
+
end
|
24
|
+
@assign_conditions ||= []
|
25
|
+
@assign_conditions << condition_name
|
26
|
+
end
|
27
|
+
|
28
|
+
def different(*method_names)
|
29
|
+
@assign_conditions ||= []
|
30
|
+
method_names.each do |name|
|
31
|
+
condition_name = "different_#{name}?"
|
32
|
+
self.class.send :define_method, condition_name do |data|
|
33
|
+
instance_variable_get("@#{name}") != data.instance_variable_get("@#{name}")
|
34
|
+
end
|
35
|
+
@assign_conditions << condition_name
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def have_conditions?
|
40
|
+
!@assign_conditions.nil? && !@assign_conditions.empty?
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'assigner'
|
3
|
+
|
4
|
+
class AssignerTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
class Item
|
7
|
+
|
8
|
+
def initialize(attributes = {})
|
9
|
+
attributes.each do |key, value|
|
10
|
+
instance_variable_set "@#{key}".to_sym, value
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_assign_each_other!
|
16
|
+
items = [
|
17
|
+
Item.new(name: "Item Name0", condition: "Condition0"),
|
18
|
+
Item.new(name: "Item Name1", condition: "Condition1")
|
19
|
+
]
|
20
|
+
|
21
|
+
Assigner.assign_each_other!(items)
|
22
|
+
|
23
|
+
assert_not_nil items[0].assignation
|
24
|
+
assert_not_nil items[1].assignation
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_assign_each_other_cant_be_self!
|
28
|
+
items = [
|
29
|
+
Item.new(name: "Item Name0", condition: "Condition0"),
|
30
|
+
Item.new(name: "Item Name1", condition: "Condition1")
|
31
|
+
]
|
32
|
+
|
33
|
+
Assigner.assign_each_other!(items) do
|
34
|
+
can_not_be_self
|
35
|
+
end
|
36
|
+
|
37
|
+
assert_equal items[0].assignation, items[1]
|
38
|
+
assert_equal items[1].assignation, items[0]
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_assign_each_other_with_different_condition!
|
42
|
+
items = [
|
43
|
+
Item.new(name: "Item Name0", condition: "Condition0"),
|
44
|
+
Item.new(name: "Item Name1", condition: "Condition1"),
|
45
|
+
Item.new(name: "Item Name2", condition: "Condition2"),
|
46
|
+
Item.new(name: "Item Name3", condition: "Condition0"),
|
47
|
+
Item.new(name: "Item Name4", condition: "Condition1")
|
48
|
+
]
|
49
|
+
|
50
|
+
Assigner.assign_each_other!(items) do
|
51
|
+
can_not_be_self
|
52
|
+
different 'condition'
|
53
|
+
end
|
54
|
+
|
55
|
+
items.each do |item|
|
56
|
+
assert_not_equal item.instance_variable_get('@condition'), item.assignation.instance_variable_get('@condition')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_assign_each_other_with_multiple_different_conditions!
|
61
|
+
items = [
|
62
|
+
Item.new(name: "Item Name0", condition1: "Condition0", condition2: "Condition0"),
|
63
|
+
Item.new(name: "Item Name1", condition1: "Condition1", condition2: "Condition1"),
|
64
|
+
Item.new(name: "Item Name2", condition1: "Condition2", condition2: "Condition2"),
|
65
|
+
Item.new(name: "Item Name3", condition1: "Condition0", condition2: "Condition0"),
|
66
|
+
Item.new(name: "Item Name4", condition1: "Condition1", condition2: "Condition1")
|
67
|
+
]
|
68
|
+
|
69
|
+
Assigner.assign_each_other!(items) do
|
70
|
+
can_not_be_self
|
71
|
+
different 'condition1', 'condition2'
|
72
|
+
end
|
73
|
+
|
74
|
+
items.each do |item|
|
75
|
+
assert_not_equal item.instance_variable_get('@condition1'), item.assignation.instance_variable_get('@condition1')
|
76
|
+
assert_not_equal item.instance_variable_get('@condition2'), item.assignation.instance_variable_get('@condition2')
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: assigner
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Agustin Pelliza
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple assigner gem
|
14
|
+
email: agpelliza@gmail.com
|
15
|
+
executables:
|
16
|
+
- assigner
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- LICENSE
|
21
|
+
- CHANGELOG
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/assigner/assignable.rb
|
25
|
+
- lib/assigner.rb
|
26
|
+
- assigner.gemspec
|
27
|
+
- test/test_assigner.rb
|
28
|
+
- bin/assigner
|
29
|
+
homepage: http://rubygems.org/gems/assigner
|
30
|
+
licenses: []
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 2.0.0
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: Assign!
|
52
|
+
test_files: []
|