Class: Prysless::Store
- Inherits:
-
Object
- Object
- Prysless::Store
- Defined in:
- lib/prysless.rb
Overview
Public: Pry store allowing to pass and persist data between sessions
Data can be accessed either via hash notation or metheod notation
This is a core functionality of prysless since we want to be able
to share states with other processes (namely: the shell) without
copy/paste.
Examples
Store.new['lol'] = 'test'
Store.new['lol]
=> 'test'
Store.new.lil = 'blah'
Store.new.lil
=> 'blah'
Instance Method Summary collapse
-
#[](key) ⇒ Object
Public: reads data from the store.
-
#[]=(key, value) ⇒ Object
Public: saves data to the store.
-
#initialize ⇒ Store
constructor
A new instance of Store.
Constructor Details
#initialize ⇒ Store
Returns a new instance of Store.
26 27 28 29 30 |
# File 'lib/prysless.rb', line 26 def initialize configuration_directory = "#{ENV['HOME']}/.config" FileUtils.mkdir_p configuration_directory @store = PStore.new("#{configuration_directory}/prysless.pstore") end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *params, &block) ⇒ Object (private)
Return the data that was saved or read
69 70 71 72 73 74 75 76 |
# File 'lib/prysless.rb', line 69 def method_missing method, *params, &block method = method.to_s if method[-1..-1] == '=' self[method[0..-2]] = params[0] else self[method] end end |
Instance Method Details
#[](key) ⇒ Object
Public: reads data from the store
key - the value of the key to retrieve
Examples
[]= 'lol'
=> 'test'
Returns the data to read
55 56 57 |
# File 'lib/prysless.rb', line 55 def [] key @store.transaction { @store[key] } end |
#[]=(key, value) ⇒ Object
Public: saves data to the store
key - the name of the key to store value - the value associated with the key
Examples
[]= 'lol', 'test'
=> 'test'
Returns the data that was saved
42 43 44 |
# File 'lib/prysless.rb', line 42 def []= key, value @store.transaction { @store[key] = value } end |