Class: Measured::Cache::Json
- Inherits:
-
Object
- Object
- Measured::Cache::Json
- Defined in:
- lib/measured/cache/json.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #exist? ⇒ Boolean
-
#initialize(filename) ⇒ Json
constructor
A new instance of Json.
- #read ⇒ Object
- #write(table) ⇒ Object
Constructor Details
#initialize(filename) ⇒ Json
Returns a new instance of Json.
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
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
4 5 6 |
# File 'lib/measured/cache/json.rb', line 4 def filename @filename end |
#path ⇒ Object (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
12 13 14 |
# File 'lib/measured/cache/json.rb', line 12 def exist? File.exist?(@path) end |
#read ⇒ Object
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
21 22 23 |
# File 'lib/measured/cache/json.rb', line 21 def write(table) raise ArgumentError, "Cannot overwrite file cache at runtime." end |