Class: NERA::SimulatorRecords

Inherits:
Object
  • Object
show all
Defined in:
lib/nera/nera_simulator_records.rb

Overview

This class access the simulator table.

Constant Summary collapse

ATTRIBUTES =

keys of the simulator table

[ [:id, Integer], [:name, String], [:created_at, DateTime], [:updated_at, DateTime] ]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db_file) ⇒ SimulatorRecords

argument is the path to database file



17
18
19
# File 'lib/nera/nera_simulator_records.rb', line 17

def initialize( db_file)
  @db = NERA::Database.new( db_file)
end

Class Method Details

.create_table(db_file) ⇒ Object

if file already exists, returns false



27
28
29
# File 'lib/nera/nera_simulator_records.rb', line 27

def self.create_table( db_file)
  NERA::Database.create_table( db_file)
end

Instance Method Details

#add(sim_class) ⇒ Object

sim_class must be a subclass of NERA::Simulator



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nera/nera_simulator_records.rb', line 39

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

#destroy(id) ⇒ Object

Raises:

  • (ArgumentError)


80
81
82
83
# File 'lib/nera/nera_simulator_records.rb', line 80

def destroy( id)
  raise ArgumentError unless id.is_a?(Integer)
  @db.destroy(id)
end

#get_class(id) ⇒ Object

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
# File 'lib/nera/nera_simulator_records.rb', line 57

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

#keysObject

return keys of the simulator table



32
33
34
35
36
# File 'lib/nera/nera_simulator_records.rb', line 32

def keys
  keys = ATTRIBUTES.map do |x|
    x[0]
  end
end

#listObject



65
66
67
68
# File 'lib/nera/nera_simulator_records.rb', line 65

def list
  l = @db.find_all do |r| true end
  return l.to_a
end

#set_yaml_file(yaml_path) ⇒ Object

argument is the path to simulators.yml



22
23
24
# File 'lib/nera/nera_simulator_records.rb', line 22

def set_yaml_file( yaml_path)
  @db.set_yaml_file( yaml_path)
end

#touch(id) ⇒ Object Also known as: update

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
# File 'lib/nera/nera_simulator_records.rb', line 70

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

#transactionObject



85
86
87
88
89
# File 'lib/nera/nera_simulator_records.rb', line 85

def transaction
  @db.transaction {
    yield
  }
end