nobrainer 0.22.0 → 0.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. checksums.yaml +4 -4
  2. data/lib/no_brainer/autoload.rb +1 -1
  3. data/lib/no_brainer/config.rb +54 -14
  4. data/lib/no_brainer/connection.rb +24 -21
  5. data/lib/no_brainer/connection_manager.rb +29 -9
  6. data/lib/no_brainer/criteria/cache.rb +8 -0
  7. data/lib/no_brainer/criteria/changes.rb +16 -0
  8. data/lib/no_brainer/criteria/core.rb +4 -5
  9. data/lib/no_brainer/criteria/eager_load.rb +2 -2
  10. data/lib/no_brainer/criteria/enumerable.rb +3 -1
  11. data/lib/no_brainer/criteria/find.rb +6 -9
  12. data/lib/no_brainer/criteria/first.rb +3 -3
  13. data/lib/no_brainer/criteria/first_or_create.rb +114 -0
  14. data/lib/no_brainer/criteria/join.rb +62 -0
  15. data/lib/no_brainer/criteria/order_by.rb +19 -16
  16. data/lib/no_brainer/criteria/run.rb +26 -0
  17. data/lib/no_brainer/criteria/scope.rb +8 -8
  18. data/lib/no_brainer/criteria/where.rb +130 -107
  19. data/lib/no_brainer/criteria.rb +4 -4
  20. data/lib/no_brainer/document/association/belongs_to.rb +44 -17
  21. data/lib/no_brainer/document/association/core.rb +11 -0
  22. data/lib/no_brainer/document/association/eager_loader.rb +26 -26
  23. data/lib/no_brainer/document/association/has_many.rb +7 -9
  24. data/lib/no_brainer/document/association/has_many_through.rb +1 -2
  25. data/lib/no_brainer/document/association/has_one.rb +5 -1
  26. data/lib/no_brainer/document/association.rb +5 -5
  27. data/lib/no_brainer/document/atomic_ops.rb +40 -7
  28. data/lib/no_brainer/document/attributes.rb +24 -15
  29. data/lib/no_brainer/document/callbacks.rb +1 -1
  30. data/lib/no_brainer/document/core.rb +18 -18
  31. data/lib/no_brainer/document/criteria.rb +9 -5
  32. data/lib/no_brainer/document/dirty.rb +15 -12
  33. data/lib/no_brainer/document/dynamic_attributes.rb +6 -0
  34. data/lib/no_brainer/document/index/index.rb +5 -9
  35. data/lib/no_brainer/document/index/meta_store.rb +1 -10
  36. data/lib/no_brainer/document/index/synchronizer.rb +16 -20
  37. data/lib/no_brainer/document/index.rb +3 -3
  38. data/lib/no_brainer/document/lazy_fetch.rb +3 -3
  39. data/lib/no_brainer/document/missing_attributes.rb +7 -2
  40. data/lib/no_brainer/document/persistance.rb +14 -30
  41. data/lib/no_brainer/document/polymorphic.rb +8 -4
  42. data/lib/no_brainer/document/primary_key/generator.rb +6 -1
  43. data/lib/no_brainer/document/primary_key.rb +19 -4
  44. data/lib/no_brainer/document/serialization.rb +0 -2
  45. data/lib/no_brainer/document/table_config/synchronizer.rb +21 -0
  46. data/lib/no_brainer/document/table_config.rb +118 -0
  47. data/lib/no_brainer/document/timestamps.rb +8 -0
  48. data/lib/no_brainer/document/validation/core.rb +63 -0
  49. data/lib/no_brainer/document/validation/uniqueness.rb +30 -36
  50. data/lib/no_brainer/document/validation.rb +1 -58
  51. data/lib/no_brainer/document.rb +2 -2
  52. data/lib/no_brainer/error.rb +1 -0
  53. data/lib/no_brainer/geo/base.rb +0 -1
  54. data/lib/no_brainer/locale/en.yml +1 -0
  55. data/lib/no_brainer/lock.rb +12 -8
  56. data/lib/no_brainer/profiler/controller_runtime.rb +76 -0
  57. data/lib/no_brainer/{query_runner → profiler}/logger.rb +11 -27
  58. data/lib/no_brainer/profiler.rb +11 -0
  59. data/lib/no_brainer/query_runner/database_on_demand.rb +8 -8
  60. data/lib/no_brainer/query_runner/driver.rb +3 -1
  61. data/lib/no_brainer/query_runner/missing_index.rb +3 -3
  62. data/lib/no_brainer/query_runner/profiler.rb +43 -0
  63. data/lib/no_brainer/query_runner/reconnect.rb +38 -23
  64. data/lib/no_brainer/query_runner/run_options.rb +35 -15
  65. data/lib/no_brainer/query_runner/table_on_demand.rb +18 -11
  66. data/lib/no_brainer/query_runner.rb +3 -3
  67. data/lib/no_brainer/railtie/database.rake +14 -4
  68. data/lib/no_brainer/railtie.rb +5 -12
  69. data/lib/no_brainer/rql.rb +11 -8
  70. data/lib/no_brainer/symbol_decoration.rb +11 -0
  71. data/lib/no_brainer/system/cluster_config.rb +5 -0
  72. data/lib/no_brainer/system/db_config.rb +5 -0
  73. data/lib/no_brainer/system/document.rb +24 -0
  74. data/lib/no_brainer/system/issue.rb +10 -0
  75. data/lib/no_brainer/system/job.rb +10 -0
  76. data/lib/no_brainer/system/log.rb +11 -0
  77. data/lib/no_brainer/system/server_config.rb +7 -0
  78. data/lib/no_brainer/system/server_status.rb +9 -0
  79. data/lib/no_brainer/system/stat.rb +11 -0
  80. data/lib/no_brainer/system/table_config.rb +10 -0
  81. data/lib/no_brainer/system/table_status.rb +8 -0
  82. data/lib/no_brainer/system.rb +17 -0
  83. data/lib/nobrainer.rb +16 -11
  84. data/lib/rails/generators/nobrainer/install_generator.rb +48 -0
  85. data/lib/rails/generators/nobrainer/{model/model_generator.rb → model_generator.rb} +7 -3
  86. data/lib/rails/generators/nobrainer/namespace_fix.rb +15 -0
  87. data/lib/rails/generators/{nobrainer/model/templates/model.rb.tt → templates/model.rb} +1 -1
  88. data/lib/rails/generators/templates/nobrainer.rb +101 -0
  89. metadata +34 -10
  90. data/lib/no_brainer/document/store_in.rb +0 -35
  91. data/lib/rails/generators/nobrainer.rb +0 -18
