collectionadapters 0.1.0 → 0.2.0

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
  SHA256:
3
- metadata.gz: 0e6b15127d37e2c2fb9dc0519eaf3bf93f59b8a617fb2113dfd9bc3a5be1656c
4
- data.tar.gz: 537506e2dfd758415109a38609ecf7dc30a62d3c55bb596576b36a89f89ceaa6
3
+ metadata.gz: 80a678b0765768108c65062a11887800f8ce44c53b4bfaf3f5b3ba73fb936cf3
4
+ data.tar.gz: 95ccc58f21132bc6bb24844529be539b0f929d0035c15d42be35679ba7db9bd7
5
5
  SHA512:
6
- metadata.gz: a00302535344debb22140ff4bfdfcd54e2122ad06b29ed8589b0f09760100e0c6ff1676f1f1d3079b8735a5ca01d9920f28a95d4da938d96e6adb55cbf734d57
7
- data.tar.gz: d63d6e4e9c0ca29bb7e1b0594089366f7c059be25bc42e8bdff35b4998afdd0a963e45c306e1816d238223cbd55f0b1a8ece1c4db75f64cb702f9e77d41d8303
6
+ metadata.gz: 26aa5653ae9e9b288030708a05b7b5ab87598bba0574493b68abb4f599d1ac0a8933ed5f58db55c6760da553cd17a4c00865aa4f9eb5a71c591c7f45cb6089e4
7
+ data.tar.gz: 8b612237e037d8923be8db0e02c7fb603362e5af113c0e57577092815a080d679f6aeec0635f9e5907fd446ef1406efac17c514d357ce3688cf4b3b52e7bfcda
@@ -0,0 +1,39 @@
1
+ module CollectionAdapters
2
+ class ArraySequel
3
+ def initialize model:, column:
4
+ @model = model
5
+ @col = column.to_sym
6
+ end
7
+
8
+ def << val
9
+ @model.new.set(@col => val).save
10
+ rescue Sequel::UniqueConstraintViolation
11
+ nil
12
+ end
13
+
14
+ def count
15
+ @model.count
16
+ end
17
+
18
+ def concat other
19
+ other.each {|v| self << v }
20
+ end
21
+
22
+ def include? key
23
+ @model[@col => key] != nil
24
+ end
25
+
26
+ def shift
27
+ @model.db.transaction do
28
+ if ob = @model.for_update.first
29
+ v = ob.values[@col]
30
+ return v if ob.delete
31
+ raise Sequel::rollback
32
+ end
33
+ end
34
+ nil
35
+ end
36
+ end
37
+ end
38
+
39
+
@@ -1,3 +1,3 @@
1
1
  module Collectionadapters
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,7 +1,6 @@
1
1
  require "collectionadapters/version"
2
2
 
3
3
  module Collectionadapters
4
- # Your code goes here...
5
4
  end
6
5
 
7
6
  CollectionAdapters = Collectionadapters
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collectionadapters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vidar Hokstad
@@ -99,8 +99,8 @@ files:
99
99
  - bin/setup
100
100
  - collectionadapters.gemspec
101
101
  - lib/collectionadapters.rb
102
+ - lib/collectionadapters/array_sequel.rb
102
103
  - lib/collectionadapters/hash_sequel.rb
103
- - lib/collectionadapters/hash_sequel.rb~
104
104
  - lib/collectionadapters/version.rb
105
105
  homepage: http://github.com/vidarh/collectionadapters
106
106
  licenses:
@@ -1,39 +0,0 @@
1
-
2
- module CollectionAdapters
3
- # Takes a Sequel model
4
- # and provides +#[]+ and +#[]=+
5
- # using Sequels API.
6
- #
7
- # Values are converted silently to String's
8
- #
9
- # NOTE: You need to require 'sequel'
10
- # yourself; this is to allow you to
11
- # optionally also use this adapter
12
- # with other classes that provide the
13
- # same API.
14
- #
15
- class HashSequel
16
- def initialize(model:, keycolumn:, valuecolumn:)
17
- @model = model
18
- @k = keycolumn.to_sym
19
- @v = valuecolumn.to_sym
20
- end
21
-
22
- def []= (key, value)
23
- key = key.to_s
24
- (@model.first(@k => key) || @model.new).set(@k => key, @v => value).save
25
- value
26
- end
27
-
28
- def [] (key)
29
- r = @model[@k => key.to_s]
30
- r ? r.values[@v] : nil
31
- end
32
-
33
- def delete(key)
34
- ob = @model.first(@k => key.to_s)
35
- ob.delete
36
- ob.values[@v]
37
- end
38
- end
39
- end