Class: MemStore::HashStore
- Inherits:
-
ObjectStore
- Object
- ObjectStore
- MemStore::HashStore
- Defined in:
- lib/memstore/hashstore.rb,
lib/memstore/json.rb,
lib/memstore/yaml.rb,
lib/memstore/msgpack.rb
Overview
A HashStore accesses item attributes through item.
Instance Attribute Summary
Attributes inherited from ObjectStore
Class Method Summary collapse
-
.from_binary(binary) ⇒ Object
Restores a data store from binary format.
-
.from_hash(hash) ⇒ Object
Restores a data store from a Hash.
- .from_json(json) ⇒ Object
- .from_json_file(file) ⇒ Object
- .from_msgpack(msgpack) ⇒ Object
- .from_msgpack_file(file) ⇒ Object
- .from_yaml(yaml) ⇒ Object
- .from_yaml_file(file) ⇒ Object
- .with_json_file(file, key = nil, items = {}, &block) ⇒ Object
- .with_msgpack_file(file, key = nil, items = {}, &block) ⇒ Object
- .with_yaml_file(file, key = nil, items = {}, &block) ⇒ Object
Instance Method Summary collapse
-
#initialize(key = nil, items = {}) ⇒ HashStore
constructor
Initializes a HashStore.
-
#to_hash ⇒ Object
Returns data store as a Hash with the fields :key and :items.
- #to_json ⇒ Object
- #to_json_file(file) ⇒ Object
- #to_msgpack ⇒ Object
- #to_msgpack_file(file) ⇒ Object
- #to_yaml ⇒ Object
- #to_yaml_file(file) ⇒ Object
Methods inherited from ObjectStore
#[], #all, #count_all, #count_any, #count_none, #count_not_all, #count_one, #delete_items, #delete_keys, #find_all, #find_any, #find_none, #find_not_all, #find_one, #first_all, #first_any, #first_none, #first_not_all, #first_one, from_file, #insert, #length, #to_binary, #to_file, with_file
Constructor Details
#initialize(key = nil, items = {}) ⇒ HashStore
Initializes a HashStore.
key - Optional Object used as an item’s key attribute (default: nil).
When no key is specified, Object#hash will be used for uniquely identifying items.
items - Optional Hash of items to initialize the data store with (default: empty Hash).
Examples
store = HashStore.new
store = HashStore.new(:id)
store = HashStore.new(nil, { a.hash => a, b.hash => b, c.hash => c })
store = HashStore.new(:id, { 1 => a, 2 => b, 3 => c })
Returns initialized ObjectStore.
22 23 24 |
# File 'lib/memstore/hashstore.rb', line 22 def initialize(key=nil, items={}) @key, @items = key, items end |
Class Method Details
.from_binary(binary) ⇒ Object
Restores a data store from binary format.
binary - Binary data containing a serialized instance of HashStore.
Examples
store = HashStore.from_binary(IO.read(file))
Returns instance of HashStore
or nil if marshalling failed or marshalled object isn
Raises whatever Marshal::load raises.
42 43 44 45 |
# File 'lib/memstore/hashstore.rb', line 42 def self.from_binary(binary) restored = Marshal.load(binary) rescue nil if restored.instance_of?(HashStore) then restored else nil end end |
.from_hash(hash) ⇒ Object
Restores a data store from a Hash.
hash - Hash that contains a serialized HashStore.
It must have the fields :key/"key" and :items/"items".
Examples
store = HashStore.from_hash(hash)
Returns instance of HashStore
or nil if hash isn
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/memstore/hashstore.rb', line 58 def self.from_hash(hash) begin if hash.has_key?(:key) then key = hash[:key] elsif hash.has_key? "key" then key = hash["key"] else return nil end if hash.has_key?(:items) then items = hash[:items] elsif hash.has_key? "items" then items = hash["items"] else return nil end if items.is_a?(Hash) then self.new(key, items) else self.new(key) end rescue nil end end |
.from_json(json) ⇒ Object
17 18 19 |
# File 'lib/memstore/json.rb', line 17 def self.from_json(json) self.from_hash(JSON.parse(json)) rescue nil end |
.from_json_file(file) ⇒ Object
21 22 23 |
# File 'lib/memstore/json.rb', line 21 def self.from_json_file(file) self.from_json(IO.read(file)) rescue nil end |
.from_msgpack(msgpack) ⇒ Object
17 18 19 |
# File 'lib/memstore/msgpack.rb', line 17 def self.from_msgpack(msgpack) self.from_hash(MessagePack.unpack(msgpack)) rescue nil end |
.from_msgpack_file(file) ⇒ Object
21 22 23 |
# File 'lib/memstore/msgpack.rb', line 21 def self.from_msgpack_file(file) self.from_msgpack(IO.read(file)) rescue nil end |
.from_yaml(yaml) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/memstore/yaml.rb', line 17 def self.from_yaml(yaml) begin self.from_hash(YAML.load(yaml)) rescue StandardError, Psych::SyntaxError nil end end |
.from_yaml_file(file) ⇒ Object
25 26 27 |
# File 'lib/memstore/yaml.rb', line 25 def self.from_yaml_file(file) self.from_yaml(IO.read(file)) rescue nil end |
.with_json_file(file, key = nil, items = {}, &block) ⇒ Object
25 26 27 |
# File 'lib/memstore/json.rb', line 25 def self.with_json_file(file, key=nil, items={}, &block) self.execute_with_file(:from_json_file, :to_json_file, file, key, items, &block) end |
.with_msgpack_file(file, key = nil, items = {}, &block) ⇒ Object
25 26 27 |
# File 'lib/memstore/msgpack.rb', line 25 def self.with_msgpack_file(file, key=nil, items={}, &block) self.execute_with_file(:from_msgpack_file, :to_msgpack_file, file, key, items, &block) end |
.with_yaml_file(file, key = nil, items = {}, &block) ⇒ Object
29 30 31 |
# File 'lib/memstore/yaml.rb', line 29 def self.with_yaml_file(file, key=nil, items={}, &block) self.execute_with_file(:from_yaml_file, :to_yaml_file, file, key, items, &block) end |
Instance Method Details
#to_hash ⇒ Object
Returns data store as a Hash with the fields :key and :items.
27 28 29 |
# File 'lib/memstore/hashstore.rb', line 27 def to_hash { key: @key, items: @items } end |
#to_json ⇒ Object
9 10 11 |
# File 'lib/memstore/json.rb', line 9 def to_json self.to_hash.to_json end |
#to_json_file(file) ⇒ Object
13 14 15 |
# File 'lib/memstore/json.rb', line 13 def to_json_file(file) IO.write(file, self.to_json) end |
#to_msgpack ⇒ Object
9 10 11 |
# File 'lib/memstore/msgpack.rb', line 9 def to_msgpack self.to_hash.to_msgpack end |
#to_msgpack_file(file) ⇒ Object
13 14 15 |
# File 'lib/memstore/msgpack.rb', line 13 def to_msgpack_file(file) IO.write(file, self.to_msgpack) end |
#to_yaml ⇒ Object
9 10 11 |
# File 'lib/memstore/yaml.rb', line 9 def to_yaml self.to_hash.to_yaml end |
#to_yaml_file(file) ⇒ Object
13 14 15 |
# File 'lib/memstore/yaml.rb', line 13 def to_yaml_file(file) IO.write(file, self.to_yaml) end |