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)


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

def active_record?
  defined?(::ActiveRecord)
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']
end


55
56
57
58
59
60
61
62
63
# File 'lib/rom/rails/railtie.rb', line 55

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

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

#containerObject



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

def container
  ROM.env
end

#create_configurationObject



65
66
67
# File 'lib/rom/rails/railtie.rb', line 65

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.



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

def create_container
  configuration = create_configuration
  configuration.auto_registration(root.join("app"), namespace: false)
  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.



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

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.



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

def gateways
  config.rom.gateways[:default] ||= infer_default_gateway if active_record?

  raise(
    MissingGatewayConfigError,
    "seems like you didn't configure any gateways"
  ) unless config.rom.gateways.any?

  config.rom.gateways
end

#infer_default_gatewayObject

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 gateway configured, try to infer it from other sources, e.g. ActiveRecord.



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

def infer_default_gateway
  spec = ROM::Rails::ActiveRecord::Configuration.call
  [:sql, spec[:uri], spec[:options]]
end

#load_initializerObject



97
98
99
# File 'lib/rom/rails/railtie.rb', line 97

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



107
108
109
# File 'lib/rom/rails/railtie.rb', line 107

def root
  ::Rails.root
end