motion-rails-model 0.0.1 → 0.0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Rakefile +5 -20
- data/app/app_delegate.rb +8 -0
- data/lib/motion-rails-model/model.rb +82 -0
- data/lib/motion-rails-model/version.rb +1 -1
- data/spec/main_spec.rb +9 -0
- metadata +6 -2
data/.gitignore
CHANGED
data/Rakefile
CHANGED
@@ -1,25 +1,10 @@
|
|
1
|
-
|
1
|
+
# -*- coding: utf-8 -*-
|
2
2
|
$:.unshift("/Library/RubyMotion/lib")
|
3
3
|
require 'motion/project'
|
4
|
-
|
4
|
+
require 'rubygems'
|
5
5
|
Bundler.require
|
6
6
|
|
7
|
-
require 'bubble-wrap/all'
|
8
|
-
require 'bubble-wrap/test'
|
9
|
-
|
10
7
|
Motion::Project::App.setup do |app|
|
11
|
-
|
12
|
-
app.
|
13
|
-
|
14
|
-
app.version = '1.2.3'
|
15
|
-
end
|
16
|
-
|
17
|
-
namespace :spec do
|
18
|
-
task :lib do
|
19
|
-
sh "bacon #{Dir.glob("spec/lib/**/*_spec.rb").join(' ')}"
|
20
|
-
end
|
21
|
-
|
22
|
-
task :motion => 'spec'
|
23
|
-
|
24
|
-
task :all => [:lib, :motion]
|
25
|
-
end
|
8
|
+
# Use `rake config' to see complete project settings.
|
9
|
+
app.name = 'modeltest'
|
10
|
+
end
|
data/app/app_delegate.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
class AppDelegate
|
2
|
+
def application(application, didFinishLaunchingWithOptions:launchOptions)
|
3
|
+
@window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
|
4
|
+
@window.rootViewController = UIViewController.alloc.init
|
5
|
+
@window.makeKeyAndVisible
|
6
|
+
true
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module RM
|
2
|
+
class Model
|
3
|
+
attr_accessor :id
|
4
|
+
@@url = ""
|
5
|
+
@@username = ""
|
6
|
+
@@password = ""
|
7
|
+
|
8
|
+
def initialize(json={})
|
9
|
+
attributes_update(json)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.find(id, &block)
|
13
|
+
x = self.new
|
14
|
+
x.fetch("#{x.class.to_s.downcase}/#{id}") do |y|
|
15
|
+
block.call(y)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.all(&block)
|
20
|
+
x = self.new
|
21
|
+
x.fetch("#{x.class.to_s.downcase}") do |y|
|
22
|
+
block.call(y)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def save(url="#{self.class.to_s.downcase}#{('/' + self.id.to_s) if self.id}", &block)
|
27
|
+
type = (self.id.nil?)? "post": "put"
|
28
|
+
data = {"_method" => type}
|
29
|
+
keys = {}
|
30
|
+
self.instance_variables.map do |x|
|
31
|
+
y = x.to_s.sub(/@/, "")
|
32
|
+
z = self.send(y)
|
33
|
+
keys.merge!({ y => z}) unless z.nil?
|
34
|
+
end
|
35
|
+
data.merge!(self.class.to_s.downcase => keys)
|
36
|
+
connect(url, data, type) do |result|
|
37
|
+
block.call(result)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.set_url(url)
|
42
|
+
@@url = url
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.set_username(username)
|
46
|
+
@@username = username
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.set_password(password)
|
50
|
+
@@password = password
|
51
|
+
end
|
52
|
+
|
53
|
+
def fetch(url="#{self.class.to_s.downcase}/#{self.id}", &block)
|
54
|
+
data = { credentials: {username: @@username, password: @@password}}
|
55
|
+
connect(url, data, :get) do |result|
|
56
|
+
block.call(result)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def attributes_update(json)
|
63
|
+
@id = json['id']
|
64
|
+
end
|
65
|
+
|
66
|
+
def connect(url, data, http_verb, &block)
|
67
|
+
BW::HTTP.send(http_verb, "#{@@url}#{url}", data) do |response|
|
68
|
+
if response.ok?
|
69
|
+
json = BW::JSON.parse(response.body.to_str)
|
70
|
+
if json.instance_of?(Array)
|
71
|
+
block.call(json.map {|y| self.class.new(y) })
|
72
|
+
else
|
73
|
+
block.call(self.class.new(json))
|
74
|
+
end
|
75
|
+
else
|
76
|
+
raise response.status_code
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
data/spec/main_spec.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: motion-rails-model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1
|
4
|
+
version: 0.0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -39,9 +39,12 @@ files:
|
|
39
39
|
- LICENSE
|
40
40
|
- README.md
|
41
41
|
- Rakefile
|
42
|
+
- app/app_delegate.rb
|
42
43
|
- lib/motion-rails-model.rb
|
44
|
+
- lib/motion-rails-model/model.rb
|
43
45
|
- lib/motion-rails-model/version.rb
|
44
46
|
- motion-rails-model.gemspec
|
47
|
+
- spec/main_spec.rb
|
45
48
|
homepage: https://github.com/face-do
|
46
49
|
licenses: []
|
47
50
|
post_install_message:
|
@@ -66,5 +69,6 @@ rubygems_version: 1.8.24
|
|
66
69
|
signing_key:
|
67
70
|
specification_version: 3
|
68
71
|
summary: likes rails model
|
69
|
-
test_files:
|
72
|
+
test_files:
|
73
|
+
- spec/main_spec.rb
|
70
74
|
has_rdoc:
|