arrest 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arrest/abstract_resource.rb +73 -11
- data/lib/arrest/version.rb +1 -1
- data/test/unit.rb +23 -6
- metadata +6 -6
@@ -1,9 +1,61 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'arrest/string_utils'
|
3
|
+
require 'time'
|
4
|
+
class Boolean
|
5
|
+
|
6
|
+
end
|
3
7
|
|
4
8
|
module Arrest
|
5
9
|
|
6
|
-
Attribute = Struct.new(:name, :read_only)
|
10
|
+
Attribute = Struct.new(:name, :read_only, :clazz)
|
11
|
+
CONVERTER = {}
|
12
|
+
|
13
|
+
class Converter
|
14
|
+
class << self
|
15
|
+
attr_reader :clazz
|
16
|
+
|
17
|
+
def convert value
|
18
|
+
if value.is_a?(self.clazz)
|
19
|
+
value
|
20
|
+
else
|
21
|
+
self.parse value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def target clazz
|
26
|
+
@clazz = clazz
|
27
|
+
CONVERTER[clazz] = self
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class IdentConv < Converter
|
33
|
+
def self.convert value
|
34
|
+
value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
class StringConv < IdentConv
|
39
|
+
target String
|
40
|
+
end
|
41
|
+
|
42
|
+
class BooleanConv < IdentConv
|
43
|
+
target Boolean
|
44
|
+
end
|
45
|
+
|
46
|
+
class IntegerConv < IdentConv
|
47
|
+
target Integer
|
48
|
+
end
|
49
|
+
|
50
|
+
class TimeConv < Converter
|
51
|
+
target Time
|
52
|
+
|
53
|
+
def self.parse value
|
54
|
+
Time.parse(value)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
7
59
|
|
8
60
|
class AbstractResource
|
9
61
|
class << self
|
@@ -73,24 +125,24 @@ module Arrest
|
|
73
125
|
|
74
126
|
end
|
75
127
|
|
76
|
-
def read_only_attributes(
|
77
|
-
args.
|
78
|
-
self.send :attr_accessor,
|
79
|
-
add_attribute Attribute.new(
|
128
|
+
def read_only_attributes(args)
|
129
|
+
args.each_pair do |name, clazz|
|
130
|
+
self.send :attr_accessor,name
|
131
|
+
add_attribute Attribute.new(name, true, clazz)
|
80
132
|
end
|
81
133
|
end
|
82
134
|
|
83
|
-
def attributes(
|
84
|
-
args.
|
85
|
-
self.send :attr_accessor,
|
86
|
-
add_attribute Attribute.new(
|
135
|
+
def attributes(args)
|
136
|
+
args.each_pair do |name, clazz|
|
137
|
+
self.send :attr_accessor,name
|
138
|
+
add_attribute Attribute.new(name, false, clazz)
|
87
139
|
end
|
88
140
|
end
|
89
141
|
|
90
142
|
def belongs_to(*args)
|
91
143
|
arg = args[0]
|
92
144
|
name = arg.to_s.downcase
|
93
|
-
attributes
|
145
|
+
attributes({"#{name}_id".to_sym => String})
|
94
146
|
send :define_method, name do
|
95
147
|
val = self.instance_variable_get("@#{name}_id")
|
96
148
|
Arrest::Source.mod.const_get(StringUtils.classify name).find(val)
|
@@ -107,7 +159,17 @@ module Arrest
|
|
107
159
|
end
|
108
160
|
unless self.class.all_fields == nil
|
109
161
|
self.class.all_fields.each do |field|
|
110
|
-
|
162
|
+
value = as[field.name.to_sym]
|
163
|
+
if value
|
164
|
+
converter = CONVERTER[field.clazz]
|
165
|
+
if converter == nil
|
166
|
+
puts "No converter for: #{field.clazz.name}"
|
167
|
+
converter = IdentConv
|
168
|
+
end
|
169
|
+
else
|
170
|
+
converter = IdentConv
|
171
|
+
end
|
172
|
+
self.send("#{field.name.to_s}=", converter.convert(value))
|
111
173
|
end
|
112
174
|
end
|
113
175
|
self.id = as[:id]
|
data/lib/arrest/version.rb
CHANGED
data/test/unit.rb
CHANGED
@@ -2,19 +2,26 @@ require 'arrest'
|
|
2
2
|
require 'test/unit'
|
3
3
|
|
4
4
|
class Zoo < Arrest::RootResource
|
5
|
-
attributes :name
|
6
|
-
read_only_attributes :ro1
|
5
|
+
attributes({ :name => String })
|
6
|
+
read_only_attributes({ :ro1 => String})
|
7
7
|
has_many :animals
|
8
8
|
end
|
9
9
|
|
10
10
|
class Animal < Arrest::RestChild
|
11
|
-
attributes
|
11
|
+
attributes({
|
12
|
+
:kind => String,
|
13
|
+
:age => Integer
|
14
|
+
})
|
12
15
|
parent :zoo
|
13
16
|
end
|
14
17
|
|
15
18
|
class SpecialZoo < Zoo
|
16
|
-
read_only_attributes :ro2
|
17
|
-
attributes
|
19
|
+
read_only_attributes({ :ro2 => String})
|
20
|
+
attributes({
|
21
|
+
:is_magic => Boolean,
|
22
|
+
:opened_at => Time
|
23
|
+
})
|
24
|
+
|
18
25
|
end
|
19
26
|
|
20
27
|
class FirstTest < Test::Unit::TestCase
|
@@ -169,7 +176,14 @@ class FirstTest < Test::Unit::TestCase
|
|
169
176
|
end
|
170
177
|
|
171
178
|
def test_read_only_attributes
|
172
|
-
|
179
|
+
now = Time.now
|
180
|
+
zoo = SpecialZoo.new({
|
181
|
+
:name => "Zoo",
|
182
|
+
:ro1 => "one",
|
183
|
+
:ro2 => "two",
|
184
|
+
:is_magic => true,
|
185
|
+
:opened_at => now
|
186
|
+
})
|
173
187
|
|
174
188
|
assert_equal "Zoo", zoo.name
|
175
189
|
assert_equal "one", zoo.ro1
|
@@ -180,6 +194,9 @@ class FirstTest < Test::Unit::TestCase
|
|
180
194
|
|
181
195
|
assert_nil hash[:ro1]
|
182
196
|
assert_nil hash[:ro2]
|
197
|
+
assert_equal Time, zoo.opened_at.class
|
198
|
+
assert_equal now, zoo.opened_at
|
199
|
+
|
183
200
|
end
|
184
201
|
end
|
185
202
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arrest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-10 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &2154642500 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2154642500
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: faraday
|
27
|
-
requirement: &
|
27
|
+
requirement: &2154641400 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - =
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: 0.7.5
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2154641400
|
36
36
|
description: Consume a rest API in a AR like fashion
|
37
37
|
email:
|
38
38
|
- axel.tetzlaff@fortytools.com
|