Sha256: a5f23e3e4b2587f449ebe3c8dcfa9fbf9343697adcd0792416a6fd2f074a276b

Contents?: true

Size: 845 Bytes

Versions: 1

Compression:

Stored size: 845 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)
      ob = @model.first(@k => key.to_s)
      ob.delete
      ob.values[@v]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
collectionadapters-0.1.0 lib/collectionadapters/hash_sequel.rb~