parsecom 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -132,8 +132,7 @@ If you want to update attributes without retrieving the object, you can use
132
132
  the Parse::Client object for it.
133
133
 
134
134
  ```ruby
135
- score = GameScore.new :objectId => 'Ed1nuqPvcm'
136
- Parse::Client.default_client.update score, :score => 73453
135
+ Parse::Client.default.update :GaemScore, 'Ed1nuqPvcm', :score => 73453
137
136
  ```
138
137
 
139
138
  ### Deleting Objects
@@ -7,7 +7,7 @@ module Parse
7
7
 
8
8
  attr_accessor :http_client, :session_token, :master_key
9
9
 
10
- def self.default_client
10
+ def self.default
11
11
  @@default_client ||= new
12
12
  end
13
13
 
@@ -62,22 +62,20 @@ module Parse
62
62
  @session_token = nil
63
63
  end
64
64
 
65
- def find parse_class, object_id_or_conditions, opts={}
65
+ def find parse_class_name, object_id_or_conditions, opts={}
66
66
  case object_id_or_conditions
67
67
  when :all
68
- find_by_query parse_class, opts
68
+ find_by_query parse_class_name, opts
69
69
  when String, Symbol
70
- find_by_id parse_class, object_id_or_conditions, opts
70
+ find_by_id parse_class_name, object_id_or_conditions, opts
71
71
  when Hash
72
- find_by_query parse_class, object_id_or_conditions
72
+ find_by_query parse_class_name, object_id_or_conditions
73
73
  else
74
74
  raise ArgumentError.new('the first argument should be a string, a symbol, or a hash.')
75
75
  end
76
76
  end
77
77
 
78
- def find_by_id parse_class, object_id, opts={}
79
- parse_class_name = parse_class.is_a?(Parse::Object) \
80
- ? parse_class.parse_class_name : parse_class
78
+ def find_by_id parse_class_name, object_id, opts={}
81
79
  call_api :get, "classes/#{parse_class_name}/#{object_id}" do |resp_body|
82
80
  if opts.has_key? :include
83
81
  included_keys = [opts[:include]].flatten
@@ -95,8 +93,8 @@ module Parse
95
93
  end
96
94
  end
97
95
 
98
- def find_by_query parse_class, conditions
99
- query = Query.new parse_class
96
+ def find_by_query parse_class_name, conditions
97
+ query = Query.new parse_class_name, self
100
98
  query.limit conditions[:limit] if conditions.has_key? :limit
101
99
  query.skip conditions[:skip] if conditions.has_key? :skip
102
100
  query.count conditions[:count] if conditions.has_key? :count
@@ -128,20 +126,20 @@ module Parse
128
126
  query.invoke
129
127
  end
130
128
 
131
- def create parse_object, values
132
- call_api :post, "classes/#{parse_object.parse_class_name}", values.to_json do |resp_body|
129
+ def create parse_class_name, values
130
+ call_api :post, "classes/#{parse_class_name}", values.to_json do |resp_body|
133
131
  resp_body
134
132
  end
135
133
  end
136
134
 
137
- def update parse_object, values
138
- call_api :put, "classes/#{parse_object.parse_class_name}/#{parse_object.parse_object_id}", values.to_json do |resp_body|
135
+ def update parse_class_name, parse_object_id, values
136
+ call_api :put, "classes/#{parse_class_name}/#{parse_object_id}", values.to_json do |resp_body|
139
137
  resp_body
140
138
  end
141
139
  end
142
140
 
143
- def delete parse_object
144
- call_api :delete, "classes/#{parse_object.parse_class_name}/#{parse_object.parse_object_id}" do |resp_body|
141
+ def delete parse_class_name, parse_object_id
142
+ call_api :delete, "classes/#{parse_class_name}/#{parse_object_id}" do |resp_body|
145
143
  resp_body
146
144
  end
147
145
  end
@@ -0,0 +1,6 @@
1
+ # coding:utf-8
2
+ module Parse
3
+ class Installation < Object
4
+ # TODO
5
+ end
6
+ end
@@ -1,7 +1,9 @@
1
1
  # coding:utf-8
2
2
  module Parse
3
3
  RESERVED_PARSE_CLASS = {
4
- '_User' => 'Parse::User'
4
+ '_User' => 'Parse::User',
5
+ '_Role' => 'Parse::Role',
6
+ '_Installation' => 'Parse::Installation'
5
7
  }
6
8
 
7
9
  class Object
@@ -33,11 +35,11 @@ module Parse
33
35
  end
34
36
 
35
37
  def parse_client
36
- @parse_client ||= Parse::Client.default_client
38
+ @parse_client ||= Parse::Client.default
37
39
  end
38
40
 
39
41
  def find object_id_or_conditions, opts={}
40
- results = [parse_client.find(self, object_id_or_conditions, opts)].flatten
42
+ results = [parse_client.find(self.parse_class_name, object_id_or_conditions, opts)].flatten
41
43
  results.map! {|hash| self.new hash}
42
44
  end
43
45
 
@@ -47,7 +49,7 @@ module Parse
47
49
 
