class Store
A file backed storage class
Attributes
Public Class Methods
Get the Singleton
param
void return
Object
@@instance
# File lib/handset_detection/store.rb, line 52 def self.get_instance @@instance = self.new if @@instance == nil @@instance end
Constructor
# File lib/handset_detection/store.rb, line 39 def initialize @dirname = "hd40store" @path = "" @directory = "" @cache = nil @config = {} end
Public Instance Methods
Fetch data from disk
param
string $key. return
mixed
# File lib/handset_detection/store.rb, line 125 def fetch(key) begin jsonstr = File.read(File.join(@directory, "#{key}.json")) return JSON.parse(jsonstr) rescue end false end
Returns all devices inside one giant array
Used by localDevice* functions to iterate over all devies
param
void return
array All devices in one giant assoc array
# File lib/handset_detection/store.rb, line 141 def fetch_devices data = {'devices' => []} Dir.glob(File.join(@directory, 'Device*.json')).each do |file| jsonstr = File.read file return false if not jsonstr or jsonstr.blank? data['devices'] << JSON.parse(jsonstr) end data end
Moves a json file into storage.
param
string $srcAbsName The fully qualified path and file name eg /tmp/sjjhas778hsjhh param
string $destName The key name inside the cache eg Device_19.json return
boolean true on success, false otherwise
# File lib/handset_detection/store.rb, line 157 def move_in(src_abs_name, dest_name) FileUtils.mv src_abs_name, File.join(@directory, dest_name) end
Cleans out the store - Use with caution
param
void return
true on success, false otherwise
# File lib/handset_detection/store.rb, line 166 def purge files = Dir.glob File.join(@directory, '*.json') files.each do |file| if File.file? file return false unless File.unlink file end end @cache.purge end
Read $data, try cache first
param
sting $key Key to search for return
boolean true on success, false
# File lib/handset_detection/store.rb, line 111 def read(key) reply = @cache.read(key) return reply unless reply.blank? reply = fetch(key) return false if reply.blank? @cache.write(key, reply) return reply end
Sets the storage config options, optionally creating the storage directory.
param
array $config An assoc array of config info. param
boolean $createDirectory return
void
# File lib/handset_detection/store.rb, line 63 def set_config(config, create_directory=false) config.each { |key, value| @config[key] = value } @path = @config.include?('filesdir') ? @config['filesdir'] : File.dirname(__FILE__) @directory = File.join @path, @dirname @cache = Cache.new(@config) if create_directory unless File.directory? @directory unless FileUtils.mkdir_p @directory raise("Error : Failed to create storage directory at #{@directory}. Check permissions.") end end end end
Store
data to disk
param
string $key The search key (becomes the filename .. so keep it alphanumeric) param
array $data Data to persist (will be persisted in json format) return
boolean true on success, false otherwise
# File lib/handset_detection/store.rb, line 96 def store(key, data) jsonstr = JSON.generate data begin File.open(File.join(@directory, "#{key}.json"), 'w') { |f| f.write jsonstr } return true rescue end false end
Write data to cache & disk
param
string $key param
array $data return
boolean true on success, false otherwise
# File lib/handset_detection/store.rb, line 84 def write(key, data) return false if data.blank? return false unless store(key, data) @cache.write(key, data) end