class CollectionAdapters::HashSequel
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.
Public Class Methods
new(model:, keycolumn:, valuecolumn:)
click to toggle source
# File lib/collectionadapters/hash_sequel.rb, line 16 def initialize(model:, keycolumn:, valuecolumn:) @model = model @k = keycolumn.to_sym @v = valuecolumn.to_sym end
Public Instance Methods
[](key)
click to toggle source
# File lib/collectionadapters/hash_sequel.rb, line 28 def [] (key) r = @model[@k => key.to_s] r ? r.values[@v] : nil end
[]=(key, value)
click to toggle source
# 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)
click to toggle source
# 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