togostanza 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: db4a805741be024742284435dbd2345928a7e3cd
4
- data.tar.gz: f69d6d6677133877b7c06954083c92f7f6e341cd
3
+ metadata.gz: 042aa7b35b0f820ab3da8aedadd94a4fa126cfc5
4
+ data.tar.gz: a29f5b821a587df43170069b419d12bd361e0ae7
5
5
  SHA512:
6
- metadata.gz: c0701d33713f66135f7b86b06c4c81d74a9c2d376949c47fb95ed90c3194375845d858f3492600ec57d128b1447e4adc3f0a03669b2485ae8d521ebaffedc9c9
7
- data.tar.gz: 4c1353c47fbf04570e2916f4de343e8ffecdde76664607baa47fb876db10fa63e53b4174b41c97ded828d3c9b2b0e4a0c130176cd1393bb61888fbd6c1f9fc55
6
+ metadata.gz: ff4ac8d41f00f446fa7f263ee89374d59091dfff193704bf5afa1fc37d053312364a0ee83581b4f2a3bdabdeba286cf0aae8ec0d79769a41ffb2a9249ac1ac39
7
+ data.tar.gz: 68e7b02c61a96fe199f9bed1dc7e5080bc87f9e9201d2a8c73eef2f6fb3456ba4ea1b8473273e7236e453ec38447f830c8197ffd1f980fcfdf7136a3df0ae767
@@ -15,10 +15,12 @@ module TogoStanza::Stanza
15
15
  autoload :ExpressionMap, 'togostanza/stanza/expression_map'
16
16
  autoload :Markdown, 'togostanza/stanza/markdown'
17
17
  autoload :Querying, 'togostanza/stanza/querying'
18
+ autoload :Grouping, 'togostanza/stanza/grouping'
18
19
 
19
20
  class Base
20
21
  extend ExpressionMap::Macro
21
22
  include Querying
23
+ include Grouping
22
24
 
23
25
  define_expression_map :properties
24
26
  define_expression_map :resources
@@ -0,0 +1,28 @@
1
+ module TogoStanza::Stanza
2
+ module Grouping
3
+ module_function
4
+
5
+ def grouping(rows, *keys)
6
+ normalized_keys = keys.inject([]) {|acc, key|
7
+ acc + (key.is_a?(Hash) ? key.to_a : [[key, key]])
8
+ }
9
+
10
+ _grouping(rows, *normalized_keys)
11
+ end
12
+
13
+ def _grouping(rows, *keys)
14
+ (k1, a1), (k2, a2) = keys
15
+
16
+ return rows.map {|row| row[k1] } if keys.size == 1
17
+
18
+ rows.group_by {|row|
19
+ k1.is_a?(Array) ? row.values_at(*k1) : row[k1]
20
+ }.map {|vs, rs|
21
+ {
22
+ a1 => vs,
23
+ a2 => _grouping(rs, *keys.drop(1))
24
+ }
25
+ }
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module TogoStanza
2
- VERSION = '0.0.6'
2
+ VERSION = '0.0.7'
3
3
  end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe TogoStanza::Stanza::Grouping do
4
+ specify do
5
+ data = [
6
+ {cpd1: 'cpd1-1', cpd2: 'cpd2-1', ec: 'ec-1', gene: 'gene-1'},
7
+ {cpd1: 'cpd1-1', cpd2: 'cpd2-1', ec: 'ec-1', gene: 'gene-2'},
8
+ {cpd1: 'cpd1-1', cpd2: 'cpd2-1', ec: 'ec-2', gene: 'gene-3'},
9
+ {cpd1: 'cpd1-2', cpd2: 'cpd2-2', ec: 'ec-3', gene: 'gene-4'}
10
+ ]
11
+
12
+ TogoStanza::Stanza::Grouping.grouping(data, {[:cpd1, :cpd2] => :cpd}, :ec, :gene).should == [
13
+ {
14
+ :cpd => ["cpd1-1", "cpd2-1"],
15
+ :ec => [
16
+ {:ec => "ec-1", :gene => ["gene-1", "gene-2"]},
17
+ {:ec => "ec-2", :gene => ["gene-3"]}
18
+ ]
19
+ },
20
+ {
21
+ :cpd => ["cpd1-2", "cpd2-2"],
22
+ :ec => [
23
+ {:ec => "ec-3", :gene => ["gene-4"]}
24
+ ]
25
+ }
26
+ ]
27
+ end
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: togostanza
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keita Urashima
@@ -264,6 +264,7 @@ files:
264
264
  - lib/togostanza/stanza.rb
265
265
  - lib/togostanza/stanza/base.rb
266
266
  - lib/togostanza/stanza/expression_map.rb
267
+ - lib/togostanza/stanza/grouping.rb
267
268
  - lib/togostanza/stanza/markdown.rb
268
269
  - lib/togostanza/stanza/querying.rb
269
270
  - lib/togostanza/version.rb
@@ -281,6 +282,7 @@ files:
281
282
  - spec/features/rendering_spec.rb
282
283
  - spec/features/resource_spec.rb
283
284
  - spec/lib/togostanza/stanza/base_spec.rb
285
+ - spec/lib/togostanza/stanza/grouping_spec.rb
284
286
  - spec/spec_helper.rb
285
287
  - templates/provider/Gemfile.erb
286
288
  - templates/provider/config.ru.erb
@@ -332,4 +334,5 @@ test_files:
332
334
  - spec/features/rendering_spec.rb
333
335
  - spec/features/resource_spec.rb
334
336
  - spec/lib/togostanza/stanza/base_spec.rb
337
+ - spec/lib/togostanza/stanza/grouping_spec.rb
335
338
  - spec/spec_helper.rb