motion-parse 0.0.1
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.
- data/.gitignore +10 -0
- data/Gemfile +6 -0
- data/README.md +13 -0
- data/Rakefile +14 -0
- data/app/app_delegate.rb +5 -0
- data/lib/motion-parse.rb +3 -0
- data/lib/motion-parse/base.rb +70 -0
- data/lib/motion-parse/user.rb +19 -0
- data/lib/motion-parse/version.rb +3 -0
- data/motion-parse.gemspec +19 -0
- data/spec/base_spec.rb +95 -0
- data/spec/env.rb +60 -0
- data/spec/user_spec.rb +26 -0
- metadata +96 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
$:.unshift("/Library/RubyMotion/lib")
|
3
|
+
require 'motion/project'
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
Bundler.setup
|
6
|
+
Bundler.require
|
7
|
+
|
8
|
+
require 'motion-parse'
|
9
|
+
|
10
|
+
Motion::Project::App.setup do |app|
|
11
|
+
# Use `rake config' to see complete project settings.
|
12
|
+
app.name = 'MotionParse'
|
13
|
+
app.detect_dependencies = false
|
14
|
+
end
|
data/app/app_delegate.rb
ADDED
data/lib/motion-parse.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
module MotionParse
|
2
|
+
class Base
|
3
|
+
attr_accessor :parse_object
|
4
|
+
|
5
|
+
class_attribute :attributes
|
6
|
+
self.attributes = []
|
7
|
+
|
8
|
+
def self.attribute(*fields)
|
9
|
+
fields.each do |field|
|
10
|
+
define_method field do
|
11
|
+
@parse_object.objectForKey(field)
|
12
|
+
end
|
13
|
+
define_method "#{field}=" do |value|
|
14
|
+
@parse_object.setObject(value, forKey:field)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
self.attributes += fields
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(arg = nil)
|
21
|
+
if arg.is_a?(PFObject)
|
22
|
+
@parse_object = arg
|
23
|
+
else
|
24
|
+
@parse_object = PFObject.objectWithClassName(self.class.name)
|
25
|
+
if arg.is_a?(Hash)
|
26
|
+
arg.each do |key, value|
|
27
|
+
@parse_object.setObject(value, forKey:key) if attributes.include?(key)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.find(hash, &block)
|
34
|
+
q = query
|
35
|
+
hash.each do |key, value|
|
36
|
+
q.whereKey(key, equalTo:value)
|
37
|
+
end
|
38
|
+
|
39
|
+
get(q, &block)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.all(&block)
|
43
|
+
find({}, &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.has_many(association)
|
47
|
+
define_method association do |&block|
|
48
|
+
klass = association.to_s.classify.constantize
|
49
|
+
klass.find(self.class.name.foreign_key => parse_object, &block)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
def self.query
|
56
|
+
PFQuery.alloc.initWithClassName(self.name)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.get(query, &block)
|
60
|
+
if block
|
61
|
+
query.findObjectsInBackgroundWithBlock(lambda { |objects, error|
|
62
|
+
objects = objects.map { |obj| new(obj) } if objects
|
63
|
+
block.call(objects, error)
|
64
|
+
})
|
65
|
+
else
|
66
|
+
query.findObjects.map { |obj| new(obj) }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module MotionParse
|
2
|
+
class User < Base
|
3
|
+
attribute :username, :password, :email
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
super(PFUser.user)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.current
|
10
|
+
if PFUser.currentUser
|
11
|
+
new.tap do |u|
|
12
|
+
u.parse_object = PFUser.currentUser
|
13
|
+
end
|
14
|
+
else
|
15
|
+
return nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/motion-parse/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "motion-parse"
|
6
|
+
s.version = MotionParse::VERSION
|
7
|
+
s.authors = ["Thomas Kadauke"]
|
8
|
+
s.email = ["thomas.kadauke@googlemail.com"]
|
9
|
+
s.homepage = "https://github.com/tkadauke/motion-parse"
|
10
|
+
s.summary = "Access Parse.com data from your iOS app"
|
11
|
+
s.description = "Access Parse.com data from your iOS app"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split($\)
|
14
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
|
17
|
+
s.add_dependency 'motion-support', ">= 0.2.0"
|
18
|
+
s.add_development_dependency 'rake'
|
19
|
+
end
|
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
describe "Base" do
|
2
|
+
describe "attributes" do
|
3
|
+
it "should store attributes in class array" do
|
4
|
+
Author.attributes.should == [:first_name, :last_name, :age]
|
5
|
+
end
|
6
|
+
|
7
|
+
it "should generate reader methods" do
|
8
|
+
Author.new.should.respond_to :first_name
|
9
|
+
Author.new.should.respond_to :last_name
|
10
|
+
Author.new.should.respond_to :age
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should generate writer methods" do
|
14
|
+
Author.new.should.respond_to :first_name=
|
15
|
+
Author.new.should.respond_to :last_name=
|
16
|
+
Author.new.should.respond_to :age=
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return nil for unset attribute" do
|
20
|
+
Author.new.first_name.should.be.nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should set and get attribute" do
|
24
|
+
author = Author.new
|
25
|
+
author.first_name = "John"
|
26
|
+
author.first_name.should == "John"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe "initialize" do
|
31
|
+
it "should accept parse object" do
|
32
|
+
obj = PFObject.new
|
33
|
+
Author.new(obj).parse_object.should == obj
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should accept hash and generate parse object" do
|
37
|
+
Author.new(:first_name => 'John').parse_object.should.not.be.nil
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should accept hash and store values" do
|
41
|
+
Author.new(:first_name => 'John').first_name.should == 'John'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should accept hash with unknown key and generate parse object" do
|
45
|
+
Author.new(:foo => 'Bar').parse_object.should.not.be.nil
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should accept no arg and generage parse object" do
|
49
|
+
Author.new.parse_object.should.not.be.nil
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "find" do
|
54
|
+
it "should find with constraints" do
|
55
|
+
Author.find(:foo => 'bar', :baz => 'boom')
|
56
|
+
PFQuery.last_object.constraints.should == { :foo => 'bar', :baz => 'boom' }
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should call block with results when given" do
|
60
|
+
PFQuery.result_objects = [1, 2, 3]
|
61
|
+
@result = nil
|
62
|
+
Author.find(:foo => 'bar') do |objects|
|
63
|
+
@result = objects
|
64
|
+
end
|
65
|
+
@result.size.should == 3
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should return models immediately when no block given" do
|
69
|
+
PFQuery.result_objects = [1, 2, 3]
|
70
|
+
Author.find(:foo => 'bar').size.should == 3
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe "has_many" do
|
75
|
+
it "should constrain with association" do
|
76
|
+
author = Author.new
|
77
|
+
author.posts
|
78
|
+
PFQuery.last_object.constraints.should == { 'author_id' => author.parse_object }
|
79
|
+
end
|
80
|
+
|
81
|
+
it "should call block with results when given" do
|
82
|
+
PFQuery.result_objects = [1, 2, 3]
|
83
|
+
@result = nil
|
84
|
+
Author.new.posts do |objects|
|
85
|
+
@result = objects
|
86
|
+
end
|
87
|
+
@result.size.should == 3
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should return models immediately when no block given" do
|
91
|
+
PFQuery.result_objects = [1, 2, 3]
|
92
|
+
Author.new.posts.size.should == 3
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/spec/env.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
class PFObject
|
2
|
+
attr_reader :fields
|
3
|
+
|
4
|
+
def self.objectWithClassName(name)
|
5
|
+
new
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@fields = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def objectForKey(field)
|
13
|
+
@fields[field]
|
14
|
+
end
|
15
|
+
|
16
|
+
def setObject(value, forKey:key)
|
17
|
+
@fields[key] = value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class PFQuery
|
22
|
+
attr_reader :constraints
|
23
|
+
cattr_accessor :last_object
|
24
|
+
cattr_accessor :result_objects
|
25
|
+
|
26
|
+
def initWithClassName(name)
|
27
|
+
@constraints = {}
|
28
|
+
PFQuery.last_object = self
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def whereKey(key, equalTo:value)
|
33
|
+
@constraints[key] = value
|
34
|
+
end
|
35
|
+
|
36
|
+
def findObjectsInBackgroundWithBlock(block)
|
37
|
+
block.call(result_objects || [], nil)
|
38
|
+
end
|
39
|
+
|
40
|
+
def findObjects
|
41
|
+
result_objects || []
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class PFUser
|
46
|
+
cattr_accessor :currentUser
|
47
|
+
def self.user
|
48
|
+
new
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Author < MotionParse::Base
|
53
|
+
attribute :first_name, :last_name, :age
|
54
|
+
|
55
|
+
has_many :posts
|
56
|
+
end
|
57
|
+
|
58
|
+
class Post < MotionParse::Base
|
59
|
+
attribute :title, :text
|
60
|
+
end
|
data/spec/user_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
describe "User" do
|
2
|
+
describe "attributes" do
|
3
|
+
[:username, :password, :email].each do |attr|
|
4
|
+
it "should have #{attr} attribute" do
|
5
|
+
MotionParse::User.new.should.respond_to attr
|
6
|
+
MotionParse::User.new.should.respond_to "#{attr}="
|
7
|
+
MotionParse::User.attributes.should.include attr
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "current" do
|
13
|
+
it "should return user model with current user if set" do
|
14
|
+
PFUser.currentUser = Object.new
|
15
|
+
|
16
|
+
MotionParse::User.current.should.is_a MotionParse::User
|
17
|
+
MotionParse::User.current.parse_object.should == PFUser.currentUser
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return nil if no current user is set" do
|
21
|
+
PFUser.currentUser = nil
|
22
|
+
|
23
|
+
MotionParse::User.current.should.be.nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: motion-parse
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Thomas Kadauke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2013-05-02 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: motion-support
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.2.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rake
|
28
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id002
|
37
|
+
description: Access Parse.com data from your iOS app
|
38
|
+
email:
|
39
|
+
- thomas.kadauke@googlemail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files: []
|
45
|
+
|
46
|
+
files:
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- app/app_delegate.rb
|
52
|
+
- lib/motion-parse.rb
|
53
|
+
- lib/motion-parse/base.rb
|
54
|
+
- lib/motion-parse/user.rb
|
55
|
+
- lib/motion-parse/version.rb
|
56
|
+
- motion-parse.gemspec
|
57
|
+
- spec/base_spec.rb
|
58
|
+
- spec/env.rb
|
59
|
+
- spec/user_spec.rb
|
60
|
+
homepage: https://github.com/tkadauke/motion-parse
|
61
|
+
licenses: []
|
62
|
+
|
63
|
+
post_install_message:
|
64
|
+
rdoc_options: []
|
65
|
+
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: -2030954612045618758
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: -2030954612045618758
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.19
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Access Parse.com data from your iOS app
|
93
|
+
test_files:
|
94
|
+
- spec/base_spec.rb
|
95
|
+
- spec/env.rb
|
96
|
+
- spec/user_spec.rb
|