Class: QuiverToolbox::Note

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

Constant Summary collapse

EXTENSION =
'qvnote'
META_JSON_FILE_NAME =
'meta.json'
CONTENT_JSON_FILE_NAME =
'content.json'
KEY_CREATED_AT =
'created_at'
KEY_TAGS =
'tags'
KEY_TITLE =
'title'
KEY_UPDATED_AT =
'updated_at'
KEY_UUID =
'uuid'
KEY_CELLS =
'cells'
JSON_KEYS =
[
  KEY_CREATED_AT,
  KEY_TAGS,
  KEY_TITLE,
  KEY_UPDATED_AT,
  KEY_UUID,
  KEY_CELLS
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = nil) {|_self| ... } ⇒ Note

Returns a new instance of Note.

Yields:

  • (_self)

Yield Parameters:



45
46
47
48
49
50
# File 'lib/quiver_toolbox/note.rb', line 45

def initialize(attributes = nil)
  attributes.each do |k, v|
    send("#{k.to_s}=", v) if respond_to?("#{k.to_s}=")
  end if attributes
  yield self if block_given?
end

Instance Attribute Details

#fileObject

Returns the value of attribute file.



44
45
46
# File 'lib/quiver_toolbox/note.rb', line 44

def file
  @file
end

#notebook_pathObject

Returns the value of attribute notebook_path.



44
45
46
# File 'lib/quiver_toolbox/note.rb', line 44

def notebook_path
  @notebook_path
end

Class Method Details

.open(note_file_path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/quiver_toolbox/note.rb', line 24

def self.open(note_file_path)
  dir = note_file_path
  content_json_file = File.join(dir, 'content.json')
  content_json = JSON.load(File.open(content_json_file).read)

  meta_json_file = File.join(dir, 'meta.json')
  meta_json = JSON.load(File.open(meta_json_file).read)

  QuiverToolbox::Note.new do |n|
    n.created_at = meta_json['created_at']
    n.tags = meta_json['tags']
    n.title = meta_json['title']
    n.updated_at = meta_json['updated_at']
    n.uuid = meta_json['uuid']
    n.cells = content_json['cells']
  end
end

Instance Method Details

#content_json_fileObject



89
90
91
# File 'lib/quiver_toolbox/note.rb', line 89

def content_json_file
  File.join(file_name_with_path, CONTENT_JSON_FILE_NAME)
end

#content_json_hashObject



94
95
96
97
98
99
# File 'lib/quiver_toolbox/note.rb', line 94

def content_json_hash
  {
    KEY_TITLE => @title,
    KEY_CELLS => @cells
  }
end

#content_json_stringObject



102
103
104
# File 'lib/quiver_toolbox/note.rb', line 102

def content_json_string
  JSON.pretty_generate(content_json_hash)
end

#file_nameObject



53
54
55
# File 'lib/quiver_toolbox/note.rb', line 53

def file_name
  "#{uuid}.#{EXTENSION}"
end

#file_name_with_pathObject



63
64
65
# File 'lib/quiver_toolbox/note.rb', line 63

def file_name_with_path
  File.join(notebook_path, file_name)
end

#meta_json_fileObject



68
69
70
# File 'lib/quiver_toolbox/note.rb', line 68

def meta_json_file
  File.join(file_name_with_path, META_JSON_FILE_NAME)
end

#meta_json_hashObject



73
74
75
76
77
78
79
80
81
# File 'lib/quiver_toolbox/note.rb', line 73

def meta_json_hash
  {
    KEY_CREATED_AT => @created_at,
    KEY_TAGS => @tags,
    KEY_TITLE => @title,
    KEY_UPDATED_AT => @updated_at,
    KEY_UUID => @uuid
  }
end

#meta_json_stringObject



84
85
86
# File 'lib/quiver_toolbox/note.rb', line 84

def meta_json_string
  JSON.pretty_generate(meta_json_hash)
end

#storeObject



107
108
109
110
# File 'lib/quiver_toolbox/note.rb', line 107

def store
  store_meta_json
  store_content_json
end

#store_content_json(store_file = content_json_file) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/quiver_toolbox/note.rb', line 122

def store_content_json(store_file = content_json_file)
  FileUtils.mkdir_p(file_name_with_path)
  File.open(store_file, 'w') do |f|
    f.puts content_json_string
  end
  self
end

#store_meta_json(store_file = meta_json_file) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/quiver_toolbox/note.rb', line 113

def store_meta_json(store_file = meta_json_file)
  FileUtils.mkdir_p(file_name_with_path)
  File.open(store_file, 'w') do |f|
    f.puts meta_json_string
  end
  self
end