ActiveRecord-like interface for read only access to YAML static data.
Add this line to your application's Gemfile:
gem 'frozen_record'
And then execute:
$ bundle
Or install it yourself as:
$ gem install frozen_record
Just like with ActiveRecord, your models need to inherits from FrozenRecord::Base
:
class Country < FrozenRecord::Base
end
But you also have to specify in which directory your data files are located. You can either do it globaly
FrozenRecord::Base.base_path = '/path/to/some/directory'
Or per model:
class Country < FrozenRecord::Base
self.base_path = '/path/to/some/directory'
end
FrozenRecord aim to replicate only modern ActiveRecord querying interface, and only the non "string typed" ones.
e.g
# Supported query interfaces
Country.
where(region: 'Europe').
where.not(language: 'English').
order(id: :desc).
limit(10).
offset(2).
pluck(:name)
# Non supported query interfaces
Country.
where('region = "Europe" AND language != "English"').
order('id DESC')
While the scope :symbol, lambda
syntax is not supported, the class methods way is:
class Country
def self.republics
where(king: nil)
end
def self.part_of_nato
where(nato: true)
end
end
Country.republics.part_of_nato.order(id: :desc)
- where
- where.not
- order
- limit
- offset
- find
- first
- last
- to_a
- exists?
- count
- pluck
- ids
- minimum
- maximum
- sum
- average
By default the YAML files are parsed once and then cached in memory. But in development you might want changes to be reflected without having to restart your application.
For such cases you can set auto_reloading
to true
either globally or on a model basis:
FrozenRecord::Base.auto_reloading = true # Activate reloading for all models
Country.auto_reloading # Activate reloading for `Country` only
FrozenRecord is a from scratch reimplementation of a Shopify project from 2007 named YamlRecord
.
So thanks to:
- John Duff - @jduff
- Dennis O'Connor - @dennisoconnor
- Christopher Saunders - @csaunders
- Jonathan Rudenberg - @titanous
- Jesse Storimer - @jstorimer
- Cody Fauser - @codyfauser
- Tobias Lütke - @tobi