The project is in a healthy, maintained state
ActiveRecord-Deepstore enhances ActiveRecord models with powerful functionality for handling deeply nested data structures within a database column. It provides methods for storing, accessing, and managing deeply nested data, making it easier to work with complex data structures in your Rails applications. With ActiveRecord-Deepstore, you can seamlessly store nested hashes in database columns, access nested data with simple method calls, track changes to nested attributes, and much more. This gem simplifies the handling of complex data structures, improving the maintainability and readability of your Rails codebase.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

ActiveRecord-Deepstore

ActiveRecord-Deepstore is a Ruby gem that extends ActiveRecord models with additional functionality for handling deeply nested data structures within a database column. It simplifies storing, accessing, and managing complex nested data in your Rails applications.

Installation

Add this line to your application's Gemfile:

gem 'activerecord-deepstore'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install activerecord-deepstore

Usage

To use ActiveRecord-Deepstore in your Rails application, include it in your ActiveRecord models:

class MyModel < ActiveRecord::Base
  extend ActiveRecord::Deepstore
end

Once included, your models gain access to methods for storing and accessing deeply nested data within database columns.

Example

class User < ActiveRecord::Base
  extend ActiveRecord::Deepstore

  deep_store :settings, {
    notifications: {
      posts: { push: true, email: true },
      comments: { push: true, email: false }
    }
  }
end

This implementation provides with an accessor for every level of the provider hash:

user.notifications_settings # => { posts: { push: true, email: true }, comments: { push: true, email: false } }
user.posts_notifications_settings # => { push: true, email: true }
user.push_posts_notifications_settings # => true
user.email_posts_notifications_settings # => true
user.comments_notifications_settings # => { push: true, email: false }
user.push_comments_notifications_settings # => true
user.email_comments_notifications_settings # => false

You can list all the generated accessors by calling deep_stored_accessors on the model User:

User.deep_stored_accessors
# => ["notifications_settings", "posts_notifications_settings", "push_posts_notifications_settings", [...], "email_comments_notifications_settings"]

Automatic typecasting

Writer methods automatically cast the value to the type the default values belong to. For example:

user.push_comments_notifications_settings = "1" # => "1"
user.push_comments_notifications_settings # => true
user.push_comments_notifications_settings = "0" # => "0"
user.push_comments_notifications_settings # => false

Tracking value changes

Dirty attributes are implemented for every accessor. For example:

user.push_comments_notifications_settings # => false
user.push_comments_notifications_settings = true # => true
user.push_comments_notifications_settings_was # => false
user.push_comments_notifications_settings_changes # => { false => true }
user.push_comments_notifications_settings_changed? # => true

Accessing default values

You can access the default value of every accessor at anytime by calling the associated default_#{accessor_name} method:

user.push_comments_notifications_settings # => false
user.update! push_comments_notifications_settings: true
user.push_comments_notifications_settings # => true
user.default_push_comments_notifications_settings #=> false

Resetting to default values

You can reset every accessor to its default value at anytime by calling the associated reset_#{accessor_name} method:

# When the changes are not persisted
user.push_comments_notifications_settings # => false
user.push_comments_notifications_settings = true
user.reset_push_comments_notifications_settings
user.push_comments_notifications_settings # => false

# When the changes are persisted
user.update! push_comments_notifications_settings: true
user.push_comments_notifications_settings # => true
user.reset_push_comments_notifications_settings!
user.reload.push_comments_notifications_settings # => false

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/EmCousin/activerecord-deepstore.

License

The gem is available as open source under the terms of the MIT License.