Parsistence 0.3.10 → 0.3.11

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.
@@ -117,7 +117,12 @@ module Parsistence
117
117
  value = value.PFObject if value.respond_to? :PFObject # unwrap object
118
118
  if has_many.include?(field.to_sym)
119
119
  relation = @PFObject.relationforKey(field)
120
- return relation.addObject(value) if relations.include? field.to_sym
120
+ if value.nil?
121
+ # Can't do it
122
+ raise "Can't set nil for has_many relation."
123
+ else
124
+ return relation.addObject(value) if relations.include? field.to_sym
125
+ end
121
126
  elsif belongs_to.include?(field.to_sym)
122
127
  return setField(field, value)
123
128
  end
@@ -1,7 +1,7 @@
1
1
  module Parsistence
2
2
  module Model
3
3
  module ClassMethods
4
- QUERY_STUBS = [ :where, :first, :limit, :order, :eq, :notEq, :lt, :gt, :lte, :gte, :in ] # :limit is different
4
+ QUERY_STUBS = [ :where, :first, :limit, :order, :eq, :notEq, :lt, :gt, :lte, :gte, :in, :cached ] # :limit is different
5
5
 
6
6
  def fetchAll(&block)
7
7
  q = Parsistence::Query.new
@@ -51,6 +51,22 @@ module Parsistence
51
51
  class Query
52
52
  attr_accessor :klass
53
53
 
54
+ def self.cache_policy=(policy)
55
+ @@cache_policy = policy
56
+ end
57
+
58
+ def self.cache_policy
59
+ @@cache_policy ||= KPFCachePolicyIgnoreCache
60
+ end
61
+
62
+ def self.cache=(val)
63
+ if val
64
+ self.cache_policy = KPFCachePolicyCacheThenNetwork
65
+ else
66
+ self.cache_policy = KPFCachePolicyNetworkOnly
67
+ end
68
+ end
69
+
54
70
  def initialize
55
71
  @conditions = {}
56
72
  @negativeConditions = {}
@@ -79,6 +95,15 @@ module Parsistence
79
95
  else
80
96
  query = PFQuery.queryWithClassName(self.klass.to_s)
81
97
  end
98
+
99
+ if @cached == true
100
+ query.cachePolicy = KPFCachePolicyCacheElseNetwork
101
+ elsif @cached == false
102
+ query.cachePolicy = KPFCachePolicyNetworkOnly
103
+ else
104
+ query.cachePolicy = self.class.cache_policy if self.class.cache_policy
105
+ end
106
+
82
107
  @includes.each do |include|
83
108
  query.includeKey(include)
84
109
  end
@@ -137,15 +162,19 @@ module Parsistence
137
162
  query
138
163
  end
139
164
 
140
- # TODO: Get this working.
141
- # def count(&callback)
142
- # query = createQuery
143
-
144
- # myKlass = self.klass
145
- # query.countObjectsInBackgroundWithBlock (lambda { |item_count, error|
146
- # callback.call item_count, error
147
- # })
148
- # end
165
+ def count(&callback)
166
+ query = createQuery
167
+ error = Pointer.new(:object)
168
+ query.countObjectsInBackgroundWithBlock lambda { |item_count, error|
169
+ if error
170
+ callback.call false
171
+ else
172
+ callback.call item_count
173
+ end
174
+ }
175
+
176
+ self
177
+ end
149
178
 
150
179
  def fetch(&callback)
151
180
  if @limit && @limit == 1
@@ -169,9 +198,14 @@ module Parsistence
169
198
 
170
199
  myKlass = self.klass
171
200
  query.findObjectsInBackgroundWithBlock (lambda { |items, error|
201
+ modelItems = []
172
202
  modelItems = items.map! { |item| myKlass.new(item) } if items
173
- callback.call modelItems, error
203
+ callback.call(modelItems, error) if callback.arity == 2
204
+ callback.call(modelItems) if callback.arity == 1
205
+ callback.call if callback.arity == 0
174
206
  })
207
+
208
+ self
175
209
  end
176
210
 
177
211
  # @deprecated
@@ -188,8 +222,12 @@ module Parsistence
188
222
  myKlass = self.klass
189
223
  query.getFirstObjectInBackgroundWithBlock (lambda { |item, error|
190
224
  modelItem = myKlass.new(item) if item
191
- callback.call modelItem, error
225
+ callback.call(modelItem, error) if callback.arity == 2
226
+ callback.call(modelItem) if callback.arity == 1
227
+ callback.call if callback.arity == 0
192
228
  })
229
+
230
+ self
193
231
  end
194
232
 
195
233
  # @deprecated
@@ -335,6 +373,11 @@ module Parsistence
335
373
  self
336
374
  end
337
375
 
376
+ def cached(val=true)
377
+ @cached = val
378
+ self
379
+ end
380
+
338
381
  # Include related object in query
339
382
  #
340
383
  # @param [Hash] fields
@@ -1,3 +1,3 @@
1
1
  module Parsistence
2
- VERSION = "0.3.10" unless defined? Parsistence::VERSION
2
+ VERSION = "0.3.11" unless defined? Parsistence::VERSION
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Parsistence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.10
4
+ version: 0.3.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-24 00:00:00.000000000 Z
13
+ date: 2013-01-08 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Your models on RubyMotion and Parse in a persistence.js style pattern.
16
16
  email: