Class: Neo4j::Railtie

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

Instance Method Summary collapse

Instance Method Details

#default_session_path_or_urlObject



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

def default_session_path_or_url
  ENV['NEO4J_URL'] || ENV['NEO4J_PATH'] || 'http://localhost:7474'
end

#default_session_typeObject



97
98
99
100
101
102
103
104
105
# File 'lib/neo4j/railtie.rb', line 97

def default_session_type
  if ENV['NEO4J_URL']
    scheme = URI(ENV['NEO4J_URL']).scheme
    fail "Invalid scheme for NEO4J_URL: #{scheme}" if !%w(http https bolt).include?(scheme)
    scheme == 'https' ? 'http' : scheme
  else
    ENV['NEO4J_TYPE'] || :http
  end.to_sym
end

#empty_configObject



10
11
12
# File 'lib/neo4j/railtie.rb', line 10

def empty_config
  ActiveSupport::OrderedOptions.new.tap { |cfg| cfg.session = ActiveSupport::OrderedOptions.new }
end

#final_session_config!(neo4j_config) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/neo4j/railtie.rb', line 74

def final_session_config!(neo4j_config)
  support_deprecated_session_configs!(neo4j_config)

  (neo4j_config[:session].empty? ? yaml_config_data : neo4j_config[:session]).dup.tap do |result|
    result[:type] ||= URI(result[:url]).scheme if result[:url]
  end
end

#register_neo4j_cypher_logging(session_type, options) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/neo4j/railtie.rb', line 126

def register_neo4j_cypher_logging(session_type, options)
  return if @neo4j_cypher_logging_registered

  Neo4j::Core::Query.pretty_cypher = Neo4j::Config[:pretty_logged_cypher_queries]

  logger_proc = ->(message) do
    (Neo4j::Config[:logger] ||= Rails.logger).debug message
  end
  Neo4j::Core::CypherSession::Adaptors::Base.subscribe_to_query(&logger_proc)
  subscribe_to_session_type_logging!(session_type, options, logger_proc)

  @neo4j_cypher_logging_registered = true
end

#setup!(neo4j_config = empty_config) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/neo4j/railtie.rb', line 61

def setup!(neo4j_config = empty_config)
  wait_for_connection = neo4j_config.wait_for_connection
  type, url, path, options = final_session_config!(neo4j_config).values_at(:type, :url, :path, :options)
  type ||= default_session_type
  options ||= {}
  register_neo4j_cypher_logging(type, options)

  Neo4j::SessionManager.open_neo4j_session(type,
                                           url || path || default_session_path_or_url,
                                           wait_for_connection,
                                           options)
end

#subscribe_to_session_type_logging!(session_type, options, logger_proc) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/neo4j/railtie.rb', line 140

def subscribe_to_session_type_logging!(session_type, options, logger_proc)
  SessionManager
    .adaptor_class(session_type, options)
    .send(
      [:embedded, :embedded_db].include?(session_type.to_sym) ? :subscribe_to_transaction : :subscribe_to_request,
      &logger_proc
    )
end

#support_deprecated_session_configs!(neo4j_config) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/neo4j/railtie.rb', line 82

def support_deprecated_session_configs!(neo4j_config)
  if neo4j_config.sessions.present?
    ActiveSupport::Deprecation.warn('neo4j.config.sessions is deprecated, please use neo4j.config.session (not an array)')
    neo4j_config[:session] = neo4j_config.sessions[0] if neo4j_config[:session].empty?
  end

  %w(type path url options).each do |key|
    value = neo4j_config.send("session_#{key}")
    if value.present?
      ActiveSupport::Deprecation.warn("neo4j.config.session_#{key} is deprecated, please use neo4j.config.session.#{key}")
      neo4j_config[:session][key] = value
    end
  end
end

#yaml_config_dataObject



111
112
113
114
115
116
117
# File 'lib/neo4j/railtie.rb', line 111

def yaml_config_data
  @yaml_config_data ||= if yaml_path
                          HashWithIndifferentAccess.new(YAML.load(ERB.new(yaml_path.read).result)[Rails.env])
                        else
                          {}
                        end
end

#yaml_pathObject



119
120
121
122
123
124
# File 'lib/neo4j/railtie.rb', line 119

def yaml_path
  return unless defined?(Rails)
  @yaml_path ||= %w(config/neo4j.yml config/neo4j.yaml).map do |path|
    Rails.root.join(path)
  end.detect(&:exist?)
end