48
50
  # TODO: need refactoring
49
51
  def find! object_id_or_conditions, opts={}
50
- results = [parse_client.find!(self, object_id_or_conditions, opts)].flatten
52
+ results = [parse_client.find!(self.parse_class_name, object_id_or_conditions, opts)].flatten
51
53
  results.map! {|hash| self.new hash}
52
54
  end
53
55
 
@@ -90,7 +92,9 @@ module Parse
90
92
  when 'File'
91
93
  Parse::File.new v
92
94
  when 'Pointer'
93
- Parse::Pointer.new self, v
95
+ Parse::Pointer.new v, self
96
+ when 'Relation'
97
+ Parse::Relation.new self, k, v
94
98
  else
95
99
  v
96
100
  end
@@ -141,9 +145,8 @@ module Parse
141
145
  check_deleted!
142
146
  hash = string_keyed_hash hash
143
147
  @updated_hash.update hash
144
- #parse_client.create(self, @updated_hash).tap do |response|
145
148
  method = use_master_key ? :create! : :create
146
- parse_client.send(method, self, @updated_hash).tap do |response|
149
+ parse_client.send(method, self.parse_class_name, @updated_hash).tap do |response|
147
150
  @parse_object_id = response['objectId']
148
151
  @created_at = Date.parse response['createdAt']
149
152
  @updated_at = @created_at
@@ -162,7 +165,7 @@ module Parse
162
165
  hash = string_keyed_hash hash
163
166
  #parse_client.update(self, hash).tap do |response|
164
167
  method = use_master_key ? :update! : :update
165
- parse_client.send(method, self, hash).tap do |response|
168
+ parse_client.send(method, parse_class_name, parse_object_id, hash).tap do |response|
166
169
  @updated_at = Date.parse response['updatedAt']
167
170
  @raw_hash.update @updated_hash
168
171
  @updated_hash.clear
@@ -176,9 +179,8 @@ module Parse
176
179
  def delete use_master_key=false
177
180
  raise 'You cannot delete new object' if new?
178
181
  check_deleted!
179
- #parse_client.delete(self).tap do |response|
180
182
  method = use_master_key ? :delete! : :delete
181
- parse_client.send(method, self).tap do |response|
183
+ parse_client.send(method, parse_class_name, parse_object_id).tap do |response|
182
184
  @deleted = true
183
185
  end
184
186
  end
@@ -214,6 +216,10 @@ module Parse
214
216
  @updated_hash[name] = value
215
217
  end
216
218
 
219
+ def to_pointer
220
+ Pointer.new 'className' => parse_class_name, 'objectId' => parse_object_id
221
+ end
222
+
217
223
  def to_s
218
224
  "<#{parse_class_name}: #{{}.update(@raw_hash).update(@updated_hash).to_s}>"
219
225
  end
@@ -3,9 +3,9 @@ module Parse
3
3
  class Pointer
4
4
  attr_reader :object
5
5
 
6
- def initialize parent, hash
7
- @parent_object = parent
6
+ def initialize hash, parent=nil
8
7
  @raw_hash = hash
8
+ @parent_object = parent
9
9
 
10
10
  if @raw_hash.has_key? 'body'
11
11
  @object = pointed_parse_class.new @raw_hash['body']
@@ -13,10 +13,11 @@ module Parse
13
13
  end
14
14
 
15
15
  def load
16
- unless @object
17
- @object = pointed_parse_class.find_by_id @raw_hash['objectId']
18
- end
19
- @object
16
+ @object ||= pointed_parse_class.find_by_id @raw_hash['objectId']
17
+ end
18
+
19
+ def to_json
20
+ %Q|{"__type":"Pointer","className":"#{@raw_hash['className']}","objectId":"#{@raw_hash['objectId']}"}|
20
21
  end
21
22
 
22
23
  private
@@ -3,26 +3,19 @@ module Parse
3
3
  attr_reader :keys
4
4
  attr_accessor :parse_class_name, :parse_client
5
5
 
6
- def initialize parse_class_or_name=nil
7
- if parse_class_or_name.is_a?(Class) && parse_class_or_name < Parse::Object
8
- @parse_class = parse_class_or_name
9
- @parse_class_name = parse_class_or_name.parse_class_name
10
- @parse_client = parse_class_or_name.parse_client
11
- else
12
- @parse_class = nil
13
- @parse_class_name = parse_class_or_name.to_s
14
- @parse_client = Parse::Client.default_client
15
- end
6
+ def initialize parse_class_name=nil, parse_client=nil
7
+ @parse_class_name = parse_class_name.to_s
8
+ @parse_client = parse_client || Parse::Client.default
16
9
  @limit = nil
17
10
  @skip = nil
18
11
  @count = false
19
- @where = []
12
+ @where = [] # array of Conditions
20
13
  @order = []
21
14
  @include = []
22
15
  @keys = []
23
16
  end
24
17
 
25
- def invoke &block
18
+ def run &block
26
19
  block = proc do |body|
27
20
  # TODO: should handle error
28
21
  body['results']
@@ -32,7 +25,7 @@ module Parse
32
25
  : "classes/#{@parse_class_name}"
