Class: QuiverToolbox::Note

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

Constant Summary collapse

EXTENSION =
'qvnote'
RESOURCES_DIR =
'resources/'
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:



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

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.



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

def file
  @file
end

#notebook_pathObject

Returns the value of attribute notebook_path.



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

def notebook_path
  @notebook_path
end

#resourcesObject (readonly)

Returns the value of attribute resources.



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

def resources
  @resources
end

Class Method Details

.open(note_file_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/quiver_toolbox/note.rb', line 25

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)

  notebook_path = nil
  Pathname.new(dir).ascend do |v|
    notebook_path =  v.expand_path.to_s if v.basename.to_s.match('.qvnotebook')
  end

  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']
    n.notebook_path = notebook_path
  end
end

Instance Method Details

#content_json_fileObject



111
112
113
# File 'lib/quiver_toolbox/note.rb', line 111

def content_json_file
  File.join(file_name_with_path, CONTENT_JSON_FILE_NAME)
end

#content_json_hashObject



116
117
118
119
120
121
# File 'lib/quiver_toolbox/note.rb', line 116

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

#content_json_stringObject



124
125
126
# File 'lib/quiver_toolbox/note.rb', line 124

def content_json_string
  JSON.pretty_generate(content_json_hash)
end

#file_nameObject



61
62
63
# File 'lib/quiver_toolbox/note.rb', line 61

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

#file_name_with_pathObject



71
72
73
# File 'lib/quiver_toolbox/note.rb', line 71

def file_name_with_path
  File.join(notebook_path, file_name)
end

#meta_json_fileObject



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

def meta_json_file
  File.join(file_name_with_path, META_JSON_FILE_NAME)
end

#meta_json_hashObject



95
96
97
98
99
100
101
102
103
# File 'lib/quiver_toolbox/note.rb', line 95

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



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

def meta_json_string
  JSON.pretty_generate(meta_json_hash)
end

#resources_dirObject



76
77
78
# File 'lib/quiver_toolbox/note.rb', line 76

def resources_dir
  File.join(file_name_with_path, RESOURCES_DIR)
end

#src_resources=(src_files) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/quiver_toolbox/note.rb', line 81

def src_resources=(src_files)
  @resources = []
  src_files.each do |src_file|
    dist_file = File.join(resources_dir, "#{QuiverToolbox::Util.rename_to_uuid_file(src_file)}")
    @resources << {'src' => src_file, 'dist' => dist_file}
  end
end

#storeObject



129
130
131
132
133
134
# File 'lib/quiver_toolbox/note.rb', line 129

def store
  store_meta_json
  store_content_json
  store_resources if @resources
  self
end

#store_content_json(store_file = content_json_file) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/quiver_toolbox/note.rb', line 155

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



146
147
148
149
150
151
152
# File 'lib/quiver_toolbox/note.rb', line 146

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

#store_resourcesObject



137
138
139
140
141
142
143
# File 'lib/quiver_toolbox/note.rb', line 137

def store_resources
  FileUtils.mkdir_p(resources_dir)
  @resources.each do |hash|
    FileUtils.cp(hash['src'], hash['dist'])
  end
  self
end