Class: Neo4j::Railtie

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config_dataObject



62
63
64
65
66
67
68
# File 'lib/neo4j/railtie.rb', line 62

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

.default_session_pathObject



81
82
83
84
85
# File 'lib/neo4j/railtie.rb', line 81

def default_session_path
  ENV['NEO4J_URL'] || ENV['NEO4J_PATH'] ||
    config_data[:url] || config_data[:path] ||
    'http://localhost:7474'
end

.default_session_typeObject



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

def default_session_type
  type = ENV['NEO4J_TYPE'] || config_data[:type] || :server_db
  type.to_sym
end

.java_platform?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/neo4j/railtie.rb', line 43

def java_platform?
  RUBY_PLATFORM =~ /java/
end

.open_neo4j_session(options, wait_for_connection = false) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/neo4j/railtie.rb', line 96

def open_neo4j_session(options, wait_for_connection = false)
  type, name, default, path = options.values_at(:type, :name, :default, :path)

  if !java_platform? && type == :embedded_db
    fail "Tried to start embedded Neo4j db without using JRuby (got #{RUBY_PLATFORM}), please run `rvm jruby`"
  end

  session = wait_for_value(wait_for_connection) do
    if options.key?(:name)
      Neo4j::Session.open_named(type, name, default, path)
    else
      Neo4j::Session.open(type, path, options[:options])
    end
  end

  start_embedded_session(session) if type == :embedded_db
end

.setup_config_defaults!(cfg) ⇒ Object



55
56
57
58
59
60
# File 'lib/neo4j/railtie.rb', line 55

def setup_config_defaults!(cfg)
  cfg.session_type ||= default_session_type
  cfg.session_path ||= default_session_path
  cfg.session_options ||= {}
  cfg.sessions ||= []
end

.setup_default_session(cfg) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/neo4j/railtie.rb', line 47

def setup_default_session(cfg)
  setup_config_defaults!(cfg)

  return if !cfg.sessions.empty?

  cfg.sessions << {type: cfg.session_type, path: cfg.session_path, options: cfg.session_options.merge(default: true)}
end

.start_embedded_session(session) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/neo4j/railtie.rb', line 87

def start_embedded_session(session)
  # See https://github.com/jruby/jruby/wiki/UnlimitedStrengthCrypto
  security_class = java.lang.Class.for_name('javax.crypto.JceSecurity')
  restricted_field = security_class.get_declared_field('isRestricted')
  restricted_field.accessible = true
  restricted_field.set nil, false
  session.start
end

.yaml_pathObject



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

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

Instance Method Details

#register_neo4j_cypher_loggingObject



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/neo4j/railtie.rb', line 134

def register_neo4j_cypher_logging
  return if @neo4j_cypher_logging_registered

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

  Neo4j::Server::CypherSession.log_with do |message|
    (Neo4j::Config[:logger] || Rails.logger).debug message
  end

  @neo4j_cypher_logging_registered = true
end

#wait_for_value(wait) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/neo4j/railtie.rb', line 115

def wait_for_value(wait)
  session = nil
  Timeout.timeout(60) do
    until session
      begin
        if session = yield
          puts
          return session
        end
      rescue Faraday::ConnectionFailed => e
        raise e if !wait

        putc '.'
        sleep(1)
      end
    end
  end
end