diff --git a/src/library.cr b/src/library.cr index a0cd9b1..e91d885 100644 --- a/src/library.cr +++ b/src/library.cr @@ -40,6 +40,16 @@ class SortOptions self.new method, ascend end + def self.from_info_json(dir, username) + opt = SortOptions.new + TitleInfo.new dir do |info| + if info.sort_by.has_key? username + opt = SortOptions.from_tuple info.sort_by[username] + end + end + opt + end + def to_tuple {@method.to_s.underscore, ascend} end @@ -489,7 +499,7 @@ class Title # When `opt` is not nil, it saves the options to info.json def sorted_entries(username, opt : SortOptions? = nil) if opt.nil? - opt = load_sort_options username + opt = SortOptions.from_info_json @dir, username else TitleInfo.new @dir do |info| info.sort_by[username] = opt.to_tuple @@ -528,16 +538,6 @@ class Title ary end - def load_sort_options(username) - opt = SortOptions.new - TitleInfo.new @dir do |info| - if info.sort_by.has_key? username - opt = SortOptions.from_tuple info.sort_by[username] - end - end - opt - end - # === helper methods === # Gets the last read entry in the title. If the entry has been completed, @@ -793,7 +793,7 @@ class Library def sorted_titles(username, opt : SortOptions? = nil) if opt.nil? - opt = load_sort_options username + opt = SortOptions.from_info_json @dir, username else TitleInfo.new @dir do |info| info.sort_by[username] = opt.to_tuple @@ -825,14 +825,4 @@ class Library ary end - - def load_sort_options(username) - opt = SortOptions.new - TitleInfo.new @dir do |info| - if info.sort_by.has_key? username - opt = SortOptions.from_tuple info.sort_by[username] - end - end - opt - end end diff --git a/src/routes/main.cr b/src/routes/main.cr index 00c5582..8699223 100644 --- a/src/routes/main.cr +++ b/src/routes/main.cr @@ -41,7 +41,7 @@ class MainRouter < Router begin username = get_username env - sort_opt = @context.library.load_sort_options username + sort_opt = SortOptions.from_info_json @context.library.dir, username get_sort_opt titles = @context.library.sorted_titles username, sort_opt @@ -59,7 +59,7 @@ class MainRouter < Router title = (@context.library.get_title env.params.url["title"]).not_nil! username = get_username env - sort_opt = title.load_sort_options username + sort_opt = SortOptions.from_info_json title.dir, username get_sort_opt entries = title.sorted_entries username, sort_opt