Class: Zooline::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/zooline/storage.rb

Constant Summary collapse

JSON_FILE =
"#{ENV['HOME']}/.zooline"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStorage

Public: initializes a Storage instance by loading in your persisted data.

Returns the Storage instance.



23
24
25
26
# File 'lib/zooline/storage.rb', line 23

def initialize
  @bookmarks = []
  explode_json(json_file)
end

Instance Attribute Details

#bookmarks(o = nil) ⇒ Object

Public: the list of Lists in your JSON data, sorted by number of items descending.

Returns an Array of List objects.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/zooline/storage.rb', line 40

def bookmarks(o = nil)
			if o == 'date'
@bookmarks.sort_by { |bk| -bk.added.to_i }
			elsif o == 'title'
@bookmarks.sort_by { |bk| bk.title }
			elsif o == 'uid'
@bookmarks.sort_by { |bk| bk.uid }
			else
@bookmarks.sort_by { |bk| -bk.added.to_i }
			end
end

Instance Method Details

#bookmark_exists?(title) ⇒ Boolean

Public: tests whether a named List exists.

name - the String name of a List

Returns true if found, false if not.

Returns:

  • (Boolean)


57
58
59
# File 'lib/zooline/storage.rb', line 57

def bookmark_exists?(title)
  @Bookmarks.detect { |bk| bk.title == title }
end

#bootstrap_jsonObject

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.



96
97
98
99
# File 'lib/zooline/storage.rb', line 96

def bootstrap_json
  FileUtils.touch json_file
  save!
end

#check?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/zooline/storage.rb', line 9

def check?
	!(@bookmarks.nil? || @bookmarks.empty?)
end

#explode_json(json) ⇒ Object

Take a JSON representation of data and explode it out into the consituent Lists and Items for the given Storage instance.

Returns nothing.



82
83
84
85
86
87
88
89
90
# File 'lib/zooline/storage.rb', line 82

def explode_json(json)
  bootstrap_json unless File.exist?(json)

  storage = Yajl::Parser.new.parse(File.new(json, 'r'))

  storage.each do |bookmark|
    @bookmarks << bk = Bookmark.new(bookmark['content'])
  end
end

#json_fileObject

Public: the path to the JSON file used by boom.

Returns the String path of boom’s JSON representation.



16
17
18
# File 'lib/zooline/storage.rb', line 16

def json_file
  JSON_FILE
end

#save!Object

Public: persists your in-memory objects to disk in JSON format.

Returns true if successful, false if unsuccessful.



64
65
66
# File 'lib/zooline/storage.rb', line 64

def save!
  File.open(json_file, 'w') {|f| f.write(to_json) }
end

#to_jsonObject

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.



72
73
74
# File 'lib/zooline/storage.rb', line 72

def to_json
  Yajl::Encoder.encode(bookmarks.collect{|b| b.to_hash }, :pretty => true)
end