Sha256: 76a9cdf9f51736e576956544ef1cff0e3a0ca43adc1a65cd71664c6bdbb6b4be

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

begin
  require "active_record"
rescue LoadError
  puts "You need the activerecord gem in order to use the ActiveRecord moneta store"
  exit
end

class Object
  unless respond_to?(:symbolize_keys)
    def symbolize_keys
      
    end
  end
end

module Moneta
  module Adapters
    class SimpleActiveRecord
      class Store < ::ActiveRecord::Base
        set_primary_key 'key'
        set_table_name 'settings'

        def parsed_value
          JSON.parse(value)['root']
        end
      end
      
      def initialize(options = {})
        @options = options
        #Store.establish_connection(@options[:connection] || raise("Must specify :connection"))
        Store.set_table_name(@options[:table] || 'settings')
      end
      
      module Implementation
        def key?(key)
          !!self[key]
        end

        def has_key?(key)
          key?(key)
        end

        def [](key)
          record = Store.find_by_key(key)
          record ? record.parsed_value : nil
        end
      
        def []=(key, value)
          record = Store.find_by_key(key)
          if record
            record.update_attributes!(:value => {'root' => value}.to_json)
          else
            store = Store.new
            store.key = key
            store.value = {'root' => value}.to_json
            store.save!
          end
        end

        def fetch(key, value = nil)
          value ||= block_given? ? yield(key) : default # TODO: Shouldn't yield if key is present?
          self[key] || value
        end

        def delete(key)
          record = Store.find_by_key(key)
          if record
            record.destroy
            record.parsed_value
          end
        end
        
        def store(key, value, options = {})
          self[key] = value
        end

        def clear
          Store.delete_all
        end

      end

      # Unimplemented
      module Expiration
        def update_key(key, options)
        end
      end

      include Implementation
      include Expiration
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cockpit-0.1.1 lib/cockpit/moneta/simple_active_record.rb