Class: Database

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Singleton
Defined in:
lib/database.rb

Constant Summary collapse

ARCHIVE_ROOT_DIR_PATH =
"小説データ/"
DATABASE_NAME =
"database"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



23
24
25
# File 'lib/database.rb', line 23

def initialize
  refresh
end

Class Method Details

.archive_root_pathObject

小説格納用のルートディレクトリを取得



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

def self.archive_root_path
  @archive_root_path ||= File.expand_path(File.join(Narou.get_root_dir, ARCHIVE_ROOT_DIR_PATH))
end

.initObject

データベース初期設定



34
35
36
37
38
39
# File 'lib/database.rb', line 34

def self.init
  unless File.exist?(ARCHIVE_ROOT_DIR_PATH)
    FileUtils.mkdir(ARCHIVE_ROOT_DIR_PATH)
    puts ARCHIVE_ROOT_DIR_PATH + " を作成しました"
  end
end

Instance Method Details

#create_new_idObject



83
84
85
86
87
# File 'lib/database.rb', line 83

def create_new_id
  max_id = @database.keys.max
  id = max_id ? max_id + 1 : 0
  id
end

#get_data(type, value) ⇒ Object



65
66
67
68
69
70
# File 'lib/database.rb', line 65

def get_data(type, value)
  @database.each_value do |data|
    return data if data[type] == value
  end
  nil
end

#get_data_by_toc_url(toc_url, site_setting) ⇒ Object

SiteSetting を使って toc url を正規化してマッチングする。 get_data(“toc_url”, url) だと、アドレスが仕様変更した場合に、 古いままのデータとマッチングしなくなるため



75
76
77
78
79
80
81
# File 'lib/database.rb', line 75

def get_data_by_toc_url(toc_url, site_setting)
  @database.each_value do |data|
    site_setting.multi_match_once(data["toc_url"], "url") or next
    return data if site_setting["toc_url"] == toc_url
  end
  nil
end

#get_objectObject



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

def get_object
  @database
end

#idsObject



56
57
58
# File 'lib/database.rb', line 56

def ids
  @database.keys
end

#novel_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/database.rb', line 60

def novel_exists?(id)
  return nil if id.nil?
  @database.keys.include?(id.to_i)
end

#refreshObject



27
28
29
# File 'lib/database.rb', line 27

def refresh
  @database = Inventory.load(DATABASE_NAME)
end

#save_databaseObject



48
49
50
# File 'lib/database.rb', line 48

def save_database
  @database.save
end

#sort_by(key, reverse: true) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/database.rb', line 89

def sort_by(key, reverse: true)
  values = @database.values.sort_by { |v| v[key] }
  if reverse
    values.reverse
  else
    values
  end
end

#tag_indexiesObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/database.rb', line 98

def tag_indexies
  result = Hash.new { [] }
  @database.each do |id, data|
    tags = data["tags"] || []
    tags.each do |tag|
      result[tag.to_s] |= [id]
    end
  end
  result
end