Class: Quiver::Notebook

Inherits:
Object
  • Object
show all
Defined in:
lib/quiver/notebook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta:, path: nil, root: nil) ⇒ Notebook

Returns a new instance of Notebook.



4
5
6
7
8
9
# File 'lib/quiver/notebook.rb', line 4

def initialize(meta:, path: nil, root: nil)
  @meta = meta
  @path = Pathname.new(path) if path
  @root = root
  @adapter = root.adapter if root
end

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



3
4
5
# File 'lib/quiver/notebook.rb', line 3

def meta
  @meta
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/quiver/notebook.rb', line 3

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/quiver/notebook.rb', line 3

def root
  @root
end

Instance Method Details

#add(note) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/quiver/notebook.rb', line 26

def add(note)
  raise 'this note already added' if note.path
  note.meta['uuid'] ||= SecureRandom.uuid.upcase
  note.path = @path + "#{note.meta['uuid']}.qvnote"
  note.adapter = @adapter
  note.save
end

#each(include_content: false) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/quiver/notebook.rb', line 13

def each(include_content: false)
  raise 'This notebook has no root' unless @adapter
  @adapter.each(@path, 'qvnote') do |path|
    content = JSON.parse(@adapter.load(@path + path + 'content.json')) if include_content
    meta    = JSON.parse(@adapter.load(@path + path + 'meta.json'))
    yield Note.new(content: content, meta: meta, path: @path + path, notebook: self)
  end
end

#nameObject



10
11
12
# File 'lib/quiver/notebook.rb', line 10

def name
  @meta['name']
end

#saveObject



21
22
23
24
25
# File 'lib/quiver/notebook.rb', line 21

def save
  raise 'add to root first!' unless @path && @adapter
  adapter.mkpath(@path)
  adapter.save(@path + 'meta.json', @meta.to_json)
end