Class: NoBrainer::QueryRunner::RunOptions

Inherits:
Middleware
  • Object
show all
Defined in:
lib/no_brainer/query_runner/run_options.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Middleware

#initialize

Constructor Details

This class inherits a constructor from NoBrainer::QueryRunner::Middleware

Class Method Details

.current_run_optionsObject



12
13
14
15
16
17
# File 'lib/no_brainer/query_runner/run_options.rb', line 12

def self.current_run_options
  options = NoBrainer::Config.run_options
  options = options.merge(:durability => NoBrainer::Config.durability) if NoBrainer::Config.durability
  options = options.merge(Thread.current[:nobrainer_run_with]) if Thread.current[:nobrainer_run_with]
  options
end

.run_with(options = {}, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/no_brainer/query_runner/run_options.rb', line 19

def self.run_with(options={}, &block)
  options = options.symbolize_keys

  if options[:database]
    STDERR.puts "[NoBrainer] `run_with(database: ...)' is deprecated, please use `run_with(db: ...)' instead"
    options[:db] = options.delete(:database)
  end

  old_options = Thread.current[:nobrainer_run_with]
  Thread.current[:nobrainer_run_with] = (old_options || {}).merge(options)
  block.call
ensure
  Thread.current[:nobrainer_run_with] = old_options
end

.with(options = {}, &block) ⇒ Object



7
8
9
10
# File 'lib/no_brainer/query_runner/run_options.rb', line 7

def self.with(options={}, &block)
  STDERR.puts "[NoBrainer] `with(...)' is deprecated, please use `run_with(...)' instead"
  run_with(options, &block)
end

.with_database(db_name, &block) ⇒ Object



2
3
4
5
# File 'lib/no_brainer/query_runner/run_options.rb', line 2

def self.with_database(db_name, &block)
  STDERR.puts "[NoBrainer] `with_database()' is deprecated, please use `with(db: ...)' instead"
  with(:db => db_name, &block)
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/no_brainer/query_runner/run_options.rb', line 34

def call(env)
  options = self.class.current_run_options
  options = options.merge(env[:options].symbolize_keys)
  options = prune_default_run_options(options)

  env[:criteria] = options.delete(:criteria)

  if options[:profile] && env[:criteria].try(:raw?) == false
    STDERR.puts "[NoBrainer]"
    STDERR.puts "[NoBrainer]\e[1;31m Please use `.raw' in your criteria when profiling\e[0m"
    STDERR.puts "[NoBrainer]"
  end

  env[:options] = options
  @runner.call(env)
end

#prune_default_run_options(options) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/no_brainer/query_runner/run_options.rb', line 51

def prune_default_run_options(options)
  options = options.dup
  options.delete(:durability) if options[:durability].to_s == 'hard'

  options[:db] = options[:db].to_s if options[:db]
  options.delete(:db) if options[:db].blank? || options[:db] == NoBrainer.default_db

  options
end