@@ -1,35 +1,55 @@
1
1
  class NoBrainer::QueryRunner::RunOptions < NoBrainer::QueryRunner::Middleware
2
- # XXX NoBrainer::Database#drop() uses Thread.current[:nobrainer_options]
3
-
4
2
  def self.with_database(db_name, &block)
3
+ STDERR.puts "[NoBrainer] `with_database()' is deprecated, please use `with(db: ...)' instead"
5
4
  with(:db => db_name, &block)
6
5
  end
7
6
 
8
7
  def self.with(options={}, &block)
9
- old_options = Thread.current[:nobrainer_options]
10
- Thread.current[:nobrainer_options] = (old_options || {}).merge(options.symbolize_keys)
11
- block.call if block
8
+ STDERR.puts "[NoBrainer] `with(...)' is deprecated, please use `run_with(...)' instead"
9
+ run_with(options, &block)
10
+ end
11
+
12
+ def self.current_run_options
13
+ Thread.current[:nobrainer_run_with] || {}
14
+ end
15
+
16
+ def self.run_with(options={}, &block)
17
+ options = options.symbolize_keys
18
+
19
+ if options[:database]
20
+ STDERR.puts "[NoBrainer] `run_with(database: ...)' is deprecated, please use `run_with(db: ...)' instead"
21
+ options[:db] = options.delete(:database)
22
+ end
23
+
24
+ old_options = Thread.current[:nobrainer_run_with]
25
+ Thread.current[:nobrainer_run_with] = (old_options || {}).merge(options)
26
+ block.call
12
27
  ensure
13
- Thread.current[:nobrainer_options] = old_options
28
+ Thread.current[:nobrainer_run_with] = old_options
14
29
  end
15
30
 
16
31
  def call(env)
17
- env[:options].symbolize_keys!
18
- if Thread.current[:nobrainer_options]
19
- env[:options].reverse_merge!(Thread.current[:nobrainer_options])
20
- end
32
+ options = env[:options].symbolize_keys
33
+ options = self.class.current_run_options.merge(options)
21
34
 
22
35
  if NoBrainer::Config.durability.to_s != 'hard'
