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.

Raises:

  • (ArgumentError)


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

def initialize(filename)
  @filename = filename
  raise ArgumentError, "Invalid cache file: #{filename}" unless %w[length.json weight.json volume.json test.json].include?(filename.to_s)
  @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)


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

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

#readObject



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

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

#write(table) ⇒ Object

Raises:

  • (ArgumentError)


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

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