Class: Herve::Cache::Entry
- Inherits:
-
Object
- Object
- Herve::Cache::Entry
- Defined in:
- lib/herve/cache/entry.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #dir ⇒ Object
- #exist? ⇒ Boolean
-
#initialize(dir, file) ⇒ Entry
constructor
A new instance of Entry.
- #read ⇒ Object
- #read_json ⇒ Object
- #shard ⇒ Object
- #write(data) ⇒ Object
- #write_json(data) ⇒ Object
Constructor Details
#initialize(dir, file) ⇒ Entry
Returns a new instance of Entry.
9 10 11 |
# File 'lib/herve/cache/entry.rb', line 9 def initialize(dir, file) @path = dir.join(file) end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
7 8 9 |
# File 'lib/herve/cache/entry.rb', line 7 def path @path end |
Instance Method Details
#dir ⇒ Object
13 14 15 |
# File 'lib/herve/cache/entry.rb', line 13 def dir @path.parent end |
#exist? ⇒ Boolean
45 46 47 |
# File 'lib/herve/cache/entry.rb', line 45 def exist? path.exist? end |
#read ⇒ Object
21 22 23 |
# File 'lib/herve/cache/entry.rb', line 21 def read exist? ? File.read(path) : nil end |
#read_json ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/herve/cache/entry.rb', line 25 def read_json data = read return nil if data.nil? begin JSON.parse(data) rescue JSON::ParserError => e raise CacheError, "entry #{path} should be JSON but is not: #{e.message}" end end |
#shard ⇒ Object
17 18 19 |
# File 'lib/herve/cache/entry.rb', line 17 def shard Shard.new(dir) end |
#write(data) ⇒ Object
36 37 38 39 |
# File 'lib/herve/cache/entry.rb', line 36 def write(data) @path.parent.mkpath unless @path.parent.exist? File.write(path, data) end |
#write_json(data) ⇒ Object
41 42 43 |
# File 'lib/herve/cache/entry.rb', line 41 def write_json(data) write(data.to_json) end |