class LapisLazuli::Storage
Simple storage class
Public Class Methods
new(name=nil)
click to toggle source
Initialize the storage with an optional name
# File lib/lapis_lazuli/storage.rb, line 18 def initialize(name=nil) @name = name @data = {} end
Public Instance Methods
destroy(world)
click to toggle source
This will be called during the destruction of the world
# File lib/lapis_lazuli/storage.rb, line 62 def destroy(world) # No data to write if @data.keys.length == 0 world.log.debug("Storage '#{@name}' is empty") return end filename = nil # If we have a name if !@name.nil? # Check the environment for a filename env_value = world.env("#{@name}_file", nil) if !env_value.nil? filename = env_value end end # Otherwise, generate a name if filename.nil? # Folder to store in filename = world.config("storage_dir", ".#{File::SEPARATOR}storage") + File::SEPARATOR # Filename if @name.nil? # Use current timestamp and the object_id of the data filename += world.time[:timestamp] + "_" + @data.object_id else # Use the given name filename += @name end # JSON file extension filename += ".json" end world.log.debug("Writing storage to: #{filename}") self.writeToFile(filename) end
get(key)
click to toggle source
# File lib/lapis_lazuli/storage.rb, line 27 def get(key) return @data[key] end
has?(key)
click to toggle source
# File lib/lapis_lazuli/storage.rb, line 31 def has?(key) return @data.include? key end
set(key, value)
click to toggle source
# File lib/lapis_lazuli/storage.rb, line 23 def set(key, value) @data[key] = value end
writeToFile(filename=nil)
click to toggle source
Write all stored data to file
# File lib/lapis_lazuli/storage.rb, line 37 def writeToFile(filename=nil) if filename.nil? && @name.nil? raise "Need a filename" elsif filename.nil? filename = "#{@name}.json" end # Make storage directory dir = File.dirname(filename) begin Dir.mkdir dir rescue SystemCallError => ex # Swallow this error; it occurs (amongst other situations) when the # directory exists. Checking for an existing directory beforehand is # not concurrency safe. end File.open(filename, 'w') { |file| # Write the JSON to the file file.puts(@data.to_json) } end