Class: CollectionAdapters::ArraySequel

Inherits:
Object
  • Object
show all
Defined in:
lib/collectionadapters/array_sequel.rb

Instance Method Summary collapse

Constructor Details

#initialize(model:, column:) ⇒ ArraySequel

Returns a new instance of ArraySequel.



3
4
5
6
7
# File 'lib/collectionadapters/array_sequel.rb', line 3

def initialize model:, column:
  @ds    = model
  @model = model.kind_of?(Sequel::Dataset) ? @ds.model : @ds
  @col   = column.to_sym
end

Instance Method Details

#<<(val) ⇒ Object



9
10
11
12
13
# File 'lib/collectionadapters/array_sequel.rb', line 9

def << val
  @model.new.set(@col => val).save
rescue Sequel::UniqueConstraintViolation
  nil
end

#concat(other) ⇒ Object



19
20
21
# File 'lib/collectionadapters/array_sequel.rb', line 19

def concat other
  other.each {|v| self << v }
end

#countObject



15
16
17
# File 'lib/collectionadapters/array_sequel.rb', line 15

def count
  @model.count
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/collectionadapters/array_sequel.rb', line 23

def include? key
  @model[@col => key] != nil
end

#shiftObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/collectionadapters/array_sequel.rb', line 27

def shift
  @ds.db.transaction do
    if ob = @ds.for_update.first
      v  = ob.values[@col]
      return v if ob.delete
      raise Sequel::rollback
    end
  end
  nil
end