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.



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

def initialize
  refresh
end

Class Method Details

.archive_root_pathObject

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



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

def self.archive_root_path
  @archive_root_path ||= Narou.root_dir.join(ARCHIVE_ROOT_DIR_PATH).expand_path
end

.initObject

データベース初期設定



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

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



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

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

#get_data(type, value) ⇒ Object



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

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) だと、アドレスが仕様変更した場合に、 古いままのデータとマッチングしなくなるため



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

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



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

def get_object
  @database
end

#idsObject



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

def ids
  @database.keys
end

#novel_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#refreshObject



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

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

#save_databaseObject



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

def save_database
  @database.save
end

#sort_by(key, reverse: true) ⇒ Object



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

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

#tag_indexiesObject



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

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