Skip to content

Latest commit

 

History

History
59 lines (40 loc) · 1.63 KB

initializer-structure.org

File metadata and controls

59 lines (40 loc) · 1.63 KB

Place your initialization code in a file named after the environment

There are often multiple environment configurations for multiple databases in the configuration file, so you can use the method here to put the initialization code of different environments in different files to avoid conflicts.

Let’s say you have 4 database environments configured in the configuration file:

  • apollo.dev
  • apollo.prod
  • space.dev
  • space.prod

Then you can create the following file in the ~/.arql.d/ directory:

  • apollo.rb
  • space.rb

Place the initialization code for the Apollo project apollo.rb in the file; Place the initialization code for the space project space.rb in the file.

Then write the following code in the ~/.arql.d/init.eb file:

Dir.glob(File.dirname(__FILE__) + '/*.rb').each do |f|
  Arql::App.instance.definitions.each do |env, definition|
    if env.starts_with?(File.basename(f, '.rb'))
      load(f, definition.namespace_module)
    end
  end
end

In this way, arql -e apollo.prod when or is executed, the initialization apollo.rb code in the file is loaded, and arql -e space.prod when or is executed arql -e apollo.dev arql -e space.dev , the initialization code in the file is loaded space.rb .

apollo.rb The code in the space.rb or file will be executed under the corresponding Namespace Module:

class Astronaut
  has_many :missions
end

Equivalent to:

module Apollo
  class Astronaut
    has_many :missions
  end
end