23
- env[:options].reverse_merge!(:durability => NoBrainer::Config.durability)
36
+ options[:durability] ||= NoBrainer::Config.durability
24
37
  end
25
38
 
26
- if env[:options][:db] && !env[:options][:db].is_a?(RethinkDB::RQL)
27
- env[:db_name] = env[:options][:db].to_s
28
- env[:options][:db] = RethinkDB::RQL.new.db(env[:db_name])
39
+ options[:db] = options[:db].to_s if options[:db]
40
+ if options[:db].blank? || options[:db] == NoBrainer.default_db
41
+ options.delete(:db)
29
42
  end
30
43
 
31
- env[:criteria] = env[:options].delete(:criteria)
44
+ env[:criteria] = options.delete(:criteria)
45
+
46
+ if options[:profile] && env[:criteria].try(:raw?) == false
47
+ STDERR.puts "[NoBrainer]"
48
+ STDERR.puts "[NoBrainer]\e[1;31m Please use `.raw' in your criteria when profiling\e[0m"
49
+ STDERR.puts "[NoBrainer]"
50
+ end
32
51
 
52
+ env[:options] = options
33
53
  @runner.call(env)
34
54
  end
35
55
  end
@@ -15,24 +15,31 @@ class NoBrainer::QueryRunner::TableOnDemand < NoBrainer::QueryRunner::Middleware
15
15
 
16
16
  private
17
17
 
18
- def auto_create_table(env, database_name, table_name)
19
- model ||= NoBrainer::Document::Core._all.select { |m| m.table_name == table_name }.first
20
- model ||= NoBrainer::Document::Core._all_nobrainer.select { |m| m.table_name == table_name }.first
21
-
18
+ def auto_create_table(env, db_name, table_name)
19
+ model = NoBrainer::Document.all(:types => [:user, :nobrainer])
20
+ .detect { |m| m.table_name == table_name }
22
21
  if model.nil?
23
- raise "Auto table creation is not working for `#{database_name}.#{table_name}` -- Can't find the corresponding model."
22
+ raise "Auto table creation is not working for `#{db_name}.#{table_name}` -- Can't find the corresponding model."
23
+ end
24
+
25
+ if env[:last_auto_create_table] == [db_name, table_name]
26
+ raise "Auto table creation is not working for `#{db_name}.#{table_name}`"
24
27
  end
28
+ env[:last_auto_create_table] = [db_name, table_name]
29
+
30
+ create_options = model.table_create_options
25
31
 
26
- if env[:last_auto_create_table] == [database_name, table_name]
27
- raise "Auto table creation is not working for `#{database_name}.#{table_name}`"
32
+ NoBrainer.run(:db => db_name) do |r|
33
+ r.table_create(table_name, create_options.reject { |k,_| k.in? [:name, :write_acks] })
28
34
  end
29
- env[:last_auto_create_table] = [database_name, table_name]
30
35
 
31
- NoBrainer.with_database(database_name) do
32
- NoBrainer.table_create(table_name, :primary_key => model.lookup_field_alias(model.pk_name))
36
+ if create_options[:write_acks] && create_options[:write_acks] != 'single'
37
+ NoBrainer.run(:db => db_name) do |r|
38
+ r.table(table_name).config().update(:write_acks => create_options[:write_acks])
39
+ end
33
40
  end
34
41
  rescue RuntimeError => e
35
42
  # We might have raced with another table create
36
- raise unless e.message =~ /Table `#{database_name}\.#{table_name}` already exists/
43
+ raise unless e.message =~ /Table `#{db_name}\.#{table_name}` already exists/
37
44
  end
38
45
  end
@@ -11,7 +11,7 @@ module NoBrainer::QueryRunner
11
11
  end
12
12
 
13
13
  autoload :Driver, :DatabaseOnDemand, :TableOnDemand, :WriteError,
14
- :Reconnect, :Selection, :RunOptions, :Logger, :MissingIndex,
14
+ :Reconnect, :Selection, :RunOptions, :Profiler, :MissingIndex,
15
15
  :ConnectionLock
16
16
 
17
17
  class << self
@@ -28,11 +28,11 @@ module NoBrainer::QueryRunner
28
28
  # thread-safe, since require() is ran with a mutex.
29
29
  self.stack = ::Middleware::Builder.new do
30
30
  use RunOptions
31
- use WriteError
32
31
  use MissingIndex
33
32
  use DatabaseOnDemand
