require 'nera_database' require 'nera_simulator' require 'observer' module NERA # This class access the simulator table. class SimulatorRecords # keys of the simulator table ATTRIBUTES = [ [:id, Integer], [:name, String], [:created_at, DateTime], [:updated_at, DateTime] ] # NERA::Database object @db # argument is the path to database file def initialize( db_file) @db = NERA::Database.new( db_file) end # argument is the path to simulators.yml def set_yaml_file( yaml_path) @db.set_yaml_file( yaml_path) end # if file already exists, returns false def self.create_table( db_file) NERA::Database.create_table( db_file) end # return keys of the simulator table def keys keys = ATTRIBUTES.map do |x| x[0] end end # sim_class must be a subclass of NERA::Simulator def add( sim_class) unless sim_class.superclass == NERA::Simulator raise ArgumentError, "must be a subclass of NERA::Simulator" end name = sim_class.to_s found = @db.find_all do |rec| rec[:name] == name end if found $stderr.puts "Same simulator already exists." return nil end d = DateTime.now h = { :name => name, :created_at => d, :updated_at => d} id = @db.add( h) return id end def get_class( id) raise ArgumentError unless id.is_a?(Integer) rec = @db.find_by_id(id) return nil unless rec klass = eval(rec[:name]) return klass end def list l = @db.find_all do |r| true end return l.to_a end def touch( id) raise ArgumentError unless id.is_a?( Integer) rec = @db.find_by_id( id) return nil unless rec rec[:updated_at] = DateTime.now @db.update( rec) end alias :update :touch def destroy( id) raise ArgumentError unless id.is_a?(Integer) @db.destroy(id) end def transaction @db.transaction { yield } end end end