Sha256: 1c1fee531604c6257e20c836277f6fb6b4db125207c29db5e21e94c56c2bfe48

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

require 'nera_db_folders'
require 'nera_simulator_records'
require 'nera_parameter_records'

module NERA
  
  class SimulatorLayerController

    # instance of NERA::DbFolders
    @db_folders
    # instance of NERA::SimulatorRecords
    @sim_records
    
    def initialize( path_db_folds)
      raise ArgumentError unless path_db_folds.is_a?(String)
      
      @db_folders = NERA::DbFolders.new( path_db_folds)
      table_path = @db_folders.path_to_simulators_table
      @sim_records = NERA::SimulatorRecords.new(table_path)

      NERA::Simulator.inherited_simulators.each do |sim|
        next if list.find do |rec| rec[:name] == sim.to_s end
        found = sim::Parameters.find do |p|
            p[0] == :in_trashbox? or p[0] == :created_at or p[0] == :updated_at
        end
        next if found
        register_a_simulator( sim)
      end
    end

    def list
      @sim_records.list
    end

    def path_to_sim_layer
      @db_folders.path_to_simulator_layer
    end
    
    private
    def register_a_simulator( sim_klass)
      a = nil
      @sim_records.transaction {
        a = @sim_records.add( sim_klass)
        return nil unless a
        b = @db_folders.create_new_simulator_folder( sim_klass)
        raise "The folder #{sim_klass.to_s}/ already exists." unless b
        path = @db_folders.path_to_parameters_table( sim_klass)
        c = NERA::ParameterRecords.create_table( path, sim_klass)
        raise "The file #{path} already exists." unless c
      }
      dump_in_yaml
      return a
    end

    private
    def dump_in_yaml
      ymlpath = @db_folders.path_to_simulators_yaml
      File.open( ymlpath, 'w') do |io|
        YAML::dump( @sim_records.list, io)
        io.flush
      end
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nera-0.1.1 lib/nera/nera_simulator_layer_controller.rb
nera-0.0.1 lib/nera/nera_simulator_layer_controller.rb
nera-0.0.2 lib/nera/nera_simulator_layer_controller.rb
nera-0.0.3 lib/nera/nera_simulator_layer_controller.rb
nera-0.0.4 lib/nera/nera_simulator_layer_controller.rb
nera-0.1.0 lib/nera/nera_simulator_layer_controller.rb