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)


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

def active_record?
  defined?(::ActiveRecord)
end

#auto_registration_pathsObject



142
143
144
# File 'lib/rom/rails/railtie.rb', line 142

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


71
72
73
74
75
76
77
78
79
# File 'lib/rom/rails/railtie.rb', line 71

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.



157
158
159
160
161
162
# File 'lib/rom/rails/railtie.rb', line 157

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



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

def container
  ROM.env
end

#create_configurationObject



81
82
83
# File 'lib/rom/rails/railtie.rb', line 81

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.



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rom/rails/railtie.rb', line 86

def create_container
  configuration = create_configuration

  auto_registration_paths.each do |root_path|
    if root_path.is_a? Hash
      configuration.auto_registration(::Rails.root.join(root_path[:path]), namespace: root_path[:namespace])
    else
      configuration.auto_registration(::Rails.root.join(root_path), namespace: false)
    end
  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.



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

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.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rom/rails/railtie.rb', line 101

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



118
119
120
# File 'lib/rom/rails/railtie.rb', line 118

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

#load_initializerObject



122
123
124
125
126
# File 'lib/rom/rails/railtie.rb', line 122

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.



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

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)


152
153
154
# File 'lib/rom/rails/railtie.rb', line 152

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