class OBFS::Store
Public Class Methods
new(attributes = {})
click to toggle source
# File lib/obfs/store.rb, line 5 def initialize(attributes = {}) # hash argument @path = (attributes.keys.include? :path) ? attributes[:path] : (File.join(Dir.home, '.obfs')) end
Public Instance Methods
_exist(term = '')
click to toggle source
searches directory contents (1 level) and returns boolean if term exist
# File lib/obfs/store.rb, line 103 def _exist(term = '') exist_space = Dir.entries(@path).reject { |k| k != term.to_s || k == '.' || k == '..' } rescue nil if !exist_space.nil? if exist_space.length > 0 true else false end else false end end
_find(term = '', records = 1000, tolerance = 50)
click to toggle source
searches directory contents (1 level) and returns array sorted by relevance
# File lib/obfs/store.rb, line 91 def _find(term = '', records = 1000, tolerance = 50) output = [] search_space = Dir.entries(@path).reject { |k| k == '.' || k == '..' } rescue [] search_space.each do |search_space_term| if OBFS::Levenshtein.distance(search_space_term, term) <= tolerance && OBFS::WhiteSimilarity.similarity(search_space_term, term) > 0.0 output << search_space_term end end output.first(records) end
_index()
click to toggle source
returns directory contents in an array
# File lib/obfs/store.rb, line 86 def _index Dir.entries(@path).reject { |k| k == '.' || k == '..' } rescue nil end
_path()
click to toggle source
returns current working path for obfs
# File lib/obfs/store.rb, line 81 def _path @path end
method_missing(m, *args, &block)
click to toggle source
regular methods
# File lib/obfs/store.rb, line 11 def method_missing(m, *args, &block) # normalize method_name = m.to_s dataA = args[0] dataB = args[1] # prevent traversing out of dir raise "traversal through . and .. not allowed" if ['.', '..'].include? method_name # setter call if method_name.end_with?('=') # clean up name method_name = method_name.gsub('=','') # reassign if square bracket notation if method_name == "[]" method_name = dataA data = dataB else # make sure we load the proper method_name and data method_name = m.to_s.gsub('=','') data = args[0] end # prevent traversing out of dir raise "traversal through . and .. not allowed" if ['.', '..'].include? method_name # write data if data == nil FileUtils.rm_rf (File.join @path, method_name) else FileUtils.rm_rf (File.join @path, method_name) if File.exist? (File.join @path, method_name) FileUtils.mkpath @path if !File.directory? @path write(@path, method_name, data) end # bracket notation elsif method_name == "[]" method_name = dataA.to_s.gsub(/\["/,'').gsub(/"\]/,'') # prevent traversing out of dir raise "traversal through . and .. not allowed" if ['.', '..'].include? method_name if (!File.directory? File.join(@path, method_name)) && (File.exist? File.join(@path, method_name)) read(@path, method_name) else OBFS::Store.new({ path: File.join(@path, method_name.to_s) }) end # recurse or read else # prevent traversing out of dir raise "traversal through . and .. not allowed" if ['.', '..'].include? method_name if (!File.directory? File.join(@path, method_name)) && (File.exist? File.join(@path, method_name)) read(@path, method_name) else OBFS::Store.new({ path: File.join(@path, method_name.to_s) }) end end end
Private Instance Methods
read(path, filename)
click to toggle source
# File lib/obfs/store.rb, line 125 def read(path, filename) curr_path = File.join path, filename JSON.parse(File.open(curr_path).read) rescue File.open(curr_path).read end
write(path, filename, data)
click to toggle source
filesystem R/W
# File lib/obfs/store.rb, line 120 def write(path, filename, data) curr_path = File.join path, filename File.write(curr_path, JSON.unparse(data)) end