Class: Quiver::Note

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content:, meta:, path: nil, notebook: nil) ⇒ Note

Returns a new instance of Note.



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

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

Instance Attribute Details

#metaObject

Returns the value of attribute meta.



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

def meta
  @meta
end

#notebookObject

Returns the value of attribute notebook.



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

def notebook
  @notebook
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#as_json(options = nil) ⇒ Object



50
51
52
53
# File 'lib/quiver/note.rb', line 50

def as_json(options = nil)
  return super if options
  {content: @content, meta: @meta, path: @path.to_s}
end

#cellsObject



28
29
30
31
32
33
34
35
36
# File 'lib/quiver/note.rb', line 28

def cells
  content['cells'].map do |cell|
    case cell['type']
    when 'text';     Quiver::Cell::Text.new(cell, self)
    when 'markdown'; Quiver::Cell::Markdown.new(cell, self)
    else             Quiver::Cell.new(cell, self)
    end
  end
end

#contentObject



23
24
25
26
27
# File 'lib/quiver/note.rb', line 23

def content
  @content ||= begin
    JSON.parse(@adapter.load(@path + 'content.json'))
  end
end

#created_atObject



11
12
13
# File 'lib/quiver/note.rb', line 11

def created_at
  Time.at(meta['created_at'])
end

#image_url(path) ⇒ Object



40
41
42
43
# File 'lib/quiver/note.rb', line 40

def image_url(path)
  rel_path = path.gsub('quiver-image-url/', '')
  @adapter.image_url(@path + 'resources' + rel_path)
end

#saveObject



44
45
46
47
48
49
# File 'lib/quiver/note.rb', line 44

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

#titleObject



17
18
19
# File 'lib/quiver/note.rb', line 17

def title
  @meta['title']
end

#to_htmlObject



37
38
39
# File 'lib/quiver/note.rb', line 37

def to_html
  cells.map(&:to_html).join
end

#updated_atObject



14
15
16
# File 'lib/quiver/note.rb', line 14

def updated_at
  Time.at(meta['updated_at'])
end

#uuidObject



20
21
22
# File 'lib/quiver/note.rb', line 20

def uuid
  @meta['uuid']
end