arrest 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arrest.rb +1 -1
- data/lib/arrest/abstract_resource.rb +4 -44
- data/lib/arrest/attributes/converter.rb +24 -7
- data/lib/arrest/attributes/has_attributes.rb +80 -61
- data/lib/arrest/helper/child_collection.rb +6 -7
- data/lib/arrest/http_source.rb +1 -1
- data/lib/arrest/mem_source.rb +10 -14
- data/lib/arrest/nested_resource.rb +0 -16
- data/lib/arrest/root_resource.rb +11 -13
- data/lib/arrest/source.rb +17 -1
- data/lib/arrest/version.rb +1 -1
- data/test/has_attributed.rb +68 -0
- data/test/models.rb +1 -0
- data/test/nested_resource.rb +7 -41
- data/test/unit.rb +26 -11
- metadata +22 -20
data/lib/arrest.rb
CHANGED
@@ -8,11 +8,11 @@ require "arrest/helper/child_collection"
|
|
8
8
|
require "arrest/exceptions"
|
9
9
|
require "arrest/http_source"
|
10
10
|
require "arrest/mem_source"
|
11
|
+
require "arrest/source"
|
11
12
|
require "arrest/nested_resource"
|
12
13
|
require "arrest/abstract_resource"
|
13
14
|
require "arrest/root_resource"
|
14
15
|
require "arrest/rest_child"
|
15
16
|
|
16
17
|
module Arrest
|
17
|
-
|
18
18
|
end
|
@@ -7,10 +7,9 @@ Scope = Struct.new(:name, :block)
|
|
7
7
|
module Arrest
|
8
8
|
class AbstractResource
|
9
9
|
include HasAttributes
|
10
|
+
attribute :id, String
|
10
11
|
class << self
|
11
|
-
|
12
12
|
attr_reader :scopes
|
13
|
-
|
14
13
|
|
15
14
|
def source
|
16
15
|
Arrest::Source::source
|
@@ -29,11 +28,7 @@ module Arrest
|
|
29
28
|
end
|
30
29
|
|
31
30
|
def build hash
|
32
|
-
|
33
|
-
hash.each_pair do |k, v|
|
34
|
-
underscored_hash[StringUtils.underscore k] = v
|
35
|
-
end
|
36
|
-
self.new underscored_hash
|
31
|
+
self.new hash, true
|
37
32
|
end
|
38
33
|
|
39
34
|
def custom_resource_name new_name
|
@@ -97,42 +92,9 @@ module Arrest
|
|
97
92
|
include BelongsTo
|
98
93
|
|
99
94
|
attr_accessor :id
|
100
|
-
attr_reader :stub
|
101
|
-
|
102
|
-
def self.stub id
|
103
|
-
self.new({:id => id}, true)
|
104
|
-
end
|
105
|
-
|
106
|
-
def initialize hash={},stubbed=false
|
107
|
-
@stub = stubbed
|
108
|
-
init_from_hash(hash) unless stubbed
|
109
|
-
self.id = hash[:id]
|
110
|
-
self.id ||= hash['id']
|
111
|
-
end
|
112
|
-
|
113
|
-
def init_from_hash as_i={}
|
114
|
-
@stub = false
|
115
|
-
super
|
116
|
-
end
|
117
|
-
|
118
|
-
|
119
|
-
def to_hash
|
120
|
-
result = {}
|
121
|
-
unless self.class.all_fields == nil
|
122
|
-
self.class.all_fields.find_all{|a| !a.read_only}.each do |field|
|
123
|
-
json_name = StringUtils.classify(field.name.to_s,false)
|
124
|
-
val = self.instance_variable_get("@#{field.name.to_s}")
|
125
|
-
if val != nil && val.is_a?(NestedResource)
|
126
|
-
val = val.to_hash
|
127
|
-
elsif val != nil && field.is_a?(NestedCollection)
|
128
|
-
val = val.map {|v| v.to_hash}
|
129
|
-
end
|
130
|
-
result[json_name] = val
|
131
|
-
end
|
132
|
-
end
|
133
|
-
result[:id] = self.id
|
134
|
-
result
|
135
95
|
|
96
|
+
def initialize hash={}, from_json = false
|
97
|
+
initialize_has_attributes hash, from_json
|
136
98
|
end
|
137
99
|
|
138
100
|
def save
|
@@ -158,7 +120,5 @@ module Arrest
|
|
158
120
|
|
159
121
|
"curl #{hs} -v '#{Arrest::Source.source.url}/#{self.resource_location}'"
|
160
122
|
end
|
161
|
-
|
162
|
-
|
163
123
|
end
|
164
124
|
end
|
@@ -5,14 +5,15 @@ end
|
|
5
5
|
module Arrest
|
6
6
|
|
7
7
|
class Attribute
|
8
|
-
attr_accessor :name, :read_only, :clazz
|
8
|
+
attr_accessor :name, :read_only, :clazz, :json_name
|
9
9
|
def initialize name, read_only, clazz
|
10
|
-
@name = name
|
10
|
+
@name = name.to_sym
|
11
11
|
@read_only = read_only
|
12
12
|
@clazz = clazz
|
13
|
+
@json_name = Source.json_key_converter.key_to_json(name).to_sym
|
13
14
|
end
|
14
15
|
|
15
|
-
def
|
16
|
+
def from_hash value
|
16
17
|
return if value == nil
|
17
18
|
converter = CONVERTER[@clazz]
|
18
19
|
if converter == nil
|
@@ -21,6 +22,11 @@ module Arrest
|
|
21
22
|
end
|
22
23
|
converter.convert value
|
23
24
|
end
|
25
|
+
|
26
|
+
def to_hash value
|
27
|
+
return nil unless value != nil
|
28
|
+
value
|
29
|
+
end
|
24
30
|
end
|
25
31
|
|
26
32
|
class NestedAttribute < Attribute
|
@@ -28,10 +34,15 @@ module Arrest
|
|
28
34
|
super name, read_only, clazz
|
29
35
|
end
|
30
36
|
|
31
|
-
def
|
32
|
-
return unless value
|
37
|
+
def from_hash value
|
38
|
+
return nil unless value != nil
|
33
39
|
@clazz.new value
|
34
40
|
end
|
41
|
+
|
42
|
+
def to_hash val
|
43
|
+
return nil unless val!= nil
|
44
|
+
val.to_hash
|
45
|
+
end
|
35
46
|
end
|
36
47
|
|
37
48
|
class NestedCollection < Attribute
|
@@ -39,14 +50,20 @@ module Arrest
|
|
39
50
|
super name, read_only, clazz
|
40
51
|
end
|
41
52
|
|
42
|
-
def
|
43
|
-
return unless value
|
53
|
+
def from_hash value
|
54
|
+
return nil unless value != nil
|
44
55
|
raise "Expected an array but got #{value.class.name}" unless value.is_a?(Array)
|
45
56
|
value.map do |v|
|
46
57
|
@clazz.new v
|
47
58
|
end
|
48
59
|
end
|
49
60
|
|
61
|
+
def to_hash value
|
62
|
+
return nil unless value != nil
|
63
|
+
raise "Expected an array but got #{value.class.name}" unless value.is_a?(Array)
|
64
|
+
value.map(&:to_hash)
|
65
|
+
end
|
66
|
+
|
50
67
|
end
|
51
68
|
|
52
69
|
CONVERTER = {}
|
@@ -1,58 +1,86 @@
|
|
1
|
+
require "arrest/source"
|
1
2
|
module Arrest
|
2
3
|
module HasAttributes
|
3
|
-
|
4
|
-
|
5
|
-
|
4
|
+
attr_accessor :attribute_values
|
5
|
+
|
6
|
+
def initialize_has_attributes hash, from_json = false, &blk
|
7
|
+
if block_given?
|
8
|
+
@stubbed = true
|
9
|
+
@load_blk = blk
|
10
|
+
else
|
11
|
+
@stubbed = false
|
12
|
+
end
|
13
|
+
init_from_hash hash, from_json
|
6
14
|
end
|
7
15
|
|
8
|
-
def
|
16
|
+
def initialize hash = {}, from_json = false, &blk
|
17
|
+
if block_given?
|
18
|
+
@stubbed = true
|
19
|
+
@load_blk = blk
|
20
|
+
else
|
21
|
+
init_from_hash hash, from_json
|
22
|
+
end
|
23
|
+
end
|
9
24
|
|
25
|
+
def self.included(base) # :nodoc:
|
26
|
+
base.extend HasAttributesMethods
|
10
27
|
end
|
11
28
|
|
12
|
-
def init_from_hash as_i={}
|
29
|
+
def init_from_hash as_i={}, from_json = false
|
30
|
+
@attribute_values = {} unless @attribute_values != nil
|
13
31
|
as = {}
|
14
32
|
as_i.each_pair do |k,v|
|
15
33
|
as[k.to_sym] = v
|
16
34
|
end
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
35
|
+
self.class.all_fields.each do |field|
|
36
|
+
if from_json
|
37
|
+
key = field.json_name
|
38
|
+
else
|
39
|
+
key = field.name
|
22
40
|
end
|
41
|
+
value = as[key]
|
42
|
+
converted = field.from_hash(value)
|
43
|
+
self.send(field.name.to_s + '=', converted) unless converted == nil
|
23
44
|
end
|
24
45
|
end
|
25
46
|
|
26
|
-
def
|
47
|
+
def load_from_stub
|
48
|
+
@load_blk.call
|
49
|
+
@stubbed = false
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def to_jhash
|
54
|
+
to_hash(false, true)
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_hash(show_all_fields = true, json_names = false)
|
27
58
|
result = {}
|
28
|
-
|
29
|
-
|
30
|
-
json_name =
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
59
|
+
self.class.all_fields.find_all{|a| show_all_fields || !a.read_only}.each do |field|
|
60
|
+
if json_names
|
61
|
+
json_name = field.json_name
|
62
|
+
else
|
63
|
+
json_name = field.name
|
64
|
+
end
|
65
|
+
val = self.send(field.name)
|
66
|
+
converted = field.to_hash val
|
67
|
+
if converted != nil
|
68
|
+
result[json_name] = converted
|
36
69
|
end
|
37
70
|
end
|
38
71
|
result
|
39
72
|
end
|
40
73
|
|
41
74
|
module HasAttributesMethods
|
75
|
+
|
42
76
|
attr_accessor :fields
|
43
77
|
|
44
|
-
|
78
|
+
def initialize
|
79
|
+
@fields = []
|
80
|
+
end
|
81
|
+
|
45
82
|
def attribute name, clazz
|
46
|
-
|
47
|
-
|
48
|
-
send :define_method, "#{name}=" do |v|
|
49
|
-
self.unstub
|
50
|
-
self.instance_variable_set("@#{name}", v)
|
51
|
-
end
|
52
|
-
send :define_method, "#{name}" do
|
53
|
-
self.unstub
|
54
|
-
self.instance_variable_get("@#{name}")
|
55
|
-
end
|
83
|
+
add_attribute Attribute.new(name, false, clazz)
|
56
84
|
end
|
57
85
|
|
58
86
|
def attributes(args)
|
@@ -62,52 +90,43 @@ module Arrest
|
|
62
90
|
end
|
63
91
|
|
64
92
|
def add_attribute attribute
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
93
|
+
if @fields == nil
|
94
|
+
@fields = []
|
95
|
+
end
|
96
|
+
send :define_method, "#{attribute.name}=" do |v|
|
97
|
+
Arrest::debug "setter #{self.class.name} #{attribute.name} = #{v}"
|
98
|
+
self.attribute_values[attribute.name] = v
|
99
|
+
end
|
100
|
+
send :define_method, "#{attribute.name}" do
|
101
|
+
Arrest::debug "getter #{self.class.name} #{attribute.name}"
|
102
|
+
self.load_from_stub if @stubbed
|
103
|
+
self.attribute_values[attribute.name]
|
104
|
+
end
|
105
|
+
@fields << attribute
|
69
106
|
end
|
70
107
|
|
71
108
|
def all_fields
|
72
109
|
self_fields = self.fields
|
73
110
|
self_fields ||= []
|
74
|
-
if self.superclass.respond_to?('fields') && self.superclass.
|
75
|
-
self_fields + self.superclass.
|
111
|
+
if self.superclass.respond_to?('fields') && self.superclass.all_fields != nil
|
112
|
+
res = self_fields + self.superclass.all_fields
|
76
113
|
else
|
77
|
-
self_fields
|
114
|
+
res = self_fields
|
78
115
|
end
|
116
|
+
res
|
79
117
|
end
|
80
118
|
|
81
119
|
def nested name, clazz
|
82
|
-
|
83
|
-
|
84
|
-
send :define_method, "#{name}=" do |v|
|
85
|
-
self.unstub
|
86
|
-
self.instance_variable_set("@#{name}", v)
|
87
|
-
end
|
88
|
-
send :define_method, "#{name}" do
|
89
|
-
self.unstub
|
90
|
-
self.instance_variable_get("@#{name}")
|
91
|
-
end
|
92
|
-
|
120
|
+
add_attribute NestedAttribute.new(name, false, clazz)
|
93
121
|
end
|
94
122
|
|
95
123
|
def nested_array name, clazz
|
96
|
-
|
97
|
-
|
98
|
-
send :define_method, "#{name}=" do |v|
|
99
|
-
self.unstub
|
100
|
-
self.instance_variable_set("@#{name}", v)
|
101
|
-
end
|
102
|
-
send :define_method, "#{name}" do
|
103
|
-
self.unstub
|
104
|
-
self.instance_variable_get("@#{name}")
|
105
|
-
end
|
106
|
-
|
124
|
+
add_attribute NestedCollection.new(name, false, clazz)
|
107
125
|
end
|
108
|
-
|
109
126
|
end
|
110
127
|
|
111
|
-
|
128
|
+
def stubbed?
|
129
|
+
@stubbed
|
130
|
+
end
|
112
131
|
end
|
113
132
|
end
|
@@ -1,21 +1,20 @@
|
|
1
1
|
module Arrest
|
2
|
-
class ChildCollection
|
3
|
-
def initialize
|
4
|
-
@
|
2
|
+
class ChildCollection < BasicObject
|
3
|
+
def initialize parent, clazz_name
|
4
|
+
@parent = parent
|
5
5
|
@clazz_name = clazz_name
|
6
6
|
@children = nil
|
7
7
|
end
|
8
8
|
|
9
9
|
def build attributes = {}
|
10
|
-
resolved_class.new @
|
10
|
+
resolved_class.new @parent, attributes
|
11
11
|
end
|
12
12
|
|
13
13
|
def method_missing(*args, &block)
|
14
14
|
if resolved_class.respond_to?(args[0])
|
15
|
-
resolved_class.send(args[0], @
|
15
|
+
resolved_class.send(args[0], @parent)
|
16
16
|
else
|
17
17
|
children.send(*args, &block)
|
18
|
-
|
19
18
|
end
|
20
19
|
end
|
21
20
|
|
@@ -24,7 +23,7 @@ module Arrest
|
|
24
23
|
|
25
24
|
def children
|
26
25
|
if @children == nil
|
27
|
-
@children = resolved_class.all_for @
|
26
|
+
@children = resolved_class.all_for @parent
|
28
27
|
end
|
29
28
|
@children
|
30
29
|
end
|
data/lib/arrest/http_source.rb
CHANGED
@@ -58,7 +58,7 @@ module Arrest
|
|
58
58
|
def post rest_resource
|
59
59
|
raise "new object must have setter for id" unless rest_resource.respond_to?(:id=)
|
60
60
|
raise "new object must not have id" if rest_resource.respond_to?(:id) && rest_resource.id != nil
|
61
|
-
hash = rest_resource.
|
61
|
+
hash = rest_resource.to_jhash
|
62
62
|
hash.delete(:id)
|
63
63
|
hash.delete('id')
|
64
64
|
|
data/lib/arrest/mem_source.rb
CHANGED
@@ -25,24 +25,19 @@ module Arrest
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def initialize
|
28
|
-
|
28
|
+
@@all_objects = {} # holds all objects of all types,
|
29
29
|
|
30
|
-
|
31
|
-
|
30
|
+
@@collections = {} # maps urls to collections of ids of objects
|
31
|
+
@@random = Random.new(42)
|
32
32
|
|
33
33
|
end
|
34
34
|
|
35
|
-
def debug s
|
36
|
-
if Arrest::Source.debug
|
37
|
-
puts s
|
38
|
-
end
|
39
|
-
end
|
40
35
|
|
41
36
|
# only to stub collection for development
|
42
37
|
#
|
43
38
|
def set_collection clazz, scope, objects
|
44
39
|
url = clazz.scoped_path scope
|
45
|
-
|
40
|
+
Arrest::debug "url:#{url}"
|
46
41
|
@@data[url] = objects
|
47
42
|
end
|
48
43
|
|
@@ -67,7 +62,7 @@ module Arrest
|
|
67
62
|
end
|
68
63
|
|
69
64
|
def get_many sub, filters = {}
|
70
|
-
debug sub + (hash_to_query filters)
|
65
|
+
Arrest::debug sub + (hash_to_query filters)
|
71
66
|
# filters are ignored by mem impl so far
|
72
67
|
|
73
68
|
id_list = @@collections[sub] || []
|
@@ -80,7 +75,7 @@ module Arrest
|
|
80
75
|
end
|
81
76
|
|
82
77
|
def get_one sub, filters = {}
|
83
|
-
debug sub + (hash_to_query filters)
|
78
|
+
Arrest::debug sub + (hash_to_query filters)
|
84
79
|
# filters are ignored by mem impl so far
|
85
80
|
idx = sub.rindex '/'
|
86
81
|
if idx
|
@@ -90,12 +85,12 @@ module Arrest
|
|
90
85
|
if val == nil
|
91
86
|
raise Errors::DocumentNotFoundError
|
92
87
|
end
|
93
|
-
wrap val.
|
88
|
+
wrap val.to_jhash.to_json, 1
|
94
89
|
end
|
95
90
|
|
96
91
|
def collection_json values
|
97
92
|
single_jsons = values.map do |v|
|
98
|
-
v.
|
93
|
+
v.to_jhash.to_json
|
99
94
|
end
|
100
95
|
"[#{single_jsons.join(',')}]"
|
101
96
|
end
|
@@ -133,6 +128,7 @@ module Arrest
|
|
133
128
|
end
|
134
129
|
|
135
130
|
def post rest_resource
|
131
|
+
Arrest::debug "post -> #{rest_resource.class.name} #{rest_resource.to_hash} #{rest_resource.class.all_fields.map(&:name)}"
|
136
132
|
raise "new object must have setter for id" unless rest_resource.respond_to?(:id=)
|
137
133
|
raise "new object must not have id" if rest_resource.respond_to?(:id) && rest_resource.id != nil
|
138
134
|
rest_resource.id = next_id
|
@@ -140,7 +136,7 @@ module Arrest
|
|
140
136
|
unless @@data[rest_resource.resource_path()] != nil
|
141
137
|
@@data[rest_resource.resource_path()] = {}
|
142
138
|
end
|
143
|
-
debug "child path #{rest_resource.resource_path()}"
|
139
|
+
Arrest::debug "child path #{rest_resource.resource_path()}"
|
144
140
|
@@data[rest_resource.resource_path()][next_id.to_s] = rest_resource.id
|
145
141
|
if @@collections[rest_resource.resource_path] == nil
|
146
142
|
@@collections[rest_resource.resource_path] = []
|
@@ -9,21 +9,5 @@ module Arrest
|
|
9
9
|
def initialize h
|
10
10
|
init_from_hash h
|
11
11
|
end
|
12
|
-
|
13
|
-
class << self
|
14
|
-
def to_hash
|
15
|
-
result = {}
|
16
|
-
unless self.class.all_fields == nil
|
17
|
-
self.class.all_fields.find_all{|a| !a.read_only}.each do |field|
|
18
|
-
json_name = StringUtils.classify(field.name.to_s,false)
|
19
|
-
result[json_name] = self.instance_variable_get("@#{field.name.to_s}")
|
20
|
-
end
|
21
|
-
end
|
22
|
-
result[:id] = self.id
|
23
|
-
result
|
24
|
-
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|
28
12
|
end
|
29
13
|
end
|
data/lib/arrest/root_resource.rb
CHANGED
@@ -21,7 +21,7 @@ module Arrest
|
|
21
21
|
if body == nil || body.empty?
|
22
22
|
raise Errors::DocumentNotFoundError.new
|
23
23
|
end
|
24
|
-
self.build body
|
24
|
+
self.build body.merge({:id => id})
|
25
25
|
end
|
26
26
|
|
27
27
|
def scope name, &block
|
@@ -43,6 +43,16 @@ module Arrest
|
|
43
43
|
def scoped_path scope_name
|
44
44
|
resource_path + '/' + scope_name.to_s
|
45
45
|
end
|
46
|
+
|
47
|
+
def stub stub_id
|
48
|
+
n = self.new
|
49
|
+
n.initialize_has_attributes({:id => stub_id}) do
|
50
|
+
r = n.class.source().get_one "#{self.resource_path}/#{stub_id}"
|
51
|
+
body = n.class.body_root(r)
|
52
|
+
n.init_from_hash(body, true)
|
53
|
+
end
|
54
|
+
n
|
55
|
+
end
|
46
56
|
end
|
47
57
|
|
48
58
|
def resource_path
|
@@ -53,18 +63,6 @@ module Arrest
|
|
53
63
|
self.class.resource_path + '/' + self.id.to_s
|
54
64
|
end
|
55
65
|
|
56
|
-
|
57
|
-
def unstub
|
58
|
-
return unless @stub
|
59
|
-
r = self.class.source().get_one "#{self.resource_path}/#{id}"
|
60
|
-
body = self.class.body_root(r)
|
61
|
-
underscored_hash = {}
|
62
|
-
body.each_pair do |k, v|
|
63
|
-
underscored_hash[StringUtils.underscore k] = v
|
64
|
-
end
|
65
|
-
init_from_hash underscored_hash
|
66
|
-
end
|
67
|
-
|
68
66
|
end
|
69
67
|
end
|
70
68
|
|
data/lib/arrest/source.rb
CHANGED
@@ -1,4 +1,9 @@
|
|
1
1
|
module Arrest
|
2
|
+
def self.debug s
|
3
|
+
if Arrest::Source.debug
|
4
|
+
puts s
|
5
|
+
end
|
6
|
+
end
|
2
7
|
|
3
8
|
class Source
|
4
9
|
class << self
|
@@ -6,6 +11,7 @@ module Arrest
|
|
6
11
|
attr_reader :source
|
7
12
|
attr_reader :mod
|
8
13
|
attr_reader :header_decorator
|
14
|
+
attr_accessor :json_key_converter
|
9
15
|
|
10
16
|
def source=(host=nil)
|
11
17
|
if host == nil || host.blank?
|
@@ -27,7 +33,7 @@ module Arrest
|
|
27
33
|
end
|
28
34
|
|
29
35
|
def header_decorator=(hd=nil)
|
30
|
-
|
36
|
+
Arrest::debug "Setting headerd to #{hd}"
|
31
37
|
if hd == nil
|
32
38
|
@header_decorator = self
|
33
39
|
elsif hd.respond_to?(:headers)
|
@@ -41,9 +47,19 @@ module Arrest
|
|
41
47
|
{}
|
42
48
|
end
|
43
49
|
|
50
|
+
def key_from_json name
|
51
|
+
StringUtils.underscore(name.to_s)
|
52
|
+
end
|
53
|
+
|
54
|
+
def key_to_json name
|
55
|
+
StringUtils.classify(name.to_s,false)
|
56
|
+
end
|
57
|
+
|
44
58
|
end
|
45
59
|
end
|
46
60
|
Source.mod = nil
|
47
61
|
Source.header_decorator = Source
|
48
62
|
Source.debug = false
|
63
|
+
Source.json_key_converter = Source
|
64
|
+
|
49
65
|
end
|
data/lib/arrest/version.rb
CHANGED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'arrest'
|
2
|
+
require 'test/unit'
|
3
|
+
class HasAttributesTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
class ItHas
|
6
|
+
include Arrest::HasAttributes
|
7
|
+
|
8
|
+
attribute :name, String
|
9
|
+
attribute :my_name, String
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def test_empty_new
|
14
|
+
ih = ItHas.new
|
15
|
+
ih.name = "NAME"
|
16
|
+
ih.my_name = "MYNAME"
|
17
|
+
assert_equal "NAME", ih.name
|
18
|
+
assert_equal "MYNAME", ih.my_name
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_new
|
22
|
+
ih = ItHas.new({:name => "NAME", :my_name => "MYNAME"})
|
23
|
+
assert_equal "NAME", ih.name
|
24
|
+
assert_equal "MYNAME", ih.my_name
|
25
|
+
end
|
26
|
+
|
27
|
+
class ItHasMore < ItHas
|
28
|
+
|
29
|
+
attribute :my_other_name, String
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_new_inherit
|
33
|
+
ih = ItHasMore.new({:name => "NAME", :my_name => "MYNAME", :my_other_name => "OTHER"})
|
34
|
+
assert_equal "NAME", ih.name
|
35
|
+
assert_equal "MYNAME", ih.my_name
|
36
|
+
assert_equal "OTHER", ih.my_other_name
|
37
|
+
assert_equal 3, ih.class.all_fields.length
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_to_json
|
41
|
+
ih = ItHasMore.new({:name => "NAME", :my_name => "MYNAME", :my_other_name => "OTHER"})
|
42
|
+
h = ih.to_hash
|
43
|
+
assert_equal "NAME", h[:name]
|
44
|
+
assert_equal "MYNAME", h[:my_name]
|
45
|
+
assert_equal "OTHER", h[:my_other_name]
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
class ItHasMoreAndId < ItHasMore
|
50
|
+
attribute :id, String
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_to_json_with_id
|
54
|
+
assert_equal [:name, :my_name].sort, ItHas.all_fields.map(&:name).sort
|
55
|
+
assert_equal [:name, :my_name, :my_other_name].sort, ItHasMore.all_fields.map(&:name).sort
|
56
|
+
assert_equal [:name, :my_name, :my_other_name, :id].sort, ItHasMoreAndId.all_fields.map(&:name).sort
|
57
|
+
|
58
|
+
ih = ItHasMoreAndId.new({:id => 'vr23', :name => "NAME", :my_name => "MYNAME", :my_other_name => "OTHER"})
|
59
|
+
ih.id = 'foo42'
|
60
|
+
h = ih.to_hash
|
61
|
+
assert_equal "NAME", h[:name]
|
62
|
+
assert_equal "MYNAME", h[:my_name]
|
63
|
+
assert_equal "OTHER", h[:my_other_name]
|
64
|
+
assert_equal 'foo42', h[:id]
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
data/test/models.rb
CHANGED
data/test/nested_resource.rb
CHANGED
@@ -9,8 +9,9 @@ class NestedResourcesTest < Test::Unit::TestCase
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_instance_test
|
12
|
-
n = ANestedClass.new({:name => "foo"})
|
12
|
+
n = ANestedClass.new({:name => "foo", :underscore_name => "Bar"})
|
13
13
|
assert_equal "foo", n.name
|
14
|
+
assert_equal "Bar", n.underscore_name
|
14
15
|
end
|
15
16
|
|
16
17
|
def test_from_hash
|
@@ -19,6 +20,7 @@ class NestedResourcesTest < Test::Unit::TestCase
|
|
19
20
|
:bool => false,
|
20
21
|
:nested_object => {
|
21
22
|
:name => 'iamnested',
|
23
|
+
:underscore_name => 'foo',
|
22
24
|
:bool => true
|
23
25
|
}
|
24
26
|
}
|
@@ -37,23 +39,14 @@ class NestedResourcesTest < Test::Unit::TestCase
|
|
37
39
|
:bool => false,
|
38
40
|
:nested_object => {
|
39
41
|
:name => 'iamnested',
|
42
|
+
:underscore_name => 'foo',
|
40
43
|
:bool => true
|
41
44
|
}
|
42
45
|
}
|
43
46
|
|
44
47
|
actual = WithNested.new(input)
|
45
48
|
|
46
|
-
|
47
|
-
expected = {
|
48
|
-
'parentName' => 'parent',
|
49
|
-
'bool' => false,
|
50
|
-
'nestedObject' => {
|
51
|
-
'name' => 'iamnested',
|
52
|
-
'bool' => true
|
53
|
-
}
|
54
|
-
}
|
55
|
-
|
56
|
-
assert_equal_hashes expected, actual.to_hash
|
49
|
+
assert_equal_hashes input, actual.to_hash
|
57
50
|
|
58
51
|
end
|
59
52
|
|
@@ -74,22 +67,7 @@ class NestedResourcesTest < Test::Unit::TestCase
|
|
74
67
|
|
75
68
|
actual = WithManyNested.new(input)
|
76
69
|
|
77
|
-
|
78
|
-
expected = {
|
79
|
-
'parentName' => 'parent',
|
80
|
-
'bool' => false,
|
81
|
-
'nestedObjects' => [
|
82
|
-
{
|
83
|
-
'name' => 'iamnested_one',
|
84
|
-
'bool' => true
|
85
|
-
},{
|
86
|
-
'name' => 'iamnested_two',
|
87
|
-
'bool' => false
|
88
|
-
}
|
89
|
-
]
|
90
|
-
}
|
91
|
-
|
92
|
-
assert_equal_hashes expected, actual.to_hash
|
70
|
+
assert_equal_hashes input, actual.to_hash
|
93
71
|
|
94
72
|
end
|
95
73
|
|
@@ -109,19 +87,7 @@ class NestedResourcesTest < Test::Unit::TestCase
|
|
109
87
|
|
110
88
|
actual = WithNestedBelongingTo.new(input)
|
111
89
|
|
112
|
-
|
113
|
-
expected = {
|
114
|
-
'parentName' => 'parent',
|
115
|
-
'bool' => false,
|
116
|
-
'nestedObject' => {
|
117
|
-
'name' => 'iamnested',
|
118
|
-
'bool' => true,
|
119
|
-
'zooId' => new_zoo.id
|
120
|
-
|
121
|
-
}
|
122
|
-
}
|
123
|
-
|
124
|
-
assert_equal_hashes expected, actual.to_hash
|
90
|
+
assert_equal_hashes input, actual.to_hash
|
125
91
|
|
126
92
|
zoo = actual.nested_object.zoo
|
127
93
|
assert_equal "Foo", zoo.name
|
data/test/unit.rb
CHANGED
@@ -5,7 +5,7 @@ class FirstTest < Test::Unit::TestCase
|
|
5
5
|
|
6
6
|
def setup
|
7
7
|
Arrest::Source.source = nil
|
8
|
-
|
8
|
+
Arrest::Source.debug = true
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_mem_src
|
@@ -15,9 +15,14 @@ class FirstTest < Test::Unit::TestCase
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_init
|
18
|
+
puts "test_init NOWWWWWW #{Arrest::Source::source.objects.length}"
|
18
19
|
zooname = "Hagenbecks"
|
19
20
|
z = Zoo.new({:name => zooname})
|
21
|
+
puts "aaa #{z.attribute_values.inspect}"
|
20
22
|
assert_equal zooname, z.name
|
23
|
+
#assert_not_empty Arrest::AbstractResource.all_fields.select {|f| f.name == :id}, "AbstractResource defines the id field itself"
|
24
|
+
#assert_not_empty Arrest::RootResource.all_fields.select {|f| f.name == :id}, "RootResource should inherit id from AbstractResource"
|
25
|
+
#assert_not_empty Zoo.all_fields.select {|f| f.name == :id}, "Zoo should inherit id field from RootResource"
|
21
26
|
end
|
22
27
|
|
23
28
|
def test_create
|
@@ -25,15 +30,18 @@ class FirstTest < Test::Unit::TestCase
|
|
25
30
|
new_zoo = Zoo.new({:name => "Foo"})
|
26
31
|
new_zoo.save
|
27
32
|
zoo_count_after = Zoo.all.length
|
33
|
+
assert_not_nil new_zoo.id
|
28
34
|
|
29
35
|
assert_equal (zoo_count_before + 1), zoo_count_after
|
30
36
|
assert new_zoo.id != nil
|
31
37
|
|
32
38
|
zoo_the_last = Zoo.all.last
|
33
39
|
assert_equal new_zoo.name, zoo_the_last.name
|
40
|
+
assert_equal new_zoo.id, zoo_the_last.id
|
34
41
|
|
35
42
|
zoo_reloaded = Zoo.find(new_zoo.id)
|
36
43
|
assert_equal new_zoo.name, zoo_reloaded.name
|
44
|
+
assert_equal new_zoo.id, zoo_reloaded.id
|
37
45
|
end
|
38
46
|
|
39
47
|
def test_delete
|
@@ -75,6 +83,8 @@ class FirstTest < Test::Unit::TestCase
|
|
75
83
|
assert new_zoo.id != nil
|
76
84
|
|
77
85
|
zoo_reloaded = Zoo.find(new_zoo.id)
|
86
|
+
assert_not_nil zoo_reloaded
|
87
|
+
assert_equal new_zoo.id, zoo_reloaded.id
|
78
88
|
|
79
89
|
assert_equal new_zoo_name, zoo_reloaded.name
|
80
90
|
end
|
@@ -82,6 +92,8 @@ class FirstTest < Test::Unit::TestCase
|
|
82
92
|
def test_child
|
83
93
|
new_zoo = Zoo.new({:name => "Foo"})
|
84
94
|
new_zoo.save
|
95
|
+
assert_not_nil new_zoo.id
|
96
|
+
#assert_not_nil new_zoo.class.all_fields.select {|f| f.name = :id}
|
85
97
|
|
86
98
|
animal_kind = "mouse"
|
87
99
|
new_animal = Animal.new new_zoo, {:kind => animal_kind, :age => 42}
|
@@ -94,7 +106,9 @@ class FirstTest < Test::Unit::TestCase
|
|
94
106
|
assert new_animal.id != nil
|
95
107
|
|
96
108
|
zoo_reloaded = Zoo.find(new_zoo.id)
|
97
|
-
|
109
|
+
puts "--1 #{zoo_reloaded.to_hash}"
|
110
|
+
assert_equal new_zoo.id, zoo_reloaded.id
|
111
|
+
assert_equal new_animal.parent.id, zoo_reloaded.id
|
98
112
|
|
99
113
|
assert_equal 1, zoo_reloaded.animals.length
|
100
114
|
assert_equal Animal, zoo_reloaded.animals.first.class
|
@@ -126,11 +140,15 @@ class FirstTest < Test::Unit::TestCase
|
|
126
140
|
end
|
127
141
|
|
128
142
|
def test_inheritance
|
143
|
+
puts "NOWWWWWW #{Arrest::Source::source.objects.length}"
|
129
144
|
new_zoo = SpecialZoo.new({:name => "Foo", :is_magic => true})
|
145
|
+
assert_equal "Foo", new_zoo.name
|
146
|
+
assert_equal true, new_zoo.is_magic
|
130
147
|
new_zoo.save
|
131
148
|
|
132
149
|
assert new_zoo.id != nil, "Zoo must have id after save"
|
133
150
|
zoo_reloaded = SpecialZoo.find(new_zoo.id)
|
151
|
+
assert_equal new_zoo.id, zoo_reloaded.id
|
134
152
|
assert_equal true, zoo_reloaded.is_magic
|
135
153
|
assert_equal "Foo", zoo_reloaded.name
|
136
154
|
end
|
@@ -172,7 +190,7 @@ class FirstTest < Test::Unit::TestCase
|
|
172
190
|
assert_equal "two", zoo.ro2
|
173
191
|
assert_equal true, zoo.is_magic
|
174
192
|
|
175
|
-
hash = zoo.
|
193
|
+
hash = zoo.to_jhash
|
176
194
|
|
177
195
|
assert_nil hash[:ro1]
|
178
196
|
assert_nil hash[:ro2]
|
@@ -188,9 +206,9 @@ class FirstTest < Test::Unit::TestCase
|
|
188
206
|
assert_not_nil new_zoo.id
|
189
207
|
|
190
208
|
stubbed = Zoo.stub(new_zoo.id)
|
191
|
-
assert stubbed.
|
209
|
+
assert stubbed.stubbed?, "Zoo should be a stub, so not loaded yet"
|
192
210
|
new_name = stubbed.name
|
193
|
-
assert !stubbed.
|
211
|
+
assert !stubbed.stubbed?, "Zoo should not be a stub, so loaded now"
|
194
212
|
|
195
213
|
assert_equal "Foo", new_name
|
196
214
|
end
|
@@ -202,24 +220,21 @@ class FirstTest < Test::Unit::TestCase
|
|
202
220
|
assert_not_nil new_zoo.id
|
203
221
|
# this is where the magic hapens
|
204
222
|
stubbed = Zoo.stub(new_zoo.id)
|
205
|
-
return
|
206
223
|
|
207
224
|
new_animal = Animal.new new_zoo, {:kind => "foo", :age => 42}
|
208
225
|
new_animal.save
|
209
226
|
|
210
|
-
assert stubbed.
|
227
|
+
assert stubbed.stubbed?, "Zoo should be a stub, so not loaded yet"
|
211
228
|
|
212
229
|
animals = stubbed.animals
|
213
230
|
|
214
|
-
assert stubbed.
|
231
|
+
assert stubbed.stubbed?, "Zoo should still be a stub, so not loaded yet"
|
215
232
|
assert_equal 1, animals.length
|
216
233
|
|
217
234
|
new_name = stubbed.name
|
218
|
-
assert !stubbed.
|
235
|
+
assert !stubbed.stubbed?, "Zoo should not be a stub, so loaded now"
|
219
236
|
|
220
237
|
assert_equal "Foo", new_name
|
221
|
-
|
222
|
-
|
223
238
|
end
|
224
239
|
|
225
240
|
def test_root_scope
|
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.8
|
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-12-
|
12
|
+
date: 2011-12-07 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &2154088800 !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: *2154088800
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: faraday
|
27
|
-
requirement: &
|
27
|
+
requirement: &2154087020 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - =
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 0.7.5
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *2154087020
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: bundler
|
38
|
-
requirement: &
|
38
|
+
requirement: &2154085760 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.0.0
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *2154085760
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rake
|
49
|
-
requirement: &
|
49
|
+
requirement: &2154084940 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *2154084940
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rdoc
|
60
|
-
requirement: &
|
60
|
+
requirement: &2154083900 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *2154083900
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
|
-
requirement: &
|
71
|
+
requirement: &2154082700 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '2'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *2154082700
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rr
|
82
|
-
requirement: &
|
82
|
+
requirement: &2154082100 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *2154082100
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: simplecov
|
93
|
-
requirement: &
|
93
|
+
requirement: &2154081620 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *2154081620
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: rack
|
104
|
-
requirement: &
|
104
|
+
requirement: &2154081180 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,7 +109,7 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *2154081180
|
113
113
|
description: Consume a rest API in a AR like fashion
|
114
114
|
email:
|
115
115
|
- axel.tetzlaff@fortytools.com
|
@@ -141,6 +141,7 @@ files:
|
|
141
141
|
- spec/arrest_spec.rb
|
142
142
|
- spec/spec_helper.rb
|
143
143
|
- spec/support/models/user.rb
|
144
|
+
- test/has_attributed.rb
|
144
145
|
- test/models.rb
|
145
146
|
- test/nested_resource.rb
|
146
147
|
- test/unit.rb
|
@@ -172,6 +173,7 @@ test_files:
|
|
172
173
|
- spec/arrest_spec.rb
|
173
174
|
- spec/spec_helper.rb
|
174
175
|
- spec/support/models/user.rb
|
176
|
+
- test/has_attributed.rb
|
175
177
|
- test/models.rb
|
176
178
|
- test/nested_resource.rb
|
177
179
|
- test/unit.rb
|