Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dannyben/pobject
Automatically persist objects to disk as YAML files
https://github.com/dannyben/pobject
config gem persistence persistent-storage pstore ruby yaml
Last synced: 27 days ago
JSON representation
Automatically persist objects to disk as YAML files
- Host: GitHub
- URL: https://github.com/dannyben/pobject
- Owner: DannyBen
- License: mit
- Created: 2016-06-29T17:42:44.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-04-10T06:53:22.000Z (over 1 year ago)
- Last Synced: 2024-05-01T14:12:09.958Z (8 months ago)
- Topics: config, gem, persistence, persistent-storage, pstore, ruby, yaml
- Language: Ruby
- Size: 21.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Persistent Object
==================================================[![Gem Version](https://badge.fury.io/rb/pobject.svg)](https://badge.fury.io/rb/pobject)
[![Build Status](https://github.com/DannyBen/pobject/workflows/Test/badge.svg)](https://github.com/DannyBen/pobject/actions?query=workflow%3ATest)
[![Maintainability](https://api.codeclimate.com/v1/badges/aa8fa554a6b71904cd4e/maintainability)](https://codeclimate.com/github/DannyBen/pobject/maintainability)---
Transparently persist objects to disk as YAML or PStore files.
---
Install
--------------------------------------------------```
$ gem install pobject
```Or with bundler:
```ruby
gem 'pobject'
```Usage
--------------------------------------------------Your object should inherit from `PObject`.
```ruby
require 'pobject'class Settings < PObject
end
```Now, any time you access a property, it is saved to a file. By default, we
will save a YAML file with the same name as the class.```ruby
class Settings < PObject
endconfig = Settings.new
config.port = 3000
# Will create a 'settings.yml' file and store the port value
```You can access any property by either dot notation or hash notation.
```ruby
config.port
# => 3000config[:port]
# => 3000
```To change the location of the file, simply override the `to_store` method.
```ruby
class Settings < PObject
def to_store
"config/local.yml"
end
endconfig = Settings.new
config.port = 3000
# Will create a 'config/local.yml'
```Whenever you use the `.yml` (or `.yaml`) extension, we will store a YAML
file. If you wish to store a `PStore` object instead, use any other
extension.```ruby
class Settings < PObject
def to_store
"config/local.pstore"
end
end
```To store several objects in one store file, your `to_store` method should
return an array with two elements: The first, is the path to the file and
the second is any unique key identifying the instance.```ruby
class Hero < PObject
def initialize(id)
@id = id
enddef to_store
["heroes.yml", @id]
end
endhammer = Hero.new :hammer
raynor = Hero.new :raynorhammer.name = 'Sgt. Hammer'
raynor.name = 'Raynor'puts File.read 'heroes.yml'
# =>
# ---
# :hammer:
# :name: Sgt. Hammer
# :raynor:
# :name: Raynor
```By default, PObject will raise an error when accessing a property that does
not exist. To change this behavior, call `allow_missing` at the beginning of
your class.```ruby
class Book < PObject
allow_missing
endbook = Book.new
book.author
# => nil
```