Module: HstoreColumns::ClassMethods

Defined in:
lib/hstore-attributes/hstore_columns.rb

Instance Method Summary collapse

Instance Method Details

#hstore(store_attribute, options = {}) ⇒ Object



35
36
37
38
39
40
# File 'lib/hstore-attributes/hstore_columns.rb', line 35

def hstore(store_attribute, options = {})
  accessors = options[:accessors]
  accessors.each do |key, value|
    hstore_accessor store_attribute, key, value.nil? ? :string : value
  end
end

#hstore_accessor(hstore_column, attr_name, type = :string) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hstore-attributes/hstore_columns.rb', line 22

def hstore_accessor(hstore_column, attr_name, type = :string)
  return unless table_exists?
  column                      = ActiveRecord::ConnectionAdapters::Column.new(attr_name, nil, "#{type}")
  columns_hash[attr_name.to_s]= column
  cast_code                   = column.type_cast_code('v')
  access_code                 = "(v=read_hstore('#{attr_name}', '#{hstore_column}')) && #{cast_code}"

  class_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{attr_name}; #{access_code}; end
        def #{attr_name}= value; write_hstore('#{attr_name}', value, '#{hstore_column}'); end
  RUBY
end