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
  refresh
end

Class Method Details

.archive_root_pathObject

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



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

def self.archive_root_path
  @@archive_root_path
end

.initObject

データベース初期設定



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

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

#[](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

#create_new_idObject



103
104
105
106
107
# File 'lib/database.rb', line 103

def create_new_id
  max_id = @database.keys.max
  id = max_id ? max_id + 1 : 0
  id
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



88
89
90
91
92
93
# File 'lib/database.rb', line 88

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

#get_id(type, value) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/database.rb', line 95

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

#get_objectObject



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

def get_object
  @database
end

#novel_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#refreshObject



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

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

#save_databaseObject



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

def save_database
  @database.save
end

#sort_by_last_updateObject

last_update で更新順にソート



112
113
114
115
# File 'lib/database.rb', line 112

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