nobrainer 0.21.0 → 0.22.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2f2246b107585644fff659c53223f7c821bbc628
4
- data.tar.gz: 9c2b99c8969e58ac8a3e9c447a887ab30d6ee402
3
+ metadata.gz: c53755e5f9ec902612c417fcf053404cf114ca0b
4
+ data.tar.gz: 529fe4b627d366ba4813fd6ff750192caf0df316
5
5
  SHA512:
6
- metadata.gz: 96d2a9de23fc93e7283618a61107043d02fdf4e04623201e654663e4973f248d20122bdfa79f6818580d71169f7d65f56b2dfb4b75dd26f554ed0094996fe6ff
7
- data.tar.gz: 7c16007cb6fd8c4e1b0d9bb1b2a590f4cddb489f78c9215315870cfacea4cda86b6de1b63429a46d9641bb3c77216d0a5108c48f4b78459e06bdee174bbcfa4d
6
+ metadata.gz: 920d6b1c7bec9acd28b4a0e7f54a9967d068a01d31174396ca961eaf8cbfec31e3b8397ab2242ac87c73b9ca3789e80b47c8ca4b809d7a71b5fa80aeedb81f4b
7
+ data.tar.gz: 92144b91eb03ac14d2a01ea5244480b215286228691e5406a9f7bcf218499853ecfb0d1313f1380f62d509d97dc2bb1dac65ae8e97c0c2c3cc2b7b4c3835e1c2
@@ -3,6 +3,7 @@ require 'rethinkdb'
3
3
  class NoBrainer::Criteria
4
4
  extend NoBrainer::Autoload
5
5
  autoload_and_include :Core, :Raw, :AfterFind, :Where, :OrderBy, :Limit,
6
- :Pluck, :Count, :Delete, :Enumerable, :First, :Aggregate,
7
- :EagerLoad, :Update, :Cache, :Index, :Extend, :Scope
6
+ :Pluck, :Count, :Delete, :Enumerable, :First, :Find,
7
+ :Aggregate, :EagerLoad, :Update, :Cache, :Index,
8
+ :Extend, :Scope
8
9
  end
@@ -0,0 +1,27 @@
1
+ module NoBrainer::Criteria::Find
2
+ extend ActiveSupport::Concern
3
+
4
+ def find_by?(*args, &block)
5
+ where(*args, &block).first
6
+ end
7
+
8
+ def find_by(*args, &block)
9
+ find_by?(*args, &block).tap { |doc| raise_not_found(args) unless doc }
10
+ end
11
+ alias_method :find_by!, :find_by
12
+
13
+ def find?(pk)
14
+ without_ordering.find_by?(model.pk_name => pk)
15
+ end
16
+
17
+ def find(pk)
18
+ without_ordering.find_by(model.pk_name => pk)
19
+ end
20
+ alias_method :find!, :find
21
+
22
+ private
23
+
24
+ def raise_not_found(args)
25
+ raise NoBrainer::Error::DocumentNotFound, "#{model} #{args.inspect.gsub(/\[{(.*)}\]/, '\1')} not found"
26
+ end
27
+ end
@@ -2,7 +2,8 @@ module NoBrainer::Document::Criteria
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  def selector
5
- self.class.selector_for(pk_value)
5
+ # Used for writes
6
+ self.class.rql_table.get(pk_value)
6
7
  end
7
8
 
8
9
  included do
@@ -28,6 +29,7 @@ module NoBrainer::Document::Criteria
28
29
  :min, :max, :sum, :avg, # Aggregate
29
30
  :update_all, :replace_all, # Update
30
31
  :pluck, :without, :lazy_fetch, :without_plucking, # Pluck
32
+ :find_by?, :find_by, :find_by!, :find?, :find, :find!, # Find
31
33
  :to => :all
32
34
 
33
35
  def all
@@ -56,20 +58,6 @@ module NoBrainer::Document::Criteria
56
58
  super
57
59
  end
58
60
 
59
- def selector_for(pk)
60
- rql_table.get(pk)
61
- end
62
-
63
- def find?(pk)
64
- attrs = NoBrainer.run { selector_for(pk) }
65
- new_from_db(attrs).tap { |doc| doc.run_callbacks(:find) } if attrs
66
- end
67
-
68
- def find(pk)
69
- find?(pk).tap { |doc| raise NoBrainer::Error::DocumentNotFound, "#{self} #{pk_name}: #{pk} not found" unless doc }
70
- end
71
- alias_method :find!, :find
72
-
73
61
  def disable_perf_warnings
74
62
  self.perf_warnings_disabled = true
