plucky 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,8 +2,6 @@
2
2
 
3
3
  Thin layer over the ruby driver that allows you to quickly grab hold of your data (pluck it!).
4
4
 
5
- *Still rough around the edges and changing rapidly.*
6
-
7
5
  == Install
8
6
 
9
7
  $ gem install plucky
@@ -13,7 +13,7 @@ module Plucky
13
13
  @options = @options.dup
14
14
  @source = @source.dup
15
15
  each do |key, value|
16
- self[key] = value.dup if value.is_a?(Array) || value.is_a?(Hash)
16
+ self[key] = value.clone if value.duplicable?
17
17
  end
18
18
  end
19
19
 
@@ -1,30 +1,3 @@
1
1
  # encoding: UTF-8
2
- class SymbolOperator
3
- include Comparable
4
-
5
- attr_reader :field, :operator
6
-
7
- def initialize(field, operator, options={})
8
- @field, @operator = field, operator
9
- end unless method_defined?(:initialize)
10
-
11
- def <=>(other)
12
- if field == other.field
13
- operator <=> other.operator
14
- else
15
- field.to_s <=> other.field.to_s
16
- end
17
- end
18
-
19
- def ==(other)
20
- field == other.field && operator == other.operator
21
- end
22
- end
23
-
24
- class Symbol
25
- %w(gt lt gte lte ne in nin mod all size exists asc desc).each do |operator|
26
- define_method(operator) do
27
- SymbolOperator.new(self, operator)
28
- end unless method_defined?(operator)
29
- end
30
- end
2
+ require 'plucky/extensions/duplicable'
3
+ require 'plucky/extensions/symbol'
@@ -0,0 +1,86 @@
1
+ # Copyright (c) 2005-2010 David Heinemeier Hansson
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+ #
22
+ # Most objects are cloneable, but not all. For example you can't dup +nil+:
23
+ #
24
+ # nil.dup # => TypeError: can't dup NilClass
25
+ #
26
+ # Classes may signal their instances are not duplicable removing +dup+/+clone+
27
+ # or raising exceptions from them. So, to dup an arbitrary object you normally
28
+ # use an optimistic approach and are ready to catch an exception, say:
29
+ #
30
+ # arbitrary_object.dup rescue object
31
+ #
32
+ # Rails dups objects in a few critical spots where they are not that arbitrary.
33
+ # That rescue is very expensive (like 40 times slower than a predicate), and it
34
+ # is often triggered.
35
+ #
36
+ # That's why we hardcode the following cases and check duplicable? instead of
37
+ # using that rescue idiom.
38
+ class Object
39
+ # Can you safely .dup this object?
40
+ # False for nil, false, true, symbols, numbers, class and module objects; true otherwise.
41
+ def duplicable?
42
+ true
43
+ end
44
+ end
45
+
46
+ class NilClass #:nodoc:
47
+ def duplicable?
48
+ false
49
+ end
50
+ end
51
+
52
+ class FalseClass #:nodoc:
53
+ def duplicable?
54
+ false
55
+ end
56
+ end
57
+
58
+ class TrueClass #:nodoc:
59
+ def duplicable?
60
+ false
61
+ end
62
+ end
63
+
64
+ class Symbol #:nodoc:
65
+ def duplicable?
66
+ false
67
+ end
68
+ end
69
+
70
+ class Numeric #:nodoc:
71
+ def duplicable?
72
+ false
73
+ end
74
+ end
75
+
76
+ class Class #:nodoc:
77
+ def duplicable?
78
+ false
79
+ end
80
+ end
81
+
82
+ class Module #:nodoc:
83
+ def duplicable?
84
+ false
85
+ end
86
+ end
@@ -0,0 +1,84 @@
1
+ # encoding: UTF-8
2
+ module Plucky
3
+ module Extensions
4
+ module Symbol
5
+ def gt
6
+ SymbolOperator.new(self, 'gt')
7
+ end
8
+
9
+ def lt
10
+ SymbolOperator.new(self, 'lt')
11
+ end
12
+
13
+ def gte
14
+ SymbolOperator.new(self, 'gte')
15
+ end
16
+
17
+ def lte
18
+ SymbolOperator.new(self, 'lte')
19
+ end
20
+
21
+ def ne
22
+ SymbolOperator.new(self, 'ne')
23
+ end
24
+
25
+ def in
26
+ SymbolOperator.new(self, 'in')
27
+ end
28
+
29
+ def nin
30
+ SymbolOperator.new(self, 'nin')
31
+ end
32
+
33
+ def mod
34
+ SymbolOperator.new(self, 'mod')
35
+ end
36
+
37
+ def all
38
+ SymbolOperator.new(self, 'all')
39
+ end
40
+
41
+ def size
42
+ SymbolOperator.new(self, 'size')
43
+ end unless Symbol.instance_methods.include?(:size) # Ruby 1.9 defines symbol size
44
+
45
+ def exists
46
+ SymbolOperator.new(self, 'exists')
47
+ end
48
+
49
+ def asc
50
+ SymbolOperator.new(self, 'asc')
51
+ end
52
+
53
+ def desc
54
+ SymbolOperator.new(self, 'desc')
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ class SymbolOperator
61
+ include Comparable
62
+
63
+ attr_reader :field, :operator
64
+
65
+ def initialize(field, operator, options={})
66
+ @field, @operator = field, operator
67
+ end unless method_defined?(:initialize)
68
+
69
+ def <=>(other)
70
+ if field == other.field
71
+ operator <=> other.operator
72
+ else
73
+ field.to_s <=> other.field.to_s
74
+ end
75
+ end
76
+
77
+ def ==(other)
78
+ field == other.field && operator == other.operator
79
+ end
80
+ end
81
+
82
+ class Symbol
83
+ include Plucky::Extensions::Symbol
84
+ end
@@ -12,7 +12,7 @@ module Plucky
12
12
  super