34
33
  use TableOnDemand
35
- use Logger
34
+ use Profiler
35
+ use WriteError
36
36
  use ConnectionLock
37
37
  use Reconnect
38
38
  use Driver
@@ -9,8 +9,18 @@ namespace :nobrainer do
9
9
  NoBrainer.sync_indexes(:verbose => true)
10
10
  end
11
11
 
12
- task :sync_indexes_quiet => :environment do
13
- NoBrainer.sync_indexes
12
+ desc 'Synchronize table configuration'
13
+ task :sync_table_config => :environment do
14
+ NoBrainer.sync_table_config(:verbose => true)
15
+ end
16
+
17
+ desc 'Synchronize indexes and table configuration'
18
+ task :sync_schema => :environment do
19
+ NoBrainer.sync_schema(:verbose => true)
20
+ end
21
+
22
+ task :sync_schema_quiet => :environment do
23
+ NoBrainer.sync_schema
14
24
  end
15
25
 
16
26
  desc 'Load seed data from db/seeds.rb'
@@ -18,8 +28,8 @@ namespace :nobrainer do
18
28
  Rails.application.load_seed
19
29
  end
20
30
 
21
- desc 'Equivalent to :sync_indexes_quiet + :seed'
22
- task :setup => [:sync_indexes_quiet, :seed]
31
+ desc 'Equivalent to :sync_schema_quiet + :seed'
32
+ task :setup => [:sync_schema_quiet, :seed]
23
33
 
24
34
  desc 'Equivalent to :drop + :setup'
25
35
  task :reset => [:drop, :setup]
@@ -26,20 +26,13 @@ class NoBrainer::Railtie < Rails::Railtie
26
26
  config.after_initialize do
27
27
  NoBrainer::Config.configure unless NoBrainer::Config.configured?
28
28
 
29
- if defined?(ActiveRecord) && NoBrainer::Config.warn_on_active_record
30
- STDERR.puts "[NoBrainer] ActiveRecord is loaded which is probably not what you want."
31
- STDERR.puts "[NoBrainer] Follow the instructions on http://nobrainer.io/docs/configuration/#removing_activerecord"
32
- STDERR.puts "[NoBrainer] Configure NoBrainer with 'config.warn_on_active_record = false' to disable with warning."
33
- end
34
-
35
- if defined?(Mongoid)
36
- STDERR.puts "[NoBrainer] WARNING: Mongoid is loaded, and we conflict on the symbol decorations"
37
- STDERR.puts "[NoBrainer] They are used in queries such as Model.where(:tags.in => ['fun', 'stuff'])"
38
- STDERR.puts "[NoBrainer] This is a problem!"
39
- end
40
-
41
29
  ActionDispatch::Reloader.to_prepare do
42
30
  NoBrainer::Loader.cleanup
43
31
  end
44
32
  end
33
+
34
+ ActiveSupport.on_load(:action_controller) do
35
+ require 'no_brainer/profiler/controller_runtime'
36
+ include NoBrainer::Profiler::ControllerRuntime
37
+ end
45
38
  end
@@ -8,22 +8,25 @@ module NoBrainer::RQL
8
8
 
9
9
  def rql_proc_as_json(block)
10
10
  reset_lambda_var_counter
11
- RethinkDB::RQL.new.new_func(&block).as_json
11
+ RethinkDB::Shim.load_json(
12
+ RethinkDB::Shim.dump_json(
13
+ RethinkDB::RQL.new.new_func(&block)))
12
14
  end
13
15
 
14
- def is_write_query?(rql_query)
15
- type_of(rql_query) == :write
16
+ def is_write_query?(rql)
17
+ type_of(rql) == :write
16
18
  end
17
19
 
18
- def type_of(rql_query)
19
- case rql_query.body.first
20
+ def type_of(rql)
21
+ case rql.is_a?(RethinkDB::RQL) && rql.body.is_a?(Array) && rql.body.first
20
22
  when UPDATE, DELETE, REPLACE, INSERT
21
23
  :write
22
- when DB_CREATE, DB_DROP, DB_LIST, TABLE_CREATE, TABLE_DROP, TABLE_LIST, SYNC,
23
- INDEX_CREATE, INDEX_DROP, INDEX_LIST, INDEX_STATUS, INDEX_WAIT
24
+ when DB_CREATE, DB_DROP, DB_LIST, TABLE_CREATE, TABLE_DROP, TABLE_LIST,
25
+ INDEX_CREATE, INDEX_DROP, INDEX_LIST, INDEX_STATUS, INDEX_WAIT, INDEX_RENAME,
26
+ CONFIG, STATUS, WAIT, RECONFIGURE, REBALANCE, SYNC
24
27
  :management