75
63
  end
@@ -29,7 +29,7 @@ module NoBrainer::Document::Persistance
29
29
 
30
30
  def _reload(options={})
31
31
  attrs = NoBrainer.run { _reload_selector(options) }
32
- raise NoBrainer::Error::DocumentNotFound, "#{self.class} #{self.class.pk_name}: #{pk_value} not found" unless attrs
32
+ raise NoBrainer::Error::DocumentNotFound, "#{self.class} :#{self.class.pk_name}=>\"#{pk_value}\" not found" unless attrs
33
33
 
34
34
  options = options.merge(:pristine => true, :from_db => true)
35
35
 
@@ -126,15 +126,15 @@ module NoBrainer::Document::Persistance
126
126
  alias_method :update_attributes!, :update!
127
127
 
128
128
  def update_attributes?(*args)
129
- update?(*args).tap { STDERR.puts "[NoBrainer] update_attribute?() is deprecated. Please use update?() instead" }
129
+ update?(*args).tap { STDERR.puts "[NoBrainer] update_attributes?() is deprecated. Please use update?() instead" }
130
130
  end
131
131
 
132
132
  def update_attributes(*args)
133
- update(*args).tap { STDERR.puts "[NoBrainer] update_attribute() is deprecated. Please use update() instead" }
133
+ update(*args).tap { STDERR.puts "[NoBrainer] update_attributes() is deprecated. Please use update() instead" }
134
134
  end
135
135
 
136
136
  def update_attributes!(*args)
137
- update!(*args).tap { STDERR.puts "[NoBrainer] update_attribute!() is deprecated. Please use update() instead" }
137
+ update!(*args).tap { STDERR.puts "[NoBrainer] update_attributes!() is deprecated. Please use update() instead" }
138
138
  end
139
139
 
140
140
  def delete
@@ -16,13 +16,13 @@ module NoBrainer::Document::StoreIn
16
16
 
17
17
  def database_name
18
18
  db = self.store_in_options[:database]
19
- db.is_a?(Proc) ? db.call : db
19
+ (db.is_a?(Proc) ? db.call : db).try(:to_s)
20
20
  end
21
21
 
22
22
  def table_name
23
23
  table = store_in_options[:table]
24
24
  table_name = table.is_a?(Proc) ? table.call : table
25
- table_name || root_class.name.tableize.gsub('/', '__')
25
+ table_name.try(:to_s) || root_class.name.tableize.gsub('/', '__')
26
26
  end
27
27
 
28
28
  def rql_table
@@ -4,7 +4,7 @@ class NoBrainer::Binary
4
4
  def self.to_s; inspect; end
5
5
  def self.name; inspect; end
6
6
 
7
- module NoBrainerExtentions
7
+ module NoBrainerExtensions
8
8
  InvalidType = NoBrainer::Error::InvalidType
9
9
 
10
10
  def nobrainer_cast_user_to_model(value)
@@ -14,5 +14,5 @@ class NoBrainer::Binary
14
14
  end
15
15
  end
16
16
  end
17
- extend NoBrainerExtentions
17
+ extend NoBrainerExtensions
18
18
  end
@@ -4,7 +4,7 @@ class NoBrainer::Boolean
4
4
  def self.to_s; inspect; end
5
5
  def self.name; inspect; end
6
6
 
7
- module NoBrainerExtentions
7
+ module NoBrainerExtensions
8
8
  InvalidType = NoBrainer::Error::InvalidType
9
9
 
10
10
  def nobrainer_cast_user_to_model(value)
@@ -20,5 +20,5 @@ class NoBrainer::Boolean
20
20
  end
21
21
  end
22
22
  end
23
- extend NoBrainerExtentions
23
+ extend NoBrainerExtensions
24
24
  end
@@ -1,5 +1,5 @@
1
1
  class Date
2
- module NoBrainerExtentions
2
+ module NoBrainerExtensions
3
3
  InvalidType = NoBrainer::Error::InvalidType
4
4
 
5
5
  def nobrainer_cast_user_to_model(value)
@@ -22,5 +22,5 @@ class Date
22
22
  value.is_a?(Date) ? Time.utc(value.year, value.month, value.day) : value
23
23
  end
24
24
  end
25
- extend NoBrainerExtentions
25
+ extend NoBrainerExtensions
26
26
  end
@@ -1,5 +1,5 @@
1
1
  class Float
2
- module NoBrainerExtentions
2
+ module NoBrainerExtensions
3
3
  InvalidType = NoBrainer::Error::InvalidType
4
4
 
5
5
  def nobrainer_cast_user_to_model(value)
