libciel 0.0.0.2 → 0.0.0.3

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: dc7e2c339c060855b8d8d5ff00c7ae2bd868a9bb
4
- data.tar.gz: 217d0b4d4f3b05e83eeba0ec0b0b006369bb4e76
3
+ metadata.gz: 4fa58b86ead83f8e76fad4c6c059365dcee1c1c2
4
+ data.tar.gz: 6554e526034c197a4ede4383e89326891f2b0d04
5
5
  SHA512:
6
- metadata.gz: 6d102875526fbcd3d656214c1a407e3ff943a048eaf1ef380d28cdf950a7e383a4bc739b47d7f3ba0baed6ff688a9465ada839ca027909a5e9cfb4e76d115d58
7
- data.tar.gz: 2180c8d14545ec9aeaa7cb2badd0c3c902608870dc85b8f75f244a71ddde8bc33459dad6c12cbf43b232346e2dc0e7d45d5e3976aba26f290e4fa04d4d8caf00
6
+ metadata.gz: a65062be26d7329516af9e9da16b041e130e68bbd2573fb5f6c433b1284e47e99e1d402f58973f3cb58b6f15a7a2af5b31350a4e7d00d5fe9f1a08c1db4a1c35
7
+ data.tar.gz: c0a1048afdc5b940e230ce6204c74970e7e580145df124bc19da37784a7bf596c1365c9e5372ddd2c0c9e4f6f1adfb13caaf6a2f08b2f481f29fb5e3532853d1
data/CHANGELOG.md CHANGED
@@ -1,4 +1,10 @@
1
1
  # ChangeLog
2
2
 
3
+ ## 0.0.0.3 (2014 Jan 24)
4
+ - Refined source structure.
5
+ - Kernel::zip doesn't modify original Array anymore.
6
+ - Removed Hash#patch, which is actually the same as Hash#merge. Thank you smackesey.
7
+ - Dropped Hash#exists_rec? and introduced Hash#fetch_nested.
8
+
3
9
  ## 0.0.0.2 (2014 Jan 22)
4
10
  - Initial release
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- - Copyright (c) 2013, T. Yamada
1
+ - Copyright (c) 2013, @cielavenir
2
2
 
3
3
  Redistribution and use in source and binary forms, with or without
4
4
  modification, are permitted provided that the following conditions are met:
data/README.md CHANGED
@@ -9,6 +9,10 @@
9
9
  * 2.2.1 and 2.2.2 are not working.
10
10
  * (Possibly) ironruby / macruby / topaz etc
11
11
 
12
- ## Notice
12
+ ## Note
13
13
  * **Ruby 2.0+ cannot handle dbd-sqlite3.**
14
14
  * jruby cannot handle dbi.