25
28
  else
26
- # XXX Not sure if that's correct, but we'll be happy for logging colors.
29
+ # XXX Not necessarily correct, but we'll be happy for logging colors.
27
30
  :read
28
31
  end
29
32
  end
@@ -0,0 +1,11 @@
1
+ module NoBrainer::SymbolDecoration
2
+ NON_CHAINABLE_OPERATORS = %w(in eq gt ge gte lt le lte defined undefined near intersects include).map(&:to_sym)
3
+ CHAINABLE_OPERATORS = %w(not any all).map(&:to_sym)
4
+ OPERATORS = CHAINABLE_OPERATORS + NON_CHAINABLE_OPERATORS
5
+
6
+ def self.hook
7
+ require 'symbol_decoration'
8
+ Symbol::Decoration.register(*NON_CHAINABLE_OPERATORS)
9
+ Symbol::Decoration.register(*CHAINABLE_OPERATORS, :chainable => true)
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ class NoBrainer::System::ClusterConfig
2
+ include NoBrainer::System::Document
3
+
4
+ field :auth_key
5
+ end
@@ -0,0 +1,5 @@
1
+ class NoBrainer::System::DBConfig
2
+ include NoBrainer::System::Document
3
+
4
+ field :name
5
+ end
@@ -0,0 +1,24 @@
1
+ module NoBrainer::System::Document
2
+ extend ActiveSupport::Concern
3
+
4
+ include NoBrainer::Document
5
+ include NoBrainer::Document::DynamicAttributes
6
+
7
+ included do
8
+ disable_perf_warnings
9
+
10
+ field :id, :type => Object, :default => nil, :primary_key => true
11
+
12
+ default_scope { without_ordering }
13
+ end
14
+
15
+ module ClassMethods
16
+ def table_name
17
+ table_config_options[:name] || name.split('::').last.underscore
18
+ end
19
+
20
+ def rql_table
21
+ RethinkDB::RQL.new.db('rethinkdb').table(table_name)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ class NoBrainer::System::Issue
2
+ include NoBrainer::System::Document
3
+
4
+ table_config :name => 'current_issues'
5
+
6
+ field :type
7
+ field :critical
8
+ field :info
9
+ field :description
10
+ end
@@ -0,0 +1,10 @@
1
+ class NoBrainer::System::Job
2
+ include NoBrainer::System::Document
3
+
4
+ table_config :name => 'jobs'
5
+
6
+ field :duration_sec
7
+ field :info
8
+ field :servers
9
+ field :type
10
+ end
@@ -0,0 +1,11 @@
1
+ class NoBrainer::System::Log
2
+ include NoBrainer::System::Document
3
+
4
+ table_config :name => 'logs'
5
+
6
+ field :level
7
+ field :message
8
+ field :server
9
+ field :timestamp
10
+ field :uptime
11
+ end
@@ -0,0 +1,7 @@
1
+ class NoBrainer::System::ServerConfig
2
+ include NoBrainer::System::Document
3
+
4
+ field :name
5
+ field :cache_size_mb
6
+ field :tags
7
+ end
@@ -0,0 +1,9 @@
1
+ class NoBrainer::System::ServerStatus
2
+ include NoBrainer::System::Document
3
+
4
+ field :name
5
+ field :status
6
+ field :connection
7
+ field :network
8
+ field :process
9
+ end
@@ -0,0 +1,11 @@
1
+ class NoBrainer::System::Stat
2
+ include NoBrainer::System::Document
3
+
4
+ table_config :name => 'stats'
5
+
6
+ field :server
7
+ field :db
8
+ field :table
9
+ field :query_engine
10
+ field :storage_engine
11
+ end
@@ -0,0 +1,10 @@
1
+ class NoBrainer::System::TableConfig
2
+ include NoBrainer::System::Document
3
+
4
+ field :db
5
+ field :name
6
+ field :durability
7
+ field :primary_key
8
+ field :shards
9
+ field :write_acks
10
+ end
@@ -0,0 +1,8 @@
1
+ class NoBrainer::System::TableStatus
2
+ include NoBrainer::System::Document
3
+
4
+ field :db
5
+ field :name
6
+ field :status
7
+ field :shards
8
+ end
@@ -0,0 +1,17 @@
1
+ module NoBrainer::System
2
+ extend NoBrainer::Autoload
3
+ autoload :Document, :ClusterConfig, :DBConfig,
4
+ :Issue, :Job, :Log, :ServerConfig, :ServerStatus,
5
+ :Stat, :TableConfig, :TableStatus
6
+
7
+ # A few shortcuts to make user's life easier
8
+ def self.const_missing(const_name)
9
+ mapping = {:CurrentIssues => :Issue,
10
+ :CurrentIssue => :Issue,
11
+ :Issues => :Issue,
12
+ :Jobs => :Job,
13
+ :Logs => :Log,
14
+ :Stats => :Stat}
15
+ const_get(mapping[const_name] || const_name)
16
+ end
17
+ end
data/lib/nobrainer.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  require 'set'
2
2
  require 'active_support'