33
26
  @parse_client.call_api :get, "#{endpoint}?#{to_params}", nil, &block
34
27
  end
35
- alias run invoke
28
+ alias invoke run
36
29
 
37
30
  def limit val=nil
38
31
  if val
@@ -143,6 +136,12 @@ module Parse
143
136
  end
144
137
  alias _or_ or_condition
145
138
 
139
+ def related_to column_name, pointer
140
+ RelatedToCondition.new(column_name, pointer).tap do |condition|
141
+ @where.push condition
142
+ end
143
+ end
144
+
146
145
  class Subquery < Query
147
146
  attr_accessor :parent
148
147
 
@@ -283,6 +282,17 @@ module Parse
283
282
  %Q|"$or":[#{@conditions.map {|c| "{#{c.to_s}}"}.join ','}]|
284
283
  end
285
284
  end
285
+
286
+ class RelatedToCondition
287
+ def initialize column_name, pointer
288
+ @column_name = column_name
289
+ @pointer = pointer
290
+ end
291
+
292
+ def to_s
293
+ %Q|"$relatedTo":{"object":#{@pointer.to_json},"key":"#{@column_name}"}|
294
+ end
295
+ end
286
296
  end
287
297
 
288
298
  def Query parse_class_name=nil
@@ -0,0 +1,22 @@
1
+ # coding:utf-8
2
+ module Parse
3
+ class Relation
4
+ def initialize parent, column_name, hash
5
+ @parent_object = parent
6
+ @column_name = column_name
7
+ @raw_hash = hash
8
+ end
9
+
10
+ def load parse_client=Parse::Client.default
11
+ unless @objects
12
+ pointer = @parent_object.to_pointer
13
+ key = @column_name
14
+ related_class = Parse::Object @raw_hash['className']
15
+ @objects = related_class.find :where => proc {
16
+ related_to key, pointer
17
+ }
18
+ end
19
+ @objects
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ # coding:utf-8
2
+ module Parse
3
+ class Role < Object
4
+ # TODO
5
+ end
6
+ end
@@ -1,3 +1,3 @@
1
1
  module Parse
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -13,7 +13,10 @@ require 'parse/client'
13
13
  require 'parse/query'
14
14
  require 'parse/object'
15
15
  require 'parse/user'
16
+ require 'parse/role'
17
+ require 'parse/installation'
16
18
  require 'parse/pointer'
19
+ require 'parse/relation'
17
20
  require 'parse/file'
18
21
 
19
22
  module Parse
@@ -14,7 +14,6 @@ Gem::Specification.new do |gem|
14
14
  gem.license = "MIT"
15
15
 
16
16
  gem.files = `git ls-files`.split($/)
17
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
18
  gem.require_paths = ["lib"]
20
19
 
@@ -108,21 +108,12 @@ describe Parse::Query, 'when it builds conditions' do
108
108
  query.to_params.should have_params('where' => '{"post":{"$notInQuery":{"where":{"image":{"$exists":true}},"className":"Post"}}}')
109
109
  query.where.clear
110
110
 
111
- # query.where do
112
- # #{
113
- # # "$relatedTo":{
114
- # # "object":{
115
- # # "__type":"Pointer",
116
- # # "className":"Post",
117
- # # "objectId":"8TOXdXf3tz"
118
- # # },
119
- # # "key":"likes"
120
- # # }
121
- # #}
122
- # # TODO
123
- # end
124
- # query.to_params.should == 'where={}'
125
- # query.where.clear
111
+ query.where do
112
+ pointer = Parse::Pointer.new('className' => 'Post', 'objectId' => '8TOXdXf3tz')
113
+ related_to :likes, pointer
114
+ end
115
+ query.to_params.should have_params('where' => '{"$relatedTo":{"object":{"__type":"Pointer","className":"Post","objectId":"8TOXdXf3tz"},"key":"likes"}}')
116
+ query.where.clear
126
117
 
127
118
  query.where do
128
119
  column(:wins).gt(150).or column(:wins).lt(5)
@@ -5,7 +5,7 @@ RSpec::Matchers.define :have_params do |expected|
5
5
  match do |actual|
6
6
  expected.to_a.map do |k, v|
7
7
  actual.include? "#{k}=#{URI.encode v.to_s}"
8
- end.inject(true) {|s, v| s || v}
8
+ end.inject(true) {|s, v| s && v}
9
9
  end
10
10
  end
11
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsecom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-10 00:00:00.000000000 Z
12
+ date: 2013-10-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -77,9 +77,12 @@ files:
77
77
  - lib/parse/ext/string.rb
78
78
  - lib/parse/file.rb
79
79
  - lib/parse/http_client.rb
80
+ - lib/parse/installation.rb
80
81
  - lib/parse/object.rb
81
82
  - lib/parse/pointer.rb
82
83
  - lib/parse/query.rb
84
+ - lib/parse/relation.rb
85
+ - lib/parse/role.rb
83
86
  - lib/parse/user.rb
84
87
  - lib/parse/version.rb
85
88
  - lib/parsecom.rb