class Imgurr::Storage
Constants
- JSON_FILE
Attributes
Public: the in-memory collection of all Lists attached to this Storage
instance.
lists - an Array of individual List items
Returns nothing.
Public Class Methods
Public Instance Methods
Public: Adds a deletehash to the hashes list
id - Image ID hash - Delete hash
Returns nothing
# File lib/imgurr/storage.rb, line 41 def add_hash(id, hash, source) @hashes[id] = {:deletehash => hash, :source => source, :stamp => Time.now} end
Takes care of bootstrapping the Json file, both in terms of creating the file and in terms of creating a skeleton Json schema.
Return true if successfully saved.
# File lib/imgurr/storage.rb, line 91 def bootstrap return if File.exist?(json_file) FileUtils.touch json_file File.open(json_file, 'w') {|f| f.write(to_json) } save end
Public: delete an Item entry from storage.
Returns the deleted Item or nil.
# File lib/imgurr/storage.rb, line 74 def delete(id) @hashes.delete(id) end
Public: finds any given delete_hash by id.
name - String name of the list to search for
Returns the first instance of delete_hash that it finds.
# File lib/imgurr/storage.rb, line 59 def find(id) hash = @hashes[id] hash ? hash[:deletehash] : nil end
Public: test whether out storage contains the delete hash for given id
id - ID of the image
Returns true if found, false if not.
# File lib/imgurr/storage.rb, line 50 def hash_exists?(id) @hashes.has_key? id end
Public: all Items in storage sorted in chronological order.
Returns an Array of all Items.
# File lib/imgurr/storage.rb, line 67 def items @hashes.to_a.sort {|(_, a), (_, b)| a[:stamp] <=> b[:stamp] } end
Public: the path to the Json file used by imgurr.
ENV is mostly used for tests
Returns the String path of imgurr's Json representation.
# File lib/imgurr/storage.rb, line 14 def json_file ENV['IMGURRFILE'] || JSON_FILE end
Take a JSON representation of data and explode it out into the constituent Lists and Items for the given Storage
instance.
Returns all hashes.
# File lib/imgurr/storage.rb, line 102 def populate file = File.read(json_file) storage = JSON.parse(file, :symbolize_names => true) @hashes = storage[:hashes] convert if @hashes.is_a? Array @hashes end
Public: persists your in-memory objects to disk in Json format.
lists_Json - list in Json format
Returns true if successful, false if unsuccessful.
# File lib/imgurr/storage.rb, line 117 def save File.open(json_file, 'w') {|f| f.write(to_json) } end
Public: creates a Hash of the representation of the in-memory data structure. This percolates down to Items by calling to_hash
on the List, which in tern calls to_hash
on individual Items.
Returns a Hash of the entire data set.
# File lib/imgurr/storage.rb, line 83 def to_hash {:hashes => @hashes} end
Public: the Json representation of the current List and Item assortment attached to the Storage
instance.
Returns a String Json representation of its Lists and their Items.
# File lib/imgurr/storage.rb, line 125 def to_json JSON.pretty_generate(to_hash) end
Private Instance Methods
Private: convert from old Json representation, filling in the missing data. Also print a warning message for the user.
Returns nothing.
# File lib/imgurr/storage.rb, line 134 def convert old = @hashes @hashes = Hash.new puts 'Warning: old JSON format detected, converting.' old.each {|i| add_hash(i[:id], i[:deletehash], 'unknown') } save end