module JamRuby
  class JsonStore < ActiveRecord::Base
    self.table_name = 'json_stores'

    #serialize :data_blob, JSON

    before_create do
      self.data_blob ||= {}
    end

    after_save do
      @json = nil
    end

    attr_accessible :user_id

    belongs_to :user, class_name: "JamRuby::User"

    def json
      @json ||= self.data_blob
    end

    def update_json_value(key, val)
      self.json[key] = val
      self.update_attribute(:data_blob, self.json)
    end

    def json_value(key)
      self.json[key]
    end

  end
end
