Sha256: 9aa701ec6e80df6ae3facac90041244ccb963898fee84a3ced3e7741e0c63066

Contents?: true

Size: 885 Bytes

Versions: 3

Compression:

Stored size: 885 Bytes

Contents

module CollectionAdapters
  # 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.
  #
  class HashSequel
    def initialize(model:, keycolumn:, valuecolumn:)
      @model  = model
      @k = keycolumn.to_sym
      @v = valuecolumn.to_sym
    end

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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
collectionadapters-0.3.0 lib/collectionadapters/hash_sequel.rb
collectionadapters-0.2.0 lib/collectionadapters/hash_sequel.rb
collectionadapters-0.1.0 lib/collectionadapters/hash_sequel.rb