Class: QiitaTrend::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/qiita_trend/cache.rb

Constant Summary collapse

DEFAULT_CACHE_DIRECTORY =

キャッシュファイルが格納されるデフォルトのディレクトリ

Dir.home + '/qiita_cache/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, directory = DEFAULT_CACHE_DIRECTORY) ⇒ Cache

コンストラクタ



12
13
14
15
16
# File 'lib/qiita_trend/cache.rb', line 12

def initialize(file_name, directory = DEFAULT_CACHE_DIRECTORY)
  @file_name = file_name
  @directory = directory
  @full_path = "#{directory}#{file_name}"
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



5
6
7
# File 'lib/qiita_trend/cache.rb', line 5

def directory
  @directory
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



5
6
7
# File 'lib/qiita_trend/cache.rb', line 5

def file_name
  @file_name
end

#full_pathObject (readonly)

Returns the value of attribute full_path.



5
6
7
# File 'lib/qiita_trend/cache.rb', line 5

def full_path
  @full_path
end

Instance Method Details

#cached?Boolean

キャッシュファイルが存在するか?

Returns:

  • (Boolean)


33
34
35
# File 'lib/qiita_trend/cache.rb', line 33

def cached?
  File.exist?(@full_path)
end

#create_cache(content) ⇒ Object

キャッシュファイルを作成する



20
21
22
23
24
25
# File 'lib/qiita_trend/cache.rb', line 20

def create_cache(content)
  Dir.mkdir(@directory) unless Dir.exist?(@directory)
  File.open(@full_path, 'wb') do |file|
    file.print(content)
  end
end

#load_cacheObject

キャッシュファイルを読み込む



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

def load_cache
  File.open(@full_path, 'r', &:read)
end