@@ -16,5 +16,5 @@ class Float
16
16
  end
17
17
  end
18
18
  end
19
- extend NoBrainerExtentions
19
+ extend NoBrainerExtensions
20
20
  end
@@ -1,5 +1,5 @@
1
1
  class Integer
2
- module NoBrainerExtentions
2
+ module NoBrainerExtensions
3
3
  InvalidType = NoBrainer::Error::InvalidType
4
4
 
5
5
  def nobrainer_cast_user_to_model(value)
@@ -14,5 +14,5 @@ class Integer
14
14
  end
15
15
  end
16
16
  end
17
- extend NoBrainerExtentions
17
+ extend NoBrainerExtensions
18
18
  end
@@ -1,5 +1,5 @@
1
1
  class Set
2
- module NoBrainerExtentions
2
+ module NoBrainerExtensions
3
3
  InvalidType = NoBrainer::Error::InvalidType
4
4
 
5
5
  def nobrainer_cast_user_to_model(value)
@@ -19,5 +19,5 @@ class Set
19
19
  end
20
20
  end
21
21
 
22
- extend NoBrainerExtentions
22
+ extend NoBrainerExtensions
23
23
  end
@@ -1,5 +1,5 @@
1
1
  class String
2
- module NoBrainerExtentions
2
+ module NoBrainerExtensions
3
3
  InvalidType = NoBrainer::Error::InvalidType
4
4
 
5
5
  def nobrainer_cast_user_to_model(value)
@@ -13,5 +13,5 @@ class String
13
13
  end
14
14
  end
15
15
  end
16
- extend NoBrainerExtentions
16
+ extend NoBrainerExtensions
17
17
  end
@@ -1,5 +1,5 @@
1
1
  class Symbol
2
- module NoBrainerExtentions
2
+ module NoBrainerExtensions
3
3
  InvalidType = NoBrainer::Error::InvalidType
4
4
 
5
5
  def nobrainer_cast_user_to_model(value)
@@ -17,5 +17,5 @@ class Symbol
17
17
  value.to_sym rescue (value.to_s.to_sym rescue value)
18
18
  end
19
19
  end
20
- extend NoBrainerExtentions
20
+ extend NoBrainerExtensions
21
21
  end
@@ -4,7 +4,7 @@ class NoBrainer::Text
4
4
  def self.to_s; inspect; end
5
5
  def self.name; inspect; end
6
6
 
7
- module NoBrainerExtentions
7
+ module NoBrainerExtensions
8
8
  InvalidType = NoBrainer::Error::InvalidType
9
9
 
10
10
  def nobrainer_cast_user_to_model(value)
@@ -14,5 +14,5 @@ class NoBrainer::Text
14
14
  end
15
15
  end
16
16
  end
17
- extend NoBrainerExtentions
17
+ extend NoBrainerExtensions
18
18
  end
@@ -1,7 +1,7 @@
1
1
  require 'time'
2
2
 
3
3
  class Time
4
- module NoBrainerExtentions
4
+ module NoBrainerExtensions
5
5
  InvalidType = NoBrainer::Error::InvalidType
6
6
 
7
7
  def nobrainer_cast_user_to_model(value)
@@ -37,5 +37,5 @@ class Time
37
37
  end
38
38
  end
39
39
  end
40
- extend NoBrainerExtentions
40
+ extend NoBrainerExtensions
41
41
  end
@@ -43,7 +43,11 @@ end
43
43
 
44
44
  class ActiveModel::EachValidator
45
45
  def should_validate_field?(record, attribute)
46
- record.new_record? || record.__send__("#{attribute}_changed?")
46
+ return true unless record.is_a?(NoBrainer::Document)
47
+ return true if record.new_record?
48
+
49
+ attr_changed = "#{attribute}_changed?"
50
+ return record.respond_to?(attr_changed) ? record.__send__(attr_changed) : true
47
51
  end
48
52
 
49
53
  # XXX Monkey Patching :(
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nobrainer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Viennot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-19 00:00:00.000000000 Z
11
+ date: 2015-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rethinkdb
@@ -103,6 +103,7 @@ files:
103
103
  - lib/no_brainer/criteria/eager_load.rb
104
104
  - lib/no_brainer/criteria/enumerable.rb
105
105
  - lib/no_brainer/criteria/extend.rb
106
+ - lib/no_brainer/criteria/find.rb
106
107
  - lib/no_brainer/criteria/first.rb
107
108
  - lib/no_brainer/criteria/index.rb
108
109
  - lib/no_brainer/criteria/limit.rb