https://github.com/timcraft/minimodel
Ruby gem for defining read-only models
https://github.com/timcraft/minimodel
rails ruby yaml
Last synced: over 1 year ago
JSON representation
Ruby gem for defining read-only models
- Host: GitHub
- URL: https://github.com/timcraft/minimodel
- Owner: timcraft
- License: other
- Created: 2011-09-21T12:55:29.000Z (almost 15 years ago)
- Default Branch: main
- Last Pushed: 2024-01-11T15:11:02.000Z (over 2 years ago)
- Last Synced: 2025-03-15T23:15:36.602Z (over 1 year ago)
- Topics: rails, ruby, yaml
- Language: Ruby
- Homepage:
- Size: 40 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# minimodel
[](https://badge.fury.io/rb/minimodel) [](https://github.com/timcraft/minimodel/actions/workflows/test.yml)
Ruby gem for defining read-only models.
## Installation
$ gem install minimodel
## Motivation
Many apps use small "read only" datasets that can easily be held in memory,
for example: currency data, country data, colour data, advertising banners,
options for select boxes, payment plans etc. Sometimes it's useful to model
this data without using a database, and that's what minimodel is for.
## Example
Here's how you could implement a currency model using minimodel:
```ruby
require 'minimodel'
class Currency < MiniModel
indexed_by :code
insert code: 'EUR', name: 'Euro'
insert code: 'GBP', name: 'Pound sterling'
insert code: 'USD', name: 'United States dollar'
end
```
The Currency class will respond to `#count` (returning the total number of
currencies), `#all` (returning an array of currency objects), and `#find`
(to lookup a specific currency by its code). Similar to ActiveRecord.
There's also a `load_from` class method which will load data from a YAML
file; useful for when you'd rather store the raw data outside of the class.
Take a look at `spec/*_spec.rb` for more examples. Have fun!