15
+
16
+ ## Warning
17
+ * This library is experimental. Incompatible API change might happen. You might want to include the source directly.
18
+ * None of my projects (cTouch (builder) / yomebrowser / picrawler) don't depend on this gem.
@@ -0,0 +1,20 @@
1
+ class Array
2
+ #Enumerates permutation of Array.
3
+ #Unlike Array#permutation, there are no duplicates in generated permutations.
4
+ #Instead, elements must be comparable.
5
+ def permutation2(n=self.size)
6
+ return to_enum(:permutation2,n) unless block_given?
7
+ return if n<0||self.size<n
8
+ a=self.sort
9
+ yield a.dup[0,n]
10
+ loop{
11
+ a=a[0,n]+a[n..-1].reverse
12
+ k=(a.size-2).downto(0).find{|i|a[i]<a[i+1]}
13
+ break if !k
14
+ l=(a.size-1).downto(k+1).find{|i|a[k]<a[i]}
15
+ a[k],a[l]=a[l],a[k]
16
+ a=a[0,k+1]+a[k+1..-1].reverse
17
+ yield a.dup[0,n]
18
+ }
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ module DBI
2
+ #connect-transaction-disconnect triplet.
3
+ # To use this method, you need to require dbi before requiring libciel.
4
+ def self.connect_transaction(driver_url, user=nil, auth=nil, params=nil, &block)
5
+ x=connect(driver_url, user, auth, params)
6
+ begin
7
+ x.transaction(&block)
8
+ ensure
9
+ x.disconnect
10
+ end
11
+ end
12
+
13
+ class DatabaseHandle
14
+ #execute-map,count-finish triplet.
15
+ # To use this method, you need to require dbi before requiring libciel.
16
+ def execute_immediate(stmt,*bindvars,&block)
17
+ sth=execute(stmt,*bindvars)
18
+ ret=0
19
+ begin
20
+ if block then ret=sth.map(&block).count end
21
+ ensure
22
+ sth.finish
23
+ end
24
+ ret
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ module Enumerable
2
+ #Squeezes the same element. This behaves like C++ unique().
3
+ #To get the similar result to Array#uniq, you need to sort it prior.
4
+ # Calculation order is O(n).
5
+ def squeeze
6
+ r=[]
7
+ cur=nil
8
+ self.each{|e|
9
+ if r.empty?||cur!=e
10
+ r<<e
11
+ cur=e
12
+ end
13
+ }
14
+ r
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ class Enumerator
2
+ begin
3
+ Lazy.class_eval{
4
+ #Enumerator.Lazy version of Enumerable#squeeze.
5
+ #Enumerator.Lazy is evaluated as Enumerable::Lazy on Ruby 1.9 + enumerable/lazy, otherwise Enumerator::Lazy.
6
+ # To use this method, on Ruby <2.0, you need to require enumerable/lazy or backports before requiring libciel.
7
+ def squeeze
8
+ first=true
9
+ cur=nil
10
+ self.class.new(self){|y,v|
11
+ if first||cur!=v
12
+ y<<v
13
+ first=false
14
+ cur=v
15
+ end
16
+ }
17
+ end
18
+ }
19
+ rescue NameError=>e; end
20
+ end
@@ -0,0 +1,11 @@
1
+ class Hash
2
+ #nil safe version of Hash#[].
3
+ # h.fetch_nested(*['hello','world']) is basically the same as h['hello'].try.send(:[],'world').
4
+ def fetch_nested(*keys)
5
+ begin
6
+ keys.reduce(self){|accum, k| accum.fetch(k)}
7
+ rescue (RUBY_VERSION<'1.9' ? IndexError : KeyError)
8
+ block_given? ? yield(*keys) : nil
9
+ end
10
+ end
11
+ end
data/lib/kernel/zip.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Kernel
2
+ #Pythonic zip. The same as a.shift.zip(*a).
3
+ def zip(_a)
4
+ a=_a.dup
5
+ a.shift.zip(*a)
6
+ end
7
+ end
data/lib/libciel.rb CHANGED
@@ -1,136 +1,15 @@
1
- module LibCiel
2
- #Version string
3
- VERSION='0.0.0.2'
4
- end
5
-
6
- class Object
7
- #PHPic extract(). Hash will be injected into self as instance variables (@var).
8
- def extract(h,overwrite=false)
9
- h.each{|k,v|
10
- if overwrite || !self.instance_variable_defined?('@'+k) then
11
- self.instance_variable_set('@'+k,v) #k should always be String
12
- end
13
- }
14
- end
15
- end
1
+ require File.expand_path(File.dirname(__FILE__)+'/array/permutation2')
2
+ require File.expand_path(File.dirname(__FILE__)+'/enumerable/squeeze')
3
+ require File.expand_path(File.dirname(__FILE__)+'/enumerator/lazy/squeeze')
4
+ require File.expand_path(File.dirname(__FILE__)+'/hash/fetch_nested')
5
+ require File.expand_path(File.dirname(__FILE__)+'/kernel/zip')
6
+ require File.expand_path(File.dirname(__FILE__)+'/object/extract')
7
+ require File.expand_path(File.dirname(__FILE__)+'/string/rotate')
16
8
 
17
- module Kernel
18
- #Pythonic zip. The same as a.shift.zip(*a).
19
- def zip(a) a.shift.zip(*a) end
20
- end
21
-
22
- module Enumerable
23
- #Squeezes the same element. This behaves like C++ unique().
24
- #To get the similar result to Array#uniq, you need to sort it prior.
25
- # Calculation order is O(n).
26
- def squeeze
27
- r=[]
28
- cur=nil
29
- self.each{|e|
30
- if r.empty?||cur!=e
31
- r<<e
32
- cur=e
33
- end
34
- }
35
- r
36
- end
37
- end
38
-
39
- class Enumerator
40
- begin
41
- Lazy.class_eval{
42
- #Enumerator.Lazy version of Enumerable#squeeze.
43
- #Enumerator.Lazy is evaluated as Enumerable::Lazy on Ruby 1.9 + enumerable/lazy, otherwise Enumerator::Lazy.
44
- # To use this method, on Ruby <2.0, you need to require enumerable/lazy or backports before requiring libciel.
45
- def squeeze
46
- first=true
47
- cur=nil
48
- self.class.new(self){|y,v|
49
- if first||cur!=v
50
- y<<v
51
- first=false
52
- cur=v
53
- end
54
- }
55
- end
56
- }
57
- rescue NameError=>e; end
58
- end
9
+ #DBI stuff will be dropped in the future (maybe)
10
+ require File.expand_path(File.dirname(__FILE__)+'/dbi/connect_transaction')
59
11
 
