set-combination 0.0.1

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.
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/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in set-combination.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'rake'
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jared Grippe
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,51 @@
1
+ # Set::Combination
2
+
3
+ Defined Set#* and returns a Set::Combination
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'set-combination'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install set-combination
18
+
19
+ ## Usage
20
+
21
+ # lets say you have a lawn mower with the following dimentions of state
22
+
23
+ class LawnMower
24
+
25
+ THROTTLE_STATES = Set[:on, :off]
26
+ CHOAK_STATES = Set[:open, :closed]
27
+ FUEL_STATES = Set[:full, :empty]
28
+
29
+ end
30
+
31
+ # and you want to know all the possible states this lawn mower could be in
32
+
33
+ THROTTLE_STATES * CHOAK_STATES * FUEL_STATES # =>
34
+ # #<Set::Combination
35
+ # #<Set: {:on, :open, :full}>,
36
+ # #<Set: {:on, :open, :empty}>,
37
+ # #<Set: {:on, :closed, :full}>,
38
+ # #<Set: {:on, :closed, :empty}>,
39
+ # #<Set: {:off, :open, :full}>,
40
+ # #<Set: {:off, :open, :empty}>,
41
+ # #<Set: {:off, :closed, :full}>,
42
+ # #<Set: {:off, :closed, :empty}>
43
+ # >
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,111 @@
1
+ require File.expand_path("../combination/version", __FILE__)
2
+
3
+ class Set::Combination < Set
4
+
5
+ InvalidDimentionError = Class.new(StandardError)
6
+
7
+ class << self
8
+ alias_method :[], :new
9
+ end
10
+
11
+ def initialize *dimentions
12
+ @dimentions = dimentions.clone
13
+
14
+ unless dimentions.all?{|dimention| dimention.respond_to? :to_a }
15
+ raise Set::Combination::InvalidDimentionError.new "all dimentions must respond to to_a"
16
+ end
17
+
18
+ members = dimentions.map(&:to_a).flatten
19
+ if members != members.uniq
20
+ raise Set::Combination::InvalidDimentionError.new "all members in a combination must be uniq"
21
+ end
22
+
23
+ combinations = dimentions.shift.to_a.map{|v| Set[v] }.to_set
24
+ combinations = dimentions.inject(combinations){ |combinations, dimention|
25
+ combinations.inject([]){|new_combinations, combination|
26
+ (new_combinations + dimention.to_a.map{ |member| combination + Set[member] }).to_set
27
+ }
28
+ }
29
+ super combinations
30
+ end
31
+
32
+ attr_reader :dimentions
33
+
34
+ def * dimention
35
+ dimentions = dimention.respond_to?(:dimentions) ? dimention.dimentions : [dimention]
36
+ self.class.new *(self.dimentions + dimentions)
37
+ end
38
+
39
+ def inspect
40
+ %(#<#{self.class} #{to_a.map(&:inspect)*', '}>)
41
+ end
42
+
43
+ end
44
+
45
+ class Set
46
+
47
+ def * set
48
+ Set::Combination[self] * set
49
+ end
50
+
51
+ end
52
+
53
+
54
+ # run the tests if it's this file that was executed
55
+ eval <<-RUBY, nil, $0, __LINE__+4 if $0 == __FILE__
56
+
57
+ require 'test/unit'
58
+
59
+ class SetCombinationTestCase < Test::Unit::TestCase
60
+ def test_combining
61
+
62
+ d1 = Set[:open, :closed]
63
+ d2 = Set[:on, :off]
64
+ d3 = Set[:full, :empty]
65
+
66
+ assert_equal(
67
+ Set[Set[:open, :on], Set[:open, :off], Set[:closed, :on], Set[:closed, :off]],
68
+ d1 * d2
69
+ )
70
+
71
+ assert_equal(
72
+ Set[Set[:open, :full], Set[:open, :empty], Set[:closed, :full], Set[:closed, :empty]],
73
+ d1 * d3
74
+ )
75
+
76
+ assert_equal(
77
+ Set[
78
+ Set[:open, :on, :full ],
79
+ Set[:open, :on, :empty],
80
+ Set[:open, :off, :full ],
81
+ Set[:open, :off, :empty],
82
+ Set[:closed, :on, :full ],
83
+ Set[:closed, :on, :empty],
84
+ Set[:closed, :off, :full ],
85
+ Set[:closed, :off, :empty]
86
+ ],
87
+ d1 * d2 * d3
88
+ )
89
+
90
+ assert_equal(
91
+ "all members in a combination must be uniq",
92
+ (begin
93
+ d1 * d1
94
+ rescue Set::Combination::InvalidDimentionError => e
95
+ e.message
96
+ end)
97
+ )
98
+
99
+ assert_equal(
100
+ "all dimentions must respond to to_a",
101
+ (begin
102
+ d1 * 10
103
+ rescue Set::Combination::InvalidDimentionError => e
104
+ e.message
105
+ end)
106
+ )
107
+
108
+ end
109
+ end
110
+
111
+ RUBY
@@ -0,0 +1,5 @@
1
+ require 'set'
2
+
3
+ class Set::Combination < Set
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/set/combination/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jared Grippe"]
6
+ gem.email = ["jared@deadlyicon.com"]
7
+ gem.description = %q{Defined Set#* and returns a Set::Combination}
8
+ gem.summary = %q{Defined Set#* and returns a Set::Combination}
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "set-combination"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Set::Combination::VERSION
17
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: set-combination
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jared Grippe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Defined Set#* and returns a Set::Combination
15
+ email:
16
+ - jared@deadlyicon.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/set/combination.rb
27
+ - lib/set/combination/version.rb
28
+ - set-combination.gemspec
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ segments:
42
+ - 0
43
+ hash: 4031737827828831329
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ segments:
51
+ - 0
52
+ hash: 4031737827828831329
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.24
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Defined Set#* and returns a Set::Combination
59
+ test_files: []