13
13
  @source = @source.dup
14
14
  each do |key, value|
15
- self[key] = value.dup if value.is_a?(Array) || value.is_a?(Hash)
15
+ self[key] = value.clone if value.duplicable?
16
16
  end
17
17
  end
18
18
 
data/lib/plucky/query.rb CHANGED
@@ -2,16 +2,18 @@
2
2
  require 'forwardable'
3
3
  module Plucky
4
4
  class Query
5
- extend Forwardable
5
+ include Enumerable
6
+ extend Forwardable
6
7
 
7
8
  OptionKeys = [
8
9
  :select, :offset, :order, # MM
9
10
  :fields, :skip, :limit, :sort, :hint, :snapshot, :batch_size, :timeout # Ruby Driver
10
11
  ]
11
12
 
12
- attr_reader :criteria, :options, :collection
13
- def_delegator :criteria, :simple?
14
- def_delegator :options, :fields?
13
+ attr_reader :criteria, :options, :collection
14
+ def_delegator :criteria, :simple?
15
+ def_delegator :options, :fields?
16
+ def_delegators :to_a, :each
15
17
 
16
18
  def initialize(collection, opts={})
17
19
  @collection, @options, @criteria = collection, OptionsHash.new, CriteriaHash.new
@@ -50,7 +52,7 @@ module Plucky
50
52
  end
51
53
  end
52
54
 
53
- def find_many(opts={})
55
+ def find_each(opts={})
54
56
  query = clone.update(opts)
55
57
  query.collection.find(query.criteria.to_hash, query.options.to_hash)
56
58
  end
@@ -70,7 +72,7 @@ module Plucky
70
72
  end
71
73
 
72
74
  def all(opts={})
73
- find_many(opts).to_a
75
+ find_each(opts).to_a
74
76
  end
75
77
 
76
78
  def first(opts={})
@@ -87,7 +89,11 @@ module Plucky
87
89
  end
88
90
 
89
91
  def count(opts={})
90
- find_many(opts).count
92
+ find_each(opts).count
93
+ end
94
+
95
+ def size
96
+ count
91
97
  end
92
98
 
93
99
  def update(opts={})
@@ -125,6 +131,14 @@ module Plucky
125
131
  end
126
132
  end
127
133
 
134
+ def empty?
135
+ count.zero?
136
+ end
137
+
138
+ def to_a
139
+ all
140
+ end
141
+
128
142
  def [](key)
129
143
  key = key.to_sym if key.respond_to?(:to_sym)
130
144
  if OptionKeys.include?(key)
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module Plucky
3
- Version = '0.2.1'
3
+ Version = '0.3.0'
4
4
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 1
9
- version: 0.2.1
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - John Nunemaker
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-30 00:00:00 -04:00
17
+ date: 2010-06-15 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -84,6 +84,8 @@ extra_rdoc_files: []
84
84
 
85
85
  files:
86
86
  - lib/plucky/criteria_hash.rb
87
+ - lib/plucky/extensions/duplicable.rb
88
+ - lib/plucky/extensions/symbol.rb
87
89
  - lib/plucky/extensions.rb
88
90
  - lib/plucky/options_hash.rb
89
91
  - lib/plucky/pagination/decorator.rb