Coder Social home page Coder Social logo

emcousin / activerecord-deepstore Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 41 KB

ActiveRecord-Deepstore adds powerful functionality to ActiveRecord models for handling deeply nested data structures within database columns. Simplify storing, accessing, and managing complex nested data in your Rails applications with ease.

License: MIT License

Ruby 99.69% Shell 0.31%

activerecord-deepstore's Introduction

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', require: 'active_record/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.

activerecord-deepstore's People

Contributors

emcousin avatar

Stargazers

Sebastian van Hesteren avatar Florian Wininger avatar  avatar Matthias Richard avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.