Class: QiitaTrend::Cache
- Inherits:
-
Object
- Object
- QiitaTrend::Cache
- Defined in:
- lib/qiita_trend/cache.rb
Constant Summary collapse
- DEFAULT_CACHE_DIRECTORY =
キャッシュファイルが格納されるデフォルトのディレクトリ
Dir.home + '/qiita_cache/'
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#full_path ⇒ Object
readonly
Returns the value of attribute full_path.
Instance Method Summary collapse
-
#cached? ⇒ Boolean
キャッシュファイルが存在するか?.
-
#create_cache(content) ⇒ Object
キャッシュファイルを作成する キャッシュフォルダが存在しない時は作成する.
-
#initialize(file_name, directory = DEFAULT_CACHE_DIRECTORY) ⇒ Cache
constructor
コンストラクタ クラス内で使用するインスタンス変数をセットする.
-
#load_cache ⇒ Object
キャッシュファイルを読み込む.
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
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
5 6 7 |
# File 'lib/qiita_trend/cache.rb', line 5 def directory @directory end |
#file_name ⇒ Object (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_path ⇒ Object (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
キャッシュファイルが存在するか?
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_cache ⇒ Object
キャッシュファイルを読み込む
28 29 30 |
# File 'lib/qiita_trend/cache.rb', line 28 def load_cache File.open(@full_path, 'r', &:read) end |