0.0
No commit activity in last 3 years
No release in over 3 years
This gem provides accessor methods to hash keys.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.3
>= 4.0
>= 0

Runtime

 Project Readme

HashAccessor

Build Status Coverage Status Code Climate

DEPRECATED

This gem is deprecated and will be deleted in the future. Switch to an alternative like activerecord-typedstore.

Summary

This gem provides similar functionality to Rails' new ActiveAttr, except with a bunch of powerful methods behind it. Type definitions, defaults, lambdas on arrays, and more.

The purpose behind building HashAccessor was to be able to quickly add/modify/remove variables being stored in a serialized hash of a rails model. This is very useful if you have a large list of often changing variables on a model which don't get queried against. You can create new variables without messing up your DB. Storing account configurations or custom themes is a common scenario.

This gem was designed to have HTML forms update directly to the hash, but maintain the correct class (respecting :attr_accessible of course). Options like :default, :type, and :collects are particularly helpful when dealing with forms.

Installation

In your Gemfile:

gem "hash_accessor"

Then run:

bundle install

Usage

You define each accessor you want to be stored inside the serialized hash:

hash_accessor _NAME_OF_HASH_, _NAME_OF_VARIABLE_, options

Here is an example:

class MyModel < ActiveRecord::Base
  serialize :options, Hash

  hash_accessor :options, :currency, :default => "$USD"
  hash_accessor :options, :display_currency_on_invoices, :type => :boolean, :default => true
  hash_accessor :options, :invoice_due_date_net, :type => :integer, :default => 3


  def some_method

    self.currency = "$CAD"

    display_currency_on_invoices?

    # ...
  end

end

Valid Options:

:default - if undefined, this plugin will create an empty instance of the defined type or null

:type - defaults to :string. Can also be :integer, :float, :decimal, :boolean, or :array

For Arrays only:

:collects - only runs on arrays. Calls the lambda method on each item in the array before saving

:reject_blanks - removes all blank elements after the collect method. This with :collects can be used together for storing a list of ids stored as checkboxes in a form.

Contributing & Reporting Issues

Please feel free to report any issues, provide feedback, or make suggestions on the Issue Tracker

Tests can be ran against different versions of Rails like so:

BUNDLE_GEMFILE=test/gemfiles/Gemfile.rails-3.2.x bundle install
BUNDLE_GEMFILE=test/gemfiles/Gemfile.rails-3.2.x bundle exec rake test

Copyright (c) 2014 OctopusApp Inc. (getjobber.com), released under the MIT license