3
+ require 'active_model'
3
4
  require 'thread'
4
5
  %w(module/delegation module/attribute_accessors module/introspection
5
6
  class/attribute object/blank object/inclusion object/deep_dup
6
7
  object/try hash/keys hash/indifferent_access hash/reverse_merge
7
- hash/deep_merge array/extract_options)
8
+ hash/deep_merge hash/slice array/extract_options)
8
9
  .each { |dep| require "active_support/core_ext/#{dep}" }
9
10
 
10
11
  module NoBrainer
@@ -13,21 +14,24 @@ module NoBrainer
13
14
 
14
15
  # We eager load things that could be loaded when handling the first web request.
15
16
  # Code that is loaded through the DSL of NoBrainer should not be eager loaded.
16
- autoload :Document, :IndexManager, :Loader, :Fork, :Geo, :Lock
17
- eager_autoload :Config, :Connection, :ConnectionManager, :Error,
18
- :QueryRunner, :Criteria, :RQL
17
+ autoload :Document, :IndexManager, :Loader, :Fork, :Geo, :SymbolDecoration
18
+ eager_autoload :Config, :Connection, :ConnectionManager, :Error,
19
+ :QueryRunner, :Criteria, :RQL, :Lock, :Profiler, :System
19
20
 
20
21
  class << self
21
22
  delegate :connection, :disconnect, :to => 'NoBrainer::ConnectionManager'
22
23
 
23
- delegate :db_create, :db_drop, :db_list,
24
- :table_create, :table_drop, :table_list,
25
- :drop!, :purge!, :to => :connection
24
+ delegate :drop!, :purge!, :default_db, :current_db, :to => :connection
26
25
 
27
- delegate :configure, :logger, :to => 'NoBrainer::Config'
28
- delegate :run, :to => 'NoBrainer::QueryRunner'
29
- delegate :sync_indexes, :to => 'NoBrainer::Document::Index::Synchronizer'
30
- delegate :with, :with_database, :to => 'NoBrainer::QueryRunner::RunOptions'
26
+ delegate :configure, :logger, :to => 'NoBrainer::Config'
27
+ delegate :run, :to => 'NoBrainer::QueryRunner'
28
+ delegate :current_run_options, :run_with, :to => 'NoBrainer::QueryRunner::RunOptions'
29
+
30
+ delegate :with, :with_database, :to => 'NoBrainer::QueryRunner::RunOptions' # deprecated
31
+
32
+ delegate :sync_indexes, :sync_table_config, :sync_schema, :rebalance, :to => 'NoBrainer::Document::TableConfig'
33
+
34
+ delegate :eager_load, :to => 'NoBrainer::Document::Association::EagerLoader'
31
35
 
32
36
  def jruby?
33
37
  RUBY_PLATFORM == 'java'
@@ -35,6 +39,7 @@ module NoBrainer
35
39
  end
36
40
 
37
41
  Fork.hook unless jruby?
42
+ SymbolDecoration.hook
38
43
  end
39
44
 
40
45
  ActiveSupport.on_load(:i18n) do
