Skip to content

Commit c9e712b

Browse files
committed
Change the structure of JsonGenerator
assign color as instance variable
1 parent 0007406 commit c9e712b

File tree

2 files changed

+27
-29
lines changed

2 files changed

+27
-29
lines changed

lib/ever2boost/json_generator.rb

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,31 @@
11
module Ever2boost
22
class JsonGenerator
3-
FOLDER_COLORS = [
4-
'#E10051',
5-
'#FF8E00',
6-
'#E8D252',
7-
'#3FD941',
8-
'#30D5C8',
9-
'#2BA5F7',
10-
'#B013A4'
11-
].freeze
12-
133
class << self
144
def build(notebook_list)
155
folders = <<-EOS
166
EOS
177

188
notebook_list.each do |list|
19-
random_color = self.random_color
209
brace = notebook_list.last == list ? '}' : '},'
2110
folders += <<-EOS
22-
{
23-
"key": "#{list.hash}",
24-
"name": "#{list.title}",
25-
"color": "#{random_color}"
26-
#{brace}
11+
{
12+
"key": "#{list.hash}",
13+
"name": "#{list.title}",
14+
"color": "#{list.color}"
15+
#{brace}
2716
EOS
2817
end
2918

3019
json = <<-EOS
31-
{
32-
"folders": [
33-
#{folders}
34-
],
35-
"version": "1.0"
36-
}
20+
{
21+
"folders": [
22+
#{folders.chomp}
23+
],
24+
"version": "1.0"
25+
}
3726
EOS
3827
end
3928

40-
def random_color
41-
index = ((Random.new().rand * 7)*10.floor % 7).to_i
42-
FOLDER_COLORS[index]
43-
end
44-
4529
def output(notebook_list, output_dir)
4630
File.open("#{output_dir}/boostnote.json","w") do |f|
4731
f.write(self.build(notebook_list))

lib/ever2boost/note_list.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@ module Ever2boost
44
class NoteList
55
DEFAULT_BYTES_NUMBER = 6
66

7-
attr_accessor :title, :hash, :guid
7+
FOLDER_COLORS = [
8+
'#E10051',
9+
'#FF8E00',
10+
'#E8D252',
11+
'#3FD941',
12+
'#30D5C8',
13+
'#2BA5F7',
14+
'#B013A4'
15+
].freeze
16+
17+
attr_accessor :title, :hash, :guid, :color
18+
19+
def initialize(title: nil, hash: nil, guid: nil, color: nil)
20+
index = ((Random.new().rand * 7)*10.floor % 7).to_i
21+
color = FOLDER_COLORS[index]
822

9-
def initialize(title: nil, hash: nil, guid: nil)
1023
@title = title
1124
@hash = SecureRandom.hex(DEFAULT_BYTES_NUMBER)
1225
@guid = guid
26+
@color = color
1327
end
1428
end
1529
end

0 commit comments

Comments
 (0)