60
- class Array
61
- #Enumerates permutation of Array.
62
- #Unlike Array#permutation, there are no duplicates in generated permutations.
63
- #Instead, elements must be comparable.
64
- def permutation2(n=self.size)
65
- return to_enum(:permutation2,n) unless block_given?
66
- return if n<0||self.size<n
67
- a=self.sort
68
- yield a.dup[0,n]
69
- loop{
70
- a=a[0,n]+a[n..-1].reverse
71
- k=(a.size-2).downto(0).find{|i|a[i]<a[i+1]}
72
- break if !k
73
- l=(a.size-1).downto(k+1).find{|i|a[k]<a[i]}
74
- a[k],a[l]=a[l],a[k]
75
- a=a[0,k+1]+a[k+1..-1].reverse
76
- yield a.dup[0,n]
77
- }
78
- end
79
- end
80
-
81
- class String
82
- #Rotate string to the left with count.
83
- #Specifying negative number indicates rotation to the right.
84
- def rotate(count=1)
85
- count+=self.length if count<0
86
- self.slice(count,self.length-count)+self.slice(0,count)
87
- end
88
- #Destructive version of String#rotate
89
- def rotate!(count=1) self.replace(self.rotate(count)) end
90
- end
91
-
92
- class Hash
93
- #nil safe version of Hash#[].
94
- # h.exists_rec?(['hello','world']) is the same as h['hello'].try.send(:[],'world').
95
- def exists_rec?(a)
96
- #if a.length<1 then return false
97
- if !self.include?(a[0]) then return nil end #if not found
98
- if a.length==1 then return self[a[0]] end #if found and last
99
- if !self[a[0]].is_a?(Hash) then return nil end #if not last and child not hash
100
- return self[a[0]].exists_rec?(a[1..-1]) #check child
101
- end
102
- #Returns self.dup with overwriting par.
103
- def patch(par)
104
- h=self.dup
105
- par.each{|k,v|h[k]=v}
106
- return h
107
- end
108
- end
109
-
110
- module DBI
111
- #connect-transaction-disconnect triplet.
112
- # To use this method, you need to require dbi before requiring libciel.
113
- def self.connect_transaction(driver_url, user=nil, auth=nil, params=nil, &block)
114
- x=connect(driver_url, user, auth, params)
115
- begin
116
- x.transaction(&block)
117
- ensure
118
- x.disconnect
119
- end
120
- end
121
-
122
- class DatabaseHandle
123
- #execute-map,count-finish triplet.
124
- # To use this method, you need to require dbi before requiring libciel.
125
- def execute_immediate(stmt,*bindvars,&block)
126
- sth=execute(stmt,*bindvars)
127
- ret=0
128
- begin
129
- if block then ret=sth.map(&block).count end
130
- ensure
131
- sth.finish
132
- end
133
- ret
134
- end
135
- end
12
+ module LibCiel
13
+ #Version string
14
+ VERSION='0.0.0.3'
136
15
  end
@@ -0,0 +1,10 @@
1
+ class Object
2
+ #PHPic extract(). Hash will be injected into self as instance variables (@var).
3
+ def extract(h,overwrite=false)
4
+ h.each{|k,v|
5
+ if overwrite || !self.instance_variable_defined?('@'+k) then
6
+ self.instance_variable_set('@'+k,v) #k should always be String
7
+ end
8
+ }
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ class String
2
+ #Rotate string to the left with count.
3
+ #Specifying negative number indicates rotation to the right.
4
+ def rotate(count=1)
5
+ count+=self.length if count<0
6
+ self.slice(count,self.length-count)+self.slice(0,count)
7
+ end
8
+ #Destructive version of String#rotate
9
+ def rotate!(count=1) self.replace(self.rotate(count)) end
10
+ end
data/spec/libciel_spec.rb CHANGED
@@ -11,27 +11,50 @@ describe Object do
11
11
  end
12
12
 
13
13
  describe Kernel do
14
- specify 'zip' do
15
- zip([[1,2],[3,4]]).should eq [[1,3],[2,4]]
14
+ context 'zip' do
15
+ specify 'basic case' do
16
+ a=[[1,2],[3,4]]
17
+ zip(a).should eq [[1,3],[2,4]]
18
+ a.transpose.should eq [[1,3],[2,4]]
19
+ end
20
+ specify 'awkward case' do
21
+ a=[[1,2,3],[4,5]]
22
+ zip(a).should eq [[1, 4],[2, 5],[3, nil]]
23
+ lambda{a.transpose}.should raise_error
24
+ end
16
25
  end
17
26
  end
18
27
 
