Sha256: 591b5d46ad50a789bc1b98779c5bf351b9a0d1c062ca7bdd9e852cc49d4b8dbd

Contents?: true

Size: 908 Bytes

Versions: 1

Compression:

Stored size: 908 Bytes

Contents

require 'active_support'
require 'active_record'
require 'simple_hstore_accessor/version'

module SimpleHstoreAccessor
  # Public: Rails4-like method which defines simple accessors for hstore fields
  #
  # hstore_attribute - your Hstore column
  # keys - Array of fields in your hstore
  #
  # Example
  #
  #   class Person < ActiveRecord::Base
  #     store_accessor :favorites_info, :book, :color
  #   end
  #
  # Returns nothing
  def store_accessor(hstore_attribute, *keys)
    Array(keys).flatten.each do |key|
      define_method("#{key}=") do |value|
        send("#{hstore_attribute}_will_change!")
        send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(key.to_s => value))
      end
      define_method(key) do
        send(hstore_attribute) && send(hstore_attribute)[key.to_s]
      end
    end
  end
end

ActiveSupport.on_load(:active_record) { extend SimpleHstoreAccessor }

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
simple_hstore_accessor-0.0.2 lib/simple_hstore_accessor.rb