@@ -0,0 +1,48 @@
1
+ require "rails/generators/nobrainer/namespace_fix"
2
+ require 'rails/generators/base'
3
+
4
+ module NoBrainer::Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ extend NoBrainer::Generators::NamespaceFix
7
+ source_root File.expand_path("../../templates", __FILE__)
8
+
9
+ desc "Disable ActiveRecord and generates ./config/initializers/nobrainer.rb"
10
+
11
+ class RequireProxy
12
+ attr_accessor :required_paths
13
+ def initialize
14
+ self.required_paths = []
15
+ end
16
+
17
+ def require(path)
18
+ self.required_paths << path
19
+ end
20
+
21
+ def resolve_require_path(path)
22
+ $:.map { |dir| File.join(dir, path) }.detect { |f| File.exists?(f) }
23
+ end
24
+ end
25
+
26
+ def expand_require_rails_all
27
+ require_proxy = RequireProxy.new
28
+ rails_all_file = require_proxy.resolve_require_path('rails/all.rb')
29
+ require_proxy.instance_eval(File.read(rails_all_file))
30
+
31
+ gsub_file('config/application.rb', %r(^require 'rails/all'$)) do
32
+ require_proxy.required_paths.map { |f| "require '#{f}'" }.join("\n")
33
+ end
34
+ end
35
+
36
+ def remove_active_record
37
+ (Dir['config/environments/*'] + ['config/application.rb']).each do |config_file|
38
+ comment_lines(config_file, /active_record/)
39
+ end
40
+ remove_file('config/database.yml')
41
+ end
42
+
43
+
44
+ def copy_initializer
45
+ template('nobrainer.rb', 'config/initializers/nobrainer.rb')
46
+ end
47
+ end
48
+ end
@@ -1,7 +1,11 @@
1
- require "rails/generators/nobrainer"
1
+ require "rails/generators/nobrainer/namespace_fix"
2
+ require 'rails/generators/named_base'
2
3
 
3
4
  module NoBrainer::Generators
4
- class ModelGenerator < Base
5
+ class ModelGenerator < Rails::Generators::NamedBase
6
+ extend NoBrainer::Generators::NamespaceFix
7
+ source_root File.expand_path("../../templates", __FILE__)
8
+
5
9
  argument(:attributes, :type => :array, default: [],
6
10
  banner: "field[:type][:index] ... field[:type][:index]")
7
11
 
@@ -10,7 +14,7 @@ module NoBrainer::Generators
10
14
  class_option :parent, :type => :string, :desc => "The parent class for the generated model"
11
15
 
12
16
  def create_model_file
13
- template "model.rb.tt", File.join("app/models", class_path, "#{file_name}.rb")
17
+ template "model.rb", File.join("app", "models", class_path, "#{file_name}.rb")
14
18
  end
15
19
 
16
20
  hook_for :test_framework
@@ -0,0 +1,15 @@
1
+ module NoBrainer::Generators
2
+ module NamespaceFix
3
+ def base_name
4
+ 'nobrainer'
5
+ end
6
+
7
+ def base_root
8
+ File.dirname(__FILE__)
9
+ end
10
+
11
+ def namespace
12
+ super.gsub(/no_brainer/, 'nobrainer')
13
+ end
14
+ end
15
+ end
@@ -7,7 +7,7 @@ class <%= class_name %><%= " < #{options[:parent].classify}" if options[:parent]
7
7
  <% end -%>
8
8
  <% attributes.reject(&:reference?).each do |attribute| -%>
9
9
  field :<%= attribute.name -%>
10
- <%= puts attribute; ", :type => #{attribute.type.to_s.classify}" if attribute.type != :object -%>
10
+ <%= ", :type => #{attribute.type.to_s.classify}" if attribute.type != :object -%>
11
11
  <%= ", :index => true" if attribute.has_index? %>
12
12
  <% end -%>
13
13
  <% attributes.select(&:reference?).each do |attribute| -%>
