Class: Database

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/database.rb

Constant Summary collapse

ARCHIVE_ROOT_DIR_PATH =
"小説データ/"
DATABASE_NAME =
"database"
@@archive_root_path =
File.expand_path(File.join(Narou.get_root_dir, ARCHIVE_ROOT_DIR_PATH))

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



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

def initialize
  @database = LocalSetting.get[DATABASE_NAME]
end

Class Method Details

.archive_root_pathObject

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



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

def self.archive_root_path
  @@archive_root_path
end

.initObject

データベース初期設定



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

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

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  @database[key]
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  @database[key] = value
end

#delete(key) ⇒ Object



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

def delete(key)
  @database.delete(key)
end

#each(&block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/database.rb', line 30

def each(&block)
  if block
    @database.each(&block)
  else
    @database.each
  end
end

#each_key(&block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/database.rb', line 38

def each_key(&block)
  if block
    @database.each_key(&block)
  else
    @database.each_key
  end
end

#get_data(type, value) ⇒ Object



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

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

#get_id(type, value) ⇒ Object



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

def get_id(type, value)
  data = get_data(type, value)
  if data
    return data["id"]
  end
  nil
end

#get_new_idObject



99
100
101
102
103
# File 'lib/database.rb', line 99

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

#get_objectObject



75
76
77
# File 'lib/database.rb', line 75

def get_object
  @database
end

#novel_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#save_databaseObject



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

def save_database
  LocalSetting.get.save_settings(DATABASE_NAME)
end

#sort_by_last_updateObject

last_update で更新順にソート



108
109
110
111
# File 'lib/database.rb', line 108

def sort_by_last_update
  values = @database.values.sort_by { |v| v["last_update"] }.reverse
  values
end