joinable_array 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +13 -0
- data/joinable_array.gemspec +23 -0
- data/lib/joinable_array/version.rb +3 -0
- data/lib/joinable_array.rb +120 -0
- data/test/joinable_array_test.rb +83 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Steven Noble
|
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,47 @@
|
|
1
|
+
# JoinableArray
|
2
|
+
|
3
|
+
A small gem to make it easier to do relational joins with arrays in ruby without requiring database calls.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'joinable_array'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install joinable_array
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
joinable_array offers a JoinableArray class which can be used as
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
things1 = JoinableArray(array1)
|
25
|
+
things2 = JoinableArray(array2)
|
26
|
+
|
27
|
+
outer_join = things1
|
28
|
+
.outer_join(things2) {|l,r| l.merge(r)}
|
29
|
+
|
30
|
+
inner_join = things1
|
31
|
+
.joins_on {|x| [x[0], x[1]]}
|
32
|
+
.inner_join(things2.joins_on {|x| [x.a, x.b]}) {|l,r| l.merge(r)}
|
33
|
+
|
34
|
+
left_join = things1
|
35
|
+
.joins_on {|x| [x[0], x[1]]}
|
36
|
+
.fills_with {|key| key[0 .. -1].concat([0,0,0])}
|
37
|
+
.left_join(things2.joins_on {|x| [x.a, x.b]}) {|l,r| l.merge(r)}
|
38
|
+
|
39
|
+
right_join = things1
|
40
|
+
.joins_on {|x| [x[0], x[1]]}
|
41
|
+
.right_join(things2
|
42
|
+
.joins_on {|x| [x.a, x.b]}
|
43
|
+
.fills_with {|key|, Obj.new(*key)}
|
44
|
+
) {|l,r| l.merge(r)}
|
45
|
+
```
|
46
|
+
|
47
|
+
The simplest join is `outer_join` which is a method on `JoinableArray` and requires another `JoinableArray` as its parameter and returns a 3rd `JoinableArray` which is the outer join of the original two `JoinableArray` instances. The elements of the returned `JoinableArray` is defined by the block passed to `outer_join`. The block accepts two parameters: the first is an element from the first `JoinableArray` and the second is from the other `JoinableArray`. The block returns the value of resulting `JoinableArray`
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'joinable_array/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "joinable_array"
|
8
|
+
spec.version = JoinableArray::VERSION
|
9
|
+
spec.authors = ["Steven Noble"]
|
10
|
+
spec.email = ["steven.noble@gmail.com"]
|
11
|
+
spec.description = %q{A way to do sql style joins with Array}
|
12
|
+
spec.summary = %q{joinable arrays}
|
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
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
class RelationalMethods
|
2
|
+
def self.inner_join(left_a, right_a, left_key, right_key, &block)
|
3
|
+
left, right = left_and_right(left_a, right_a, left_key, right_key)
|
4
|
+
join(left, right, left_a, &block)
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.left_join(left_a, right_a, left_key, right_key, fill, &block)
|
8
|
+
left, right = left_and_right(left_a, right_a, left_key, right_key)
|
9
|
+
right = fill_cube(right, left, &fill).sort_by {|x| x[:key]}
|
10
|
+
join(left, right, left_a, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.right_join(left_a, right_a, left_key, right_key, fill, &block)
|
14
|
+
left, right = left_and_right(left_a, right_a, left_key, right_key)
|
15
|
+
left = fill_cube(left, right, &fill).sort_by {|x| x[:key]}
|
16
|
+
join(left, right, left_a, &block)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.outer_join(left_a, right_a, left_key, right_key, left_fill, right_fill, &block)
|
20
|
+
left, right = left_and_right(left_a, right_a, left_key, right_key)
|
21
|
+
left = fill_cube(left, right, &left_fill).sort_by {|x| x[:key]}
|
22
|
+
right = fill_cube(right, left, &right_fill).sort_by {|x| x[:key]}
|
23
|
+
join(left, right, left_a, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.fill_cube(left, right, &block)
|
27
|
+
right_hash = right[idx = 0]
|
28
|
+
new_array = left.slice(0, 0)
|
29
|
+
previously_added = nil
|
30
|
+
left.each do |left_hash|
|
31
|
+
while right_hash && (right_hash[:key] <=> left_hash[:key]) < 0
|
32
|
+
if previously_added == nil || (right_hash[:key] <=> previously_added) > 0
|
33
|
+
previously_added = right_hash[:key]
|
34
|
+
new_array << {:key => right_hash[:key], :object => block ? block.call(right_hash[:key]) : nil}
|
35
|
+
end
|
36
|
+
right_hash = right[idx += 1]
|
37
|
+
end
|
38
|
+
new_array << left_hash
|
39
|
+
while right_hash && right_hash[:key] == left_hash[:key]
|
40
|
+
right_hash = right[idx += 1]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
while right_hash
|
44
|
+
if previously_added == nil || (right_hash[:key] <=> previously_added) > 0
|
45
|
+
previously_added = right_hash[:key]
|
46
|
+
new_array << {:key => right_hash[:key], :object => block ? block.call(right_hash[:key]) : nil}
|
47
|
+
end
|
48
|
+
right_hash = right[idx += 1]
|
49
|
+
end
|
50
|
+
new_array
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.join(left, right, prototype_container)
|
54
|
+
anchor = 0
|
55
|
+
new_array = prototype_container.slice(0,0)
|
56
|
+
left.each do |left_hash|
|
57
|
+
right_hash = right[anchor]
|
58
|
+
while right_hash && (right_hash[:key] <=> left_hash[:key]) < 0
|
59
|
+
right_hash = right[anchor += 1]
|
60
|
+
end
|
61
|
+
idx = anchor
|
62
|
+
while right_hash && right_hash[:key] == left_hash[:key]
|
63
|
+
new_array << yield(left_hash[:object], right_hash[:object])
|
64
|
+
right_hash = right[idx += 1]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
new_array
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.left_and_right(left_a, right_a, left_key, right_key)
|
71
|
+
left = left_a.map {|x| {:key => left_key ? left_key.call(x) : 0, :object => x}}.sort_by {|x| x[:key]}
|
72
|
+
right = right_a.map {|x| {:key => right_key ? right_key.call(x) : 0, :object => x}}.sort_by {|x| x[:key]}
|
73
|
+
[left, right]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
module Joinable
|
78
|
+
def inner_join(that, &block)
|
79
|
+
RelationalMethods.inner_join(self, that, join_key, that.join_key, &block)
|
80
|
+
end
|
81
|
+
|
82
|
+
def join_on(&block)
|
83
|
+
@join_key = block
|
84
|
+
self
|
85
|
+
end
|
86
|
+
|
87
|
+
def join_key
|
88
|
+
@join_key
|
89
|
+
end
|
90
|
+
|
91
|
+
def fills_with(&block)
|
92
|
+
@fill_row = block
|
93
|
+
self
|
94
|
+
end
|
95
|
+
|
96
|
+
def fill_row
|
97
|
+
@fill_row
|
98
|
+
end
|
99
|
+
|
100
|
+
def cross_join(that, &block)
|
101
|
+
RelationalMethods.inner_join(self, that, nil, nil, &block)
|
102
|
+
end
|
103
|
+
|
104
|
+
def left_join(that, &block)
|
105
|
+
RelationalMethods.left_join(self, that, join_key, that.join_key, that.fill_row, &block)
|
106
|
+
end
|
107
|
+
|
108
|
+
def right_join(that, &block)
|
109
|
+
RelationalMethods.right_join(self, that, join_key, that.join_key, fill_row, &block)
|
110
|
+
end
|
111
|
+
|
112
|
+
def outer_join(that, &block)
|
113
|
+
RelationalMethods.outer_join(self, that, join_key, that.join_key, fill_row, that.fill_row, &block)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
class JoinableArray < Array
|
118
|
+
include Joinable
|
119
|
+
end
|
120
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'lib/joinable_array'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class JoinableArrayTest < MiniTest::Unit::TestCase
|
5
|
+
def typeA(a,b,c,d)
|
6
|
+
{:a => a, :b => b, :c => c, :d => d}
|
7
|
+
end
|
8
|
+
|
9
|
+
def typeB(a,b,e,f)
|
10
|
+
{:a => a, :b => b, :e => e, :f => f}
|
11
|
+
end
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@a = JoinableArray.new([typeA(4,6,1,2), typeA(7,7,2,2), typeA(8,3,5,5), typeA(9,4,1,7)])
|
15
|
+
@b = JoinableArray.new([typeB(8,3,1,2), typeB(8,3,2,2), typeB(7,7,5,5), typeB(1,2,3,4)])
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_simple_join
|
19
|
+
c = @a.
|
20
|
+
join_on {|x| [x[:a], x[:b]]}.
|
21
|
+
inner_join(@b.join_on {|x| [x[:a], x[:b]]}) {|l,r| l.merge(r)}.
|
22
|
+
sort_by {|x| [x[:a], x[:b], x[:c], x[:d], x[:e], x[:f]]}
|
23
|
+
|
24
|
+
assert_equal(3, c.length)
|
25
|
+
assert_equal(@a[1].merge(@b[2]), c[0])
|
26
|
+
assert_equal(@a[2].merge(@b[0]), c[1])
|
27
|
+
assert_equal(@a[2].merge(@b[1]), c[2])
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_left_join
|
31
|
+
c = @a.
|
32
|
+
join_on {|x| [x[:a], x[:b]]}.
|
33
|
+
left_join(@b.
|
34
|
+
join_on {|x| [x[:a], x[:b]]}.
|
35
|
+
fills_with {|key| typeB(key[0], key[1], -1, -2)
|
36
|
+
}) {|l,r| l.merge(r)}.
|
37
|
+
sort_by {|x| [x[:a], x[:b], x[:c], x[:d], x[:e], x[:f]]}
|
38
|
+
|
39
|
+
assert_equal(5, c.length)
|
40
|
+
assert_equal(@a[0].merge({:e => -1, :f => -2}), c[0])
|
41
|
+
assert_equal(@a[1].merge(@b[2]), c[1])
|
42
|
+
assert_equal(@a[2].merge(@b[0]), c[2])
|
43
|
+
assert_equal(@a[2].merge(@b[1]), c[3])
|
44
|
+
assert_equal(@a[3].merge({:e => -1, :f => -2}), c[4])
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_outer_join
|
48
|
+
c = @a.
|
49
|
+
join_on {|x| [x[:a], x[:b]]}.
|
50
|
+
fills_with {|key| typeA(key[0], key[1], -1, -2)}.
|
51
|
+
outer_join(@b.
|
52
|
+
join_on {|x| [x[:a], x[:b]]}.
|
53
|
+
fills_with {|key| typeB(key[0], key[1], -3, -4)}
|
54
|
+
) {|l,r| l.merge(r)}.
|
55
|
+
sort_by {|x| [x[:a], x[:b], x[:c], x[:d], x[:e], x[:f]]}
|
56
|
+
|
57
|
+
assert_equal(6, c.length)
|
58
|
+
assert_equal(@b[3].merge({:c => -1, :d => -2}), c[0])
|
59
|
+
assert_equal(@a[0].merge({:e => -3, :f => -4}), c[1])
|
60
|
+
assert_equal(@a[1].merge(@b[2]), c[2])
|
61
|
+
assert_equal(@a[2].merge(@b[0]), c[3])
|
62
|
+
assert_equal(@a[2].merge(@b[1]), c[4])
|
63
|
+
assert_equal(@a[3].merge({:e => -3, :f => -4}), c[5])
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_cross_join
|
67
|
+
c = @a.cross_join(@b) {|l,r| l.merge(r)}
|
68
|
+
|
69
|
+
assert_equal(16, c.length)
|
70
|
+
d = []
|
71
|
+
@a.each do |a|
|
72
|
+
@b.each do |b|
|
73
|
+
d << a.merge(b)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
c.sort_by! {|x| [x[:a], x[:b], x[:c], x[:d], x[:e], x[:f]]}
|
78
|
+
d.sort_by! {|x| [x[:a], x[:b], x[:c], x[:d], x[:e], x[:f]]}
|
79
|
+
|
80
|
+
assert_equal(d, c)
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: joinable_array
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Steven Noble
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: A way to do sql style joins with Array
|
47
|
+
email:
|
48
|
+
- steven.noble@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- joinable_array.gemspec
|
59
|
+
- lib/joinable_array.rb
|
60
|
+
- lib/joinable_array/version.rb
|
61
|
+
- test/joinable_array_test.rb
|
62
|
+
homepage: ''
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.23
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: joinable arrays
|
87
|
+
test_files:
|
88
|
+
- test/joinable_array_test.rb
|