@@ -0,0 +1,101 @@
1
+ NoBrainer.configure do |config|
2
+ # app_name is the name of your application in lowercase.
3
+ # When using Rails, the application name is automatically inferred.
4
+ # config.app_name = config.default_app_name
5
+
6
+ # environment defaults to Rails.env for Rails apps or to the environment
7
+ # variables RUBY_ENV, RAILS_ENV, RACK_ENV, or :production.
8
+ # config.environment = config.default_environment
9
+
10
+ # rethinkdb_urls specifies the RethinkDB database connection urls.
11
+ # You may specify multiple urls to provide fault tolerance capabilities.
12
+ # When left unspecified, NoBrainer picks a database connection by default.
13
+ # The default is to use localhost, with a database name matching the
14
+ # application name and the environment.
15
+ # NoBrainer also reads environment variables when defined:
16
+ # * RETHINKDB_URL, RDB_URL
17
+ # * RETHINKDB_HOST, RETHINKDB_PORT, RETHINKDB_DB, RETHINKDB_AUTH
18
+ # * RDB_HOST, RDB_PORT, RDB_DB, RDB_AUTH
19
+ # config.rethinkdb_urls = [config.default_rethinkdb_url]
20
+
21
+ # ssl_options may be set to {:ca_certs => '/path/to/ca.crt'} to establish
22
+ # an SSL connection to the RethinkDB servers.
23
+ # config.ssl_options = nil
24
+
25
+ # NoBrainer uses logger to emit debugging information.
26
+ # The default logger is the Rails logger if run with Rails,
27
+ # otherwise Logger.new(STDERR) with a WARN level.
28
+ # If the logger is configured with a DEBUG level,
29
+ # then each database query is emitted.
30
+ # config.logger = config.default_logger
31
+
32
+ # NoBrainer will colorize the queries if colorize_logger is true.
33
+ # Specifically, NoBrainer will colorize management RQL queries in yellow,
34
+ # write queries in red and read queries in green.
35
+ # config.colorize_logger = true
36
+
37
+ # You probably do not want to use both NoBrainer and ActiveRecord in your
38
+ # application. NoBrainer will emit a warning if you do so.
39
+ # You can turn off the warning if you want to use both.
40
+ # config.warn_on_active_record = true
41
+
42
+ # When the network connection is lost, NoBrainer can retry running a given
43
+ # query a few times before giving up. Note that this can be a problem with
44
+ # non idempotent write queries such as increments.
45
+ # Setting it to 0 disable retries during reconnections.
46
+ # The default is 1 for development or test environment, otherwise 15.
47
+ # config.max_retries_on_connection_failure = \
48
+ # config.default_max_retries_on_connection_failure
49
+
50
+ # Configures the durability for database writes.
51
+ # The default is :soft for development or test environment, otherwise :hard.
52
+ # config.durability = config.default_durability
53
+
54
+ # Configures the default table configuration options. These values are
55
+ # reflected to the database when running `rake nobrainer:sync_schema'.
56
+ # config.table_options = { :shards => 1, :replicas => 1, :write_acks => :majority }
57
+
58
+ # Persisted Strings have a configurable maximum length. To get rid of the
59
+ # length validation, you may use the Text type instead.
60
+ # config.max_string_length = 255
61
+
62
+ # user_timezone can be configured with :utc, :local, or :unchanged.
63
+ # When reading an attribute from a model which type is Time, the timezone
64
+ # of that time is translated according to this setting.
65
+ # config.user_timezone = :local
66
+
67
+ # db_timezone can be configured with :utc, :local, or :unchanged.
68
+ # When writting to the database, the timezone of Time attributes are
69
+ # translated according to this setting.
70
+ # config.db_timezone = :utc
71
+
72
+ # Default options used when compiling geo queries.
73
+ # config.geo_options => { :geo_system => 'WGS84', :unit => 'm' }
74
+
75
+ # Configures which mechanism to use in order to perform non-racy uniqueness
76
+ # validations. More about this behavior in the Distributed Locks section.
77
+ # config.distributed_lock_class = "NoBrainer::Lock"
78
+
79
+ # Configures the default timing lock options.
80
+ # config.lock_options = { :expire => 60, :timeout => 10 }
81
+
82
+ # Instead of using a single connection to the database, You can tell
83
+ # NoBrainer to spin up a new connection for each thread. This is
84
+ # useful for multi-threading usage such as Sidekiq.
85
+ # Call NoBrainer.disconnect before a thread exits, otherwise you will have
86
+ # a resource leak, and you will run out of connections.
87
+ # Note that this is solution is temporary, until we get a connection pool.
88
+ # config.per_thread_connection = false
89
+
90
+ # The machine id is used to generate primary keys. The default one is seeded
91
+ # with the machine IP with Socket.gethostname.
92
+ # The env variable MACHINE_ID can also be used to set the machine id.
93
+ # When using distinct machine_id, then primary keys are guaranteed to be
94
+ # generated without conflicts.
95
+ # config.machine_id = config.default_machine_id
96
+
97
+ # Criteria cache elements. For example, the result of a has_many association
98
+ # is cached. The per criteria cache is disabled if it grows too big to avoid
99
+ # out of memory issues.
100
+ # config.criteria_cache_max_entries = 10_000
101
+ end