19
28
  describe Enumerable do
20
- specify 'squeeze' do
21
- [1,2,2,3,3,2,1].squeeze.should eq [1,2,3,2,1]
29
+ context 'squeeze' do
30
+ specify 'example' do
31
+ [1,2,2,3,3,2,1].squeeze.should eq [1,2,3,2,1]
32
+ end
33
+ specify 'sort.squeeze is eq uniq' do
34
+ [1,2,2,3,3,2,1].sort.squeeze.should eq [1,2,3]
35
+ end
22
36
  end
23
37
  end
24
38
 
25
39
  describe Enumerator::Lazy do
26
- specify 'squeeze' do
40
+ specify 'squeeze enum' do
27
41
  [1,2,2,3,3,2,1].lazy.squeeze.select(&:odd?).to_a.should eq [1,3,1]
28
42
  end
29
43
  end
30
44
 
31
45
  describe Array do
32
- specify 'permutation2' do
33
- [1,1,2,3].permutation2.to_a.should_not eq [1,1,2,3].permutation.to_a
34
- [1,1,2,3].permutation2.to_a.should eq [1,1,2,3].permutation.to_a.uniq
46
+ context 'permutation2' do
47
+ specify 'example' do
48
+ [1,1,2,2,3].permutation2(2).to_a.should eq [
49
+ [1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2]
50
+ ]
51
+ end
52
+ it 'is not eq permutation.to_a' do
53
+ [1,1,2,3].permutation2.to_a.should_not eq [1,1,2,3].permutation.to_a
54
+ end
55
+ it 'is eq permutation.to_a.uniq' do
56
+ [1,1,2,3].permutation2.to_a.should eq [1,1,2,3].permutation.to_a.uniq
57
+ end
35
58
  end
36
59
  end
37
60
 
@@ -64,8 +87,8 @@ describe Hash do
64
87
  specify 'usual search' do
65
88
  h['hello']['world'].should eq 42
66
89
  end
67
- specify 'exists_rec?' do
68
- h.exists_rec?(['hello','world']).should eq 42
90
+ specify 'fetch_nested' do
91
+ h.fetch_nested(*['hello','world']).should eq 42
69
92
  end
70
93
  end
71
94
  context 'dangerous case' do
@@ -73,24 +96,17 @@ describe Hash do
73
96
  specify 'usual search' do
74
97
  lambda{h['hello']['world']}.should raise_error
75
98
  end
76
- specify 'exists_rec?' do
77
- h.exists_rec?(['hello','world']).should eq nil
99
+ specify 'fetch_nested' do
100
+ h.fetch_nested(*['hello','world']).should eq nil
78
101
  end
79
102
  end
80
- specify 'patch' do
81
- h={'hello'=>1,'world'=>2}
82
- h_dup=h.dup
83
- answer={'hello'=>1,'world'=>42}
84
- h.patch({'world'=>42}).should eq answer
85
- h_dup.should eq h
86
- end
87
103
  end
88
104
 
89
105
  describe DBI do
90
106
  specify 'connect and execute' do
91
107
  pending 'dbd-sqlite3 seems stopped working from Ruby 2.0' if RUBY_VERSION>='2.0'
92
108
  pending 'dbd-sqlite3 is not supported by jruby' if defined?(RUBY_ENGINE)&&RUBY_ENGINE=='jruby'
93
- DBI.connect('DBI:SQLite3:'+File.dirname(__FILE__)+'/test.sqlite',nil,nil,'AutoCommit'=>false){|dbi|
109
+ DBI.connect_transaction('DBI:SQLite3:'+File.dirname(__FILE__)+'/test.sqlite',nil,nil,'AutoCommit'=>false){|dbi|
94
110
  #dbi.execute_immediate("create table test ( message varchar )")
95
111
  dbi.execute_immediate("select * from test"){|e|
96
112
  e.should eq ['hello']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libciel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0.2
4
+ version: 0.0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - cielavenir
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-22 00:00:00.000000000 Z
11
+ date: 2014-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,7 +67,15 @@ files:
67
67
  - LICENSE.txt
68
68
  - README.md
69
69
  - Rakefile
70
+ - lib/array/permutation2.rb
71
+ - lib/dbi/connect_transaction.rb
72
+ - lib/enumerable/squeeze.rb
73
+ - lib/enumerator/lazy/squeeze.rb
74
+ - lib/hash/fetch_nested.rb
75
+ - lib/kernel/zip.rb
70
76
  - lib/libciel.rb
77
+ - lib/object/extract.rb
78
+ - lib/string/rotate.rb
71
79
  - libciel.gemspec
72
80
  - spec/libciel_spec.rb
73
81
  - spec/spec_helper.rb