Class: QiitaTrend::Cache
- Inherits:
-
Object
- Object
- QiitaTrend::Cache
- Defined in:
- lib/qiita_trend/cache.rb
Overview
Qiitaのトレンドをキャッシュする機能を提供する
Constant Summary collapse
- DEFAULT_CACHE_DIRECTORY =
キャッシュファイルが格納されるデフォルトのディレクトリ
Dir.home + '/qiita_cache/'
Instance Attribute Summary collapse
-
#directory ⇒ String
readonly
キャッシュファイルを格納するディレクトリ(デフォルト値はDEFAULT_CACHE_DIRECTORYになります).
-
#file_name ⇒ String
readonly
ファイル名.
-
#full_path ⇒ String
readonly
キャッシュファイルへのフルパス.
Instance Method Summary collapse
-
#cached? ⇒ Boolean
キャッシュファイルが存在するかどうかを判定します.
-
#create_cache(content) ⇒ Object
キャッシュファイルを作成する ※キャッシュフォルダが存在しない場合は作成します.
-
#initialize(file_name, directory = DEFAULT_CACHE_DIRECTORY) ⇒ Cache
constructor
コンストラクタ Cacheクラスのインスタンスを返します.
-
#load_cache ⇒ Object
キャッシュファイルを読み込む File.openを使用しファイルを読み込みます.
Constructor Details
#initialize(file_name, directory = DEFAULT_CACHE_DIRECTORY) ⇒ Cache
コンストラクタ Cacheクラスのインスタンスを返します
21 22 23 24 25 |
# File 'lib/qiita_trend/cache.rb', line 21 def initialize(file_name, directory = DEFAULT_CACHE_DIRECTORY) @file_name = file_name @directory = directory @full_path = "#{directory}#{file_name}" end |
Instance Attribute Details
#directory ⇒ String (readonly)
Returns キャッシュファイルを格納するディレクトリ(デフォルト値はDEFAULT_CACHE_DIRECTORYになります).
9 10 11 |
# File 'lib/qiita_trend/cache.rb', line 9 def directory @directory end |
#file_name ⇒ String (readonly)
Returns ファイル名.
7 8 9 |
# File 'lib/qiita_trend/cache.rb', line 7 def file_name @file_name end |
#full_path ⇒ String (readonly)
Returns キャッシュファイルへのフルパス.
11 12 13 |
# File 'lib/qiita_trend/cache.rb', line 11 def full_path @full_path end |
Instance Method Details
#cached? ⇒ Boolean
キャッシュファイルが存在するかどうかを判定します
49 50 51 |
# File 'lib/qiita_trend/cache.rb', line 49 def cached? File.exist?(@full_path) end |
#create_cache(content) ⇒ Object
キャッシュファイルを作成する ※キャッシュフォルダが存在しない場合は作成します
31 32 33 34 35 36 |
# File 'lib/qiita_trend/cache.rb', line 31 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
キャッシュファイルを読み込む File.openを使用しファイルを読み込みます
42 43 44 |
# File 'lib/qiita_trend/cache.rb', line 42 def load_cache File.open(@full_path, 'r', &:read) end |