combination-extractor 0.9.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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +108 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/combination-extractor.gemspec +25 -0
- data/lib/combination_extractor.rb +20 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9dbf79f3e6301aab904e419b9c74d1dc5793bc90
|
4
|
+
data.tar.gz: be61611fac7c22f1b69b925da5f1f7f0f38e32d2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b12486f683e9b7f9f941c557fad7ee726a1198960aea601eb70f4e35494862964c95d696775915fafd7cdb3aff72b658992977027d1b5ba52a340698d92ebc9d
|
7
|
+
data.tar.gz: 9ffe50c1aab5e5d9395fd4d0432f961ab1237c5fc1176db63c233d3983485715c79d3513c1e5b17df2f2296ed4fcb4762754071f605a34a897b1e695a0d94612
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Hiroki Akiyama
|
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,108 @@
|
|
1
|
+
# CombinationExtractor
|
2
|
+
|
3
|
+
[](https://travis-ci.org/akiroom/combination-extractor)
|
4
|
+
|
5
|
+
Exctract keyed patterns hash to combination keyed hash.
|
6
|
+
|
7
|
+
Convert this hash:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
{
|
11
|
+
fruit: ['apple', 'orange'],
|
12
|
+
city: ['NewYork', 'London', 'Tokyo']
|
13
|
+
}
|
14
|
+
```
|
15
|
+
|
16
|
+
To this:
|
17
|
+
```ruby
|
18
|
+
[
|
19
|
+
{:fruit=>"apple", :city=>"NewYork"},
|
20
|
+
{:fruit=>"apple", :city=>"London"},
|
21
|
+
{:fruit=>"apple", :city=>"Tokyo"},
|
22
|
+
{:fruit=>"orange", :city=>"NewYork"},
|
23
|
+
{:fruit=>"orange", :city=>"London"},
|
24
|
+
{:fruit=>"orange", :city=>"Tokyo"}
|
25
|
+
]
|
26
|
+
```
|
27
|
+
|
28
|
+
## Installation
|
29
|
+
|
30
|
+
Add this line to your application's Gemfile:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
gem 'combination-extractor'
|
34
|
+
```
|
35
|
+
|
36
|
+
And then execute:
|
37
|
+
|
38
|
+
$ bundle
|
39
|
+
|
40
|
+
Or install it yourself as:
|
41
|
+
|
42
|
+
$ gem install combination-extractor
|
43
|
+
|
44
|
+
## Usage
|
45
|
+
|
46
|
+
**Example**
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
CombinationExtractor.extract({fruit: ['apple', 'orange'], city: ['NewYork', 'London', 'Tokyo']})
|
50
|
+
=> [{:fruit=>"apple", :city=>"NewYork"}, {:fruit=>"apple", :city=>"London"}, {:fruit=>"apple", :city=>"Tokyo"},
|
51
|
+
{:fruit=>"orange", :city=>"NewYork"}, {:fruit=>"orange", :city=>"London"}, {:fruit=>"orange", :city=>"Tokyo"}]
|
52
|
+
```
|
53
|
+
|
54
|
+
**Example for rspec**
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
def example_similar_to(user_hash)
|
58
|
+
user_hash[:birthday] == Time.new(1988, 12, 30) &&
|
59
|
+
user_hash[:gender] == :male
|
60
|
+
end
|
61
|
+
|
62
|
+
describe 'About example example_similar_to method' do
|
63
|
+
let(:nickname) { nil }
|
64
|
+
let(:gender) { nil }
|
65
|
+
let(:birthday) { nil }
|
66
|
+
let(:user) { { nickname: nickname, gender: gender, birthday: birthday } }
|
67
|
+
|
68
|
+
user_condition = {
|
69
|
+
nickname: ['John', 'Ken', 'Jessie'],
|
70
|
+
gender: [:female, :male],
|
71
|
+
birthday: [Time.new(1988, 12, 30), Time.now]
|
72
|
+
}
|
73
|
+
# Get 2*2*3 = 12 patterns
|
74
|
+
pattern_list = CombinationExtractor.extract(user_condition)
|
75
|
+
|
76
|
+
pattern_list.each do |pattern|
|
77
|
+
describe "user who is #{pattern.to_s}" do
|
78
|
+
# NOTE: Set pattern to lazy evaluated variable
|
79
|
+
pattern.each { |key, value| let(key) { value } }
|
80
|
+
|
81
|
+
should_value = pattern[:gender] == :male && pattern[:birthday] == Time.new(1988, 12, 30)
|
82
|
+
it "should return #{should_value}" do
|
83
|
+
expect(example_similar_to(user)).to eq(should_value)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
90
|
+
`bundle exec rspec` returns this:
|
91
|
+
|
92
|
+

|
93
|
+
|
94
|
+
## Development
|
95
|
+
|
96
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
97
|
+
|
98
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/akiroom/combination-extractor.
|
103
|
+
|
104
|
+
|
105
|
+
## License
|
106
|
+
|
107
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
108
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'combination_extractor'
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'combination_extractor'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'combination-extractor'
|
8
|
+
spec.version = CombinationExtractor::VERSION
|
9
|
+
spec.authors = ['Hiroki Akiyama']
|
10
|
+
spec.email = ['akiyama@akiroom.com']
|
11
|
+
|
12
|
+
spec.summary = 'Extract array to combination hash list.'
|
13
|
+
spec.description = 'Extract patterns array to hash list for combination.'
|
14
|
+
spec.homepage = 'https://github.com/akiroom/combination-extractor'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec', '~> 0'
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
module CombinationExtractor
|
3
|
+
VERSION = '0.9.0'
|
4
|
+
|
5
|
+
# Exctract keyed patterns hash to combination keyed hash.
|
6
|
+
# @param key_to_pattern key: title for values, value: [Array] various value to use making combination.
|
7
|
+
# @retun [Array] Combination keyed hash.
|
8
|
+
# @example
|
9
|
+
# CombinationExtractor.extract({fruit: ['apple', 'orange'], city: ['NewYork', 'London', 'Tokyo']})
|
10
|
+
# => [{:fruit=>"apple", :city=>"NewYork"}, {:fruit=>"apple", :city=>"London"}, {:fruit=>"apple", :city=>"Tokyo"},
|
11
|
+
# {:fruit=>"orange", :city=>"NewYork"}, {:fruit=>"orange", :city=>"London"}, {:fruit=>"orange", :city=>"Tokyo"}]
|
12
|
+
def self.extract(key_to_pattern)
|
13
|
+
return nil unless key_to_pattern
|
14
|
+
extracted = [nil].product(*(key_to_pattern.values))
|
15
|
+
extracted.map do |pattern|
|
16
|
+
pattern.shift
|
17
|
+
Hash[key_to_pattern.keys.zip(pattern)]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: combination-extractor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hiroki Akiyama
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-22 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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: rspec
|
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
|
+
description: Extract patterns array to hash list for combination.
|
56
|
+
email:
|
57
|
+
- akiyama@akiroom.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- combination-extractor.gemspec
|
72
|
+
- lib/combination_extractor.rb
|
73
|
+
homepage: https://github.com/akiroom/combination-extractor
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.5.1
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Extract array to combination hash list.
|
97
|
+
test_files: []
|