This repository has been archived by the owner. It is now read-only.
Object-Relational-Mapping in Rails with consistent JSON data.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.github/ISSUE_TEMPLATE
app/models
lib
spec
.gitignore
.rspec
.rubocop.yml
.travis.yml
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Gemfile
Gemfile.lock
LICENSE
PULL_REQUEST_TEMPLATE.md
README.md
activedata.gemspec

README.md

Active Data

Object-Relational-Mapping in Rails with consistent JSON data.

Installation

  1. Bundle the gem.
  2. Run the generator: rails g active_data

Philosophy

Active Data aims to implement the same functionality as Active Record without using a database as datastore. Instead it utilizes a JSON file. You can use Active Data alongside with Active Record to create models in your Rails application. Features include:

  • Common methods (self.all, self.create, self.where, self.find_by, self.find, save, update, update_attributes, update_attribute, destroy, destroyed?)
  • Callbacks (before_validation, after_validation, before_save, after_save, before_create, after_create, before_update, after_update, before_destroy, after_destroy)
  • Associations (has_many, belongs_to - also with Active Record models)
  • All functionality provided by the ActiveModel::Model class (e.g.: validations)

Usage

class Example < ApplicationData
  active_data(
    file_name: 'example_data', # uses /data/example_data.json; if omitted would use /data/example.json
    json_scope: lambda { |data| data[:examples] }, # if omitted would use data, result has to return a JSON array
    permit_attributes: [:foo, :bar], # should only contain attribute names that have getter and setter methods
    explicit_ids: false, # if false, does not store id's of objects in JSON
    explicit_nulls: false, # if false, does not store attributes whose values are null in JSON
    delay_loading: true, # if true, does not load data when the app starts.
    prohibit_writes: true # if true, does not allow writing operations.
  )

  attr_accessor :foo, :bar
end

To manually load data for a class:

Example.dataset.load