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
MissingGatewayConfigError =
Class.new(StandardError)

Instance Method Summary collapse

Instance 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)


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

def active_record?
  defined?(::ActiveRecord)
end

#auto_registration_pathsObject



129
130
131
# File 'lib/rom/rails/railtie.rb', line 129

def auto_registration_paths
  config.rom.auto_registration_paths + [root]
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.gateways[:default] = [:yaml, 'yaml:///data']
  config.auto_registration_paths += [MyEngine.root]
end


62
63
64
65
66
67
68
69
70
# File 'lib/rom/rails/railtie.rb', line 62

def configure(&block)
  config.rom = Configuration.new unless config.respond_to?(:rom)

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

#configure_console_loggerObject

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.



144
145
146
147
148
149
# File 'lib/rom/rails/railtie.rb', line 144

def configure_console_logger
  return if active_record? || std_err_out_logger?

  console = ActiveSupport::Logger.new(STDERR)
  ::Rails.logger.extend ActiveSupport::Logger.broadcast console
end

#containerObject



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

def container
  ROM.env
end

#create_configurationObject



72
73
74
# File 'lib/rom/rails/railtie.rb', line 72

def create_configuration
  ROM::Configuration.new(gateways)
end

#create_containerObject

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.



77
78
79
80
81
82
83
84
85
# File 'lib/rom/rails/railtie.rb', line 77

def create_container
  configuration = create_configuration

  auto_registration_paths.each do |root_path|
    configuration.auto_registration(::Rails.root.join(root_path), namespace: false)
  end

  ROM.container(configuration)
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.



116
117
118
# File 'lib/rom/rails/railtie.rb', line 116

def disconnect
  container.disconnect unless container.nil?
end

#gatewaysObject

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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rom/rails/railtie.rb', line 88

def gateways
  if active_record?
    load_active_record_config.each do |name, spec|
      config.rom.gateways[name] ||= [:sql, spec[:uri], spec[:options]]
    end
  end

  if config.rom.gateways.empty?
    ::Rails.logger.warn "It seems that you have not configured any gateways"

    config.rom.gateways[:default] = [ :memory, "memory://test" ]
  end

  config.rom.gateways
end

#load_active_record_configObject

Attempt to infer all configured gateways from activerecord



105
106
107
# File 'lib/rom/rails/railtie.rb', line 105

def load_active_record_config
  ROM::Rails::ActiveRecord::Configuration.new.call
end

#load_initializerObject



109
110
111
112
113
# File 'lib/rom/rails/railtie.rb', line 109

def load_initializer
  load "#{root}/config/initializers/rom.rb"
rescue LoadError
  # do nothing
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.



121
122
123
# File 'lib/rom/rails/railtie.rb', line 121

def root
  ::Rails.root
end

#std_err_out_logger?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)


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

def std_err_out_logger?
  ActiveSupport::Logger.logger_outputs_to?(::Rails.logger, STDERR, STDOUT)
end