Class: ROM::Rails::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/rom/rails/railtie.rb

Constant Summary collapse

COMPONENT_DIRS =
%w(relations mappers commands).freeze
MissingRepositoryConfigError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.active_record?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


45
46
47
# File 'lib/rom/rails/railtie.rb', line 45

def self.active_record?
  defined?(::ActiveRecord)
end

.finalizeObject



29
30
31
32
# File 'lib/rom/rails/railtie.rb', line 29

def self.finalize
  ROM.finalize
  self
end

.infer_default_repositoryObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

If there’s no default repository configured, try to infer it from other sources, e.g. ActiveRecord.



38
39
40
41
42
# File 'lib/rom/rails/railtie.rb', line 38

def self.infer_default_repository
  return unless active_record?
  spec = ROM::Rails::ActiveRecord::Configuration.call
  [:sql, spec[:uri], spec[:options]]
end

.setup_repositoriesObject



18
19
20
21
22
23
24
25
26
# File 'lib/rom/rails/railtie.rb', line 18

def self.setup_repositories
  raise(
    MissingRepositoryConfigError,
    "seems like you didn't configure any repositories"
  ) unless config.rom.repositories.any?

  ROM.setup(config.rom.repositories)
  self
end

Instance Method Details

#before_initializeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



50
51
52
# File 'lib/rom/rails/railtie.rb', line 50

def before_initialize
  config.rom = Configuration.new
end

#configure(&block) ⇒ Object

Behaves like ‘Railtie#configure` if the given block does not take any arguments. Otherwise yields the ROM configuration to the block.

Examples:

ROM::Rails::Railtie.configure do |config|
  config.repositories[:default] = [:yaml, 'yaml:///data']
end


92
93
94
95
96
97
98
# File 'lib/rom/rails/railtie.rb', line 92

def configure(&block)
  if block.arity == 1
    block.call(config.rom)
  else
    super
  end
end

#disconnectObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

TODO: Add ‘ROM.env.disconnect` to core.



103
104
105
# File 'lib/rom/rails/railtie.rb', line 103

def disconnect
  ROM.env.repositories.each_value(&:disconnect)
end

#load_allObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



125
126
127
128
129
# File 'lib/rom/rails/railtie.rb', line 125

def load_all
  COMPONENT_DIRS.each do |type|
    load_files(type)
  end
end

#load_files(type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



132
133
134
135
136
# File 'lib/rom/rails/railtie.rb', line 132

def load_files(type)
  Dir[root.join("app/#{type}/**/*.rb").to_s].each do |path|
    require_dependency(path)
  end
end

#rootObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



139
140
141
# File 'lib/rom/rails/railtie.rb', line 139

def root
  ::Rails.root
end

#setupObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rom/rails/railtie.rb', line 108

def setup
  if ROM.env
    ROM.setup(ROM.env.repositories)
  else
    repositories = config.rom.repositories

    if self.class.active_record?
      repositories[:default] ||= self.class.infer_default_repository
    end

    self.class.setup_repositories
  end
  load_all
  self.class.finalize
end