Class: CollectionAdapters::HashSequel

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

Overview

Takes a Sequel model and provides #[] and #[]= using Sequels API.

Values are converted silently to String’s

NOTE: You need to require ‘sequel’ yourself; this is to allow you to optionally also use this adapter with other classes that provide the same API.

Instance Method Summary collapse

Constructor Details

#initialize(model:, keycolumn:, valuecolumn:) ⇒ HashSequel

Returns a new instance of HashSequel.



16
17
18
19
20
# File 'lib/collectionadapters/hash_sequel.rb', line 16

def initialize(model:, keycolumn:, valuecolumn:)
  @model  = model
  @k = keycolumn.to_sym
  @v = valuecolumn.to_sym
end

Instance Method Details

#[](key) ⇒ Object



28
29
30
31
# File 'lib/collectionadapters/hash_sequel.rb', line 28

def [] (key)
  r = @model[@k => key.to_s]
  r ? r.values[@v] : nil
end

#[]=(key, value) ⇒ Object



22
23
24
25
26
# File 'lib/collectionadapters/hash_sequel.rb', line 22

def []= (key, value)
  key = key.to_s
  (@model.first(@k => key) || @model.new).set(@k => key, @v => value).save
  value
end

#delete(key) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/collectionadapters/hash_sequel.rb', line 33

def delete(key)
  if ob = @model.first(@k => key.to_s)
    ob.delete
    ob.values[@v]
  else
    nil
  end
end