Class: Measured::Cache::Json

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Json

Returns a new instance of Json.



6
7
8
9
# File 'lib/measured/cache/json.rb', line 6

def initialize(filename)
  @filename = filename
  @path = Pathname.new(File.join(File.dirname(__FILE__), "../../../cache", @filename)).cleanpath
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/measured/cache/json.rb', line 4

def filename
  @filename
end

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/measured/cache/json.rb', line 4

def path
  @path
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/measured/cache/json.rb', line 11

def exist?
  File.exist?(@path)
end

#readObject



15
16
17
18
# File 'lib/measured/cache/json.rb', line 15

def read
  return unless exist?
  decode(JSON.load(File.read(@path), nil, freeze: true))
end

#write(table) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
# File 'lib/measured/cache/json.rb', line 20

def write(table)
  raise ArgumentError, "Cannot overwrite file cache at runtime."
end