Class: Herve::Cache::Entry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#pathObject (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

#dirObject



13
14
15
# File 'lib/herve/cache/entry.rb', line 13

def dir
  @path.parent
end

#exist?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/herve/cache/entry.rb', line 45

def exist?
  path.exist?
end

#readObject



21
22
23
# File 'lib/herve/cache/entry.rb', line 21

def read
  exist? ? File.read(path) : nil
end

#read_jsonObject



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

#shardObject



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