grafter 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 33e72971937d31f4579593fc9f055c694074a1aa
4
+ data.tar.gz: e11a1cbb8bf0cc59a45d3ebf6f0abf35a1441c77
5
+ SHA512:
6
+ metadata.gz: b734265729d88bfaa5a68943eee4393a14d2b660161092fdf68926ea9a494fb140c7ef5928c34dd99c9350c54098b632b4e8141009d3c4172e76b992e389e3d3
7
+ data.tar.gz: b676445bea95914d8a1e76c3c6c252df0737e2a4d4247cc036977a2ecf4678a629efa3b9a551d840ce638f485328cd5c7c3a5558a1560213af947bae79caf906
@@ -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/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in grafter.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 OSA Shunsuke
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.
@@ -0,0 +1,29 @@
1
+ # Grafter
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'grafter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install grafter
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 'grafter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "grafter"
8
+ spec.version = Grafter::VERSION
9
+ spec.authors = ["OSA Shunsuke"]
10
+ spec.email = ["hhelibebcnofnenamg@gmail.com"]
11
+ spec.description = %q{This gem bundles layered instances to tree structured instances.}
12
+ spec.summary = %q{Bundle layered instances to tree structured instances}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,5 @@
1
+ require "grafter/version"
2
+ require "grafter/core_ext"
3
+
4
+ module Grafter
5
+ end
@@ -0,0 +1 @@
1
+ require "grafter/core_ext/array"
@@ -0,0 +1,29 @@
1
+ class Array
2
+ def graft(args={})
3
+ branch = args[:branch].to_sym
4
+ operator = args[:operator] && args[:operator].to_sym || "===".to_sym
5
+ check_response_in_graft(branch,operator)
6
+
7
+ base_trees = self
8
+ base_trees.each do |base_tree|
9
+ graftable_trees = base_trees.select{|obj| base_tree.send(operator,obj) && !base_tree.equal?(obj) }
10
+ graftable_trees.each do |graftable_tree|
11
+ base_tree.send(branch).concat(graftable_tree.send(branch))
12
+ base_trees.delete(graftable_tree)
13
+ end
14
+ end
15
+
16
+ base_trees.each{|base_tree| yield(base_tree.send(branch)) } if block_given?
17
+ base_trees
18
+ end
19
+
20
+ private
21
+
22
+ def check_response_in_graft(*methods)
23
+ methods.each do |method|
24
+ self.each do |obj|
25
+ raise NoMethodError, "#{obj.class}##{method} is not defined" unless obj.respond_to?(method)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Grafter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,55 @@
1
+ require "spec_helper"
2
+
3
+ describe Array do
4
+ describe "Array#graft" do
5
+ context "depth = 1" do
6
+ before do
7
+ @samples = []
8
+ @samples << Sample.new("a1","b1")
9
+ @samples << Sample.new("a1","b2")
10
+ @samples << Sample.new("a2","b1")
11
+ end
12
+
13
+ it "should merge elements in receiver array" do
14
+ samples = @samples.graft(operator: :===, branch: :children)
15
+ expect(samples.size).to eq(2)
16
+ expect(samples[0].children.size).to eq(2)
17
+ end
18
+
19
+ it "should raise error invalid operator" do
20
+ expect{@samples.graft(operator: :nomethod, branch: :chiildren)}.to raise_error(NoMethodError)
21
+ end
22
+
23
+ it "should raise error invalid branch" do
24
+ expect{@samples.graft}.to raise_error(NoMethodError)
25
+ expect{@samples.graft(branch: :nomethod)}.to raise_error(NoMethodError)
26
+ end
27
+ end
28
+
29
+ context "depth = 2" do
30
+ before do
31
+ @samples = []
32
+ @samples << Sample.new("a1","b1","c1")
33
+ @samples << Sample.new("a1","b1","c2")
34
+ @samples << Sample.new("a1","b2","c1")
35
+ @samples << Sample.new("a2","b1","c1")
36
+ @samples << Sample.new("a2","b1","c2")
37
+ end
38
+
39
+ it "should merge elements in receiver array recursively." do
40
+ samples = @samples.graft(branch: :children) do |children|
41
+ children.graft(branch: :children)
42
+ end
43
+
44
+ puts samples
45
+
46
+ expect(samples.size).to eq(2)
47
+ expect(samples[0].children.size).to eq(2)
48
+ expect(samples[0].children[0].children.size).to eq(2)
49
+ expect(samples[0].children[1].children.size).to eq(1)
50
+ expect(samples[1].children.size).to eq(1)
51
+ expect(samples[0].children[0].children.size).to eq(2)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,4 @@
1
+ require "spec_helper"
2
+
3
+ describe Grafter do
4
+ end
@@ -0,0 +1,23 @@
1
+ require "grafter"
2
+
3
+ class Sample
4
+ def initialize(*args)
5
+ raise unless args[0]
6
+ @value = args.shift
7
+ @children = [Sample.new(*args)] if args[0]
8
+ end
9
+
10
+ attr_accessor :value
11
+ attr_accessor :children
12
+
13
+ def inspect
14
+ children_status = @children ? ", children:#{@children}" : nil
15
+ "<Sample: value:#{@value}#{children_status}>"
16
+ end
17
+ alias_method :to_s, :inspect
18
+
19
+ def ===(other)
20
+ self.value == other.value
21
+ end
22
+ end
23
+
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grafter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - OSA Shunsuke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-16 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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: This gem bundles layered instances to tree structured instances.
56
+ email:
57
+ - hhelibebcnofnenamg@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - grafter.gemspec
69
+ - lib/grafter.rb
70
+ - lib/grafter/core_ext.rb
71
+ - lib/grafter/core_ext/array.rb
72
+ - lib/grafter/version.rb
73
+ - spec/grafter/core_ext/array_spec.rb
74
+ - spec/grafter_spec.rb
75
+ - spec/spec_helper.rb
76
+ homepage: ''
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.0.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Bundle layered instances to tree structured instances
100
+ test_files:
101
+ - spec/grafter/core_ext/array_spec.rb
102
+ - spec/grafter_spec.rb
103
+ - spec/spec_helper.rb