Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pobject

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pobject

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Persistent Object

Gem Travis Code Climate Gemnasium


Transparently persist objects to disk as YAML or PStore files.


Install

$ gem install pobject

Or with bundler:

gem 'pobject'

Usage

Your object should inherit from PObject.

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.

class Settings < PObject
end

config = 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.

config.port
# => 3000

config[:port]
# => 3000

To change the location of the file, simply override the to_store method.

class Settings < PObject
  def to_store
    "config/local.yml"
  end
end

config = 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.

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.

class Hero < PObject
  def initialize(id)
    @id = id
  end

  def to_store
    ["heroes.yml", @id]
  end
end

hammer = Hero.new :hammer
raynor = Hero.new :raynor

hammer.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 beinning of your class.

class Book < PObject
  allow_missing
end

book = Book.new
book.author
# => nil

FAQs

Package last updated on 01 Jul 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc