Skip to content

Instantly share code, notes, and snippets.

@ZeroPivot
Created September 27, 2014 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZeroPivot/9cbc15a9dc11802282c6 to your computer and use it in GitHub Desktop.
Save ZeroPivot/9cbc15a9dc11802282c6 to your computer and use it in GitHub Desktop.
# [Written By: Vivex - eigenfruit@gmail.com] #
require 'fileutils'
#TODO:
=begin
-24-Sep-14 : most of the day
* Implemented File saving methods: [aoh_object].save!(file_location: "test", aoh: nil)
* As a side note: you may have to modify the get_id instance method to return a certain string (or false)
if there is no id at that particular location. Also, will rem_id return an error if it can't
find any data at that location.
-20-Sep-14 : 10:40am-
* Create an Exception library (inheriting StandardError)
* Find a way to implement containers, where every function below can perform operations based on what type or
What's inside the container <-- .map :p
-20-Sep-14 : 5:37pm-
* Make self.load and self.save more secure by using something like Base64.encode / Base64.decode
=end
class AOH #Array-of-hashes container
def initialize(*args)
@collection=[]
@path_to_root=nil #we give a default path for some instance object of AOH, to make specifying where a file is to be saved with a bit more ease
args.map{|a_hash| add(a_hash) }
end
def add(hash)
if hash.is_a? Hash #this is where you could check to see what kind of classN it is then react accordingly
@collection.push(hash)
else
raise "#{hash.inspect} is NOT a Hash..."
end
end
def default_tags
end
def get_id(id=nil) #or nil to get all ---- THIS IS WHERE you may have to make a modification on the return of @collection[id]
if @collection.empty?
return nil
elsif id==nil
return map {|i| i}
else
return @collection[id]
end
end
def set_id(id, &block)
raise "Improper id: #{id}"
raise "No block given in set_by()" if !block_given?
hash=get_id(id)
if hash == nil #there is nothing here
raise "#{id} not in #{hash.inspect}"
else
block.call( get_id(id) )
end
end
def rem_by_id(id)
raise "id is not an integer" if id.is_a? Integer
@collection.delete_at(id)
end
def each_with_index(&block)
raise "block not given in each_with_index(#&block)" if !block_given?
#@collection.each_with_index.map {|a_hash, index| yield [a_hash, index]} #now figure out a nice way to add to the arguments...
@collection.each_with_index.map {|a_hash, index| yield [a_hash, index]} #now figure out a nice way to add to the arguments...
end
def map(&block)
raise "block not given in map(&block)" if !block_given?
#@collection.each_with_index.map {|a_hash, index| yield [a_hash, index]} #now figure out a nice way to add to the arguments...
@collection.map {|a_hash| block.call(a_hash) } #now figure out a nice way to add to the arguments...
end
def each
@collection.map {|hash| hash}
end
def print_all
loc=0
each do |hash|
hash.each_pair do |id, data|
puts "Loc[#{loc}]: #{id.inspect} => #{data.inspect}" #you could put a yield here one of these days
end
loc += i1
puts "\n\n"
end
end
def every_container
end
def id_range
end
def self.save(aoh: nil, file_location: "test")
File.open(file_location, 'w') do |file_handle|
raise "AOH undefined" unless aoh.class == AOH
Marshal.dump(aoh, file_handle)
end
aoh
end
def self.touch!(file_location: "test")
if !file_location
FileUtils.touch(file_location)
load!(file_location: file_location)
end
end
def save!(file_location: "test", aoh: nil)
self.class.save(file_location: file_location, aoh: self)
end
def self.save!(aoh: AOH.new(), file_location: nil) #save no matter what
raise "file_location not specified or is nil (file_location: #{file_location.inspect})" if !file_location
File.open( file_location , 'w' ) do |file_handle|
Marshal.dump(aoh, file_handle)
end
end
def self.load!(aoh: AOH.new(), file_location: nil, s_self: nil) #factory method; returns an AOH object with those specific array-of-hashes (will create a new file if file does not exist; use iff certain that you know what file you're loading)
raise "file_location not specified or is nil (file_location: #{file_location})" if !file_location
loaded=false
if !File.exist? file_location
touch!(file_location: file_location)
save!(aoh: aoh, file_location: file_location)
end
File.open( file_location) do |file_handle|
loaded=Marshal.load( file_handle )
end
loaded
end
def self.load(file_location: "test") #factory method; returns an AOH object with those specific array-of-hashes
loaded=false
File.open( file_location ) do |file_handle|
loaded=Marshal.load( file_handle )
end
loaded
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment