arrest 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/arrest.gemspec CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
22
22
  # s.add_development_dependency "rspec"
23
23
  s.add_runtime_dependency "json"
24
24
  s.add_runtime_dependency "faraday", '0.7.5'
25
+ s.add_runtime_dependency "activemodel", '~> 3'
25
26
 
26
27
  s.add_development_dependency 'bundler', '>= 1.0.0'
27
28
  s.add_development_dependency 'rake'
@@ -1,11 +1,13 @@
1
1
  require 'json'
2
2
  require 'arrest/string_utils'
3
3
  require 'time'
4
+ require 'active_model'
4
5
 
5
6
  Scope = Struct.new(:name, :block)
6
7
 
7
8
  module Arrest
8
9
  class AbstractResource
10
+ extend ActiveModel::Naming
9
11
  include HasAttributes
10
12
  attribute :id, String
11
13
  class << self
@@ -16,8 +16,10 @@ module Arrest
16
16
  if params
17
17
  field_name = params[:field_name] unless params[:field_name] == nil
18
18
  class_name = params[:class_name].to_s unless params[:class_name] == nil
19
+ read_only = params[:read_only] == true
19
20
  end
20
21
  attributes({field_name.to_sym => String})
22
+ add_attribute(Attribute.new(field_name.to_sym, read_only, String))
21
23
  send :define_method, name do
22
24
  val = self.send(field_name)
23
25
  begin
@@ -1,5 +1,5 @@
1
1
  module Arrest
2
- class ChildCollection < BasicObject
2
+ class ChildCollection #< BasicObject
3
3
  def initialize parent, clazz_name
4
4
  @parent = parent
5
5
  @clazz_name = clazz_name
@@ -12,12 +12,18 @@ module Arrest
12
12
 
13
13
  def method_missing(*args, &block)
14
14
  if resolved_class.respond_to?(args[0])
15
- resolved_class.send(args[0], @parent)
15
+ sub_args = [@parent]
16
+ sub_args += args.drop(1)
17
+ resolved_class.send(args[0], *sub_args)
16
18
  else
17
19
  children.send(*args, &block)
18
20
  end
19
21
  end
20
22
 
23
+ def inspect
24
+ children.inspect
25
+ end
26
+
21
27
  private
22
28
 
23
29
 
@@ -38,6 +38,18 @@ module Arrest
38
38
  self.build body
39
39
  end
40
40
 
41
+ def filter name, &aproc
42
+ if aproc != nil
43
+ send :define_singleton_method, name do |*args|
44
+ self.all_for(args[0]).select do |instance|
45
+ instance.instance_exec(*(args.drop(1)), &aproc)
46
+ end
47
+ end
48
+ else
49
+ raise "You must specify a block for a filter"
50
+ end
51
+ end
52
+
41
53
  def scope name, &block
42
54
  super name
43
55
  if block_given?
@@ -24,6 +24,18 @@ module Arrest
24
24
  self.build body.merge({:id => id})
25
25
  end
26
26
 
27
+ def filter name, &aproc
28
+ if aproc != nil
29
+ send :define_singleton_method, name do |args|
30
+ self.all.select do |instance|
31
+ instance.instance_exec(args, &aproc)
32
+ end
33
+ end
34
+ else
35
+ raise "You must specify a block for a filter"
36
+ end
37
+ end
38
+
27
39
  def scope name, &block
28
40
  super(name)
29
41
  if block_given?
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
data/test/models.rb CHANGED
@@ -67,3 +67,20 @@ class CustomNamedBelongsTo < Arrest::RootResource
67
67
  belongs_to :zoo_thing, { :field_name => :schinken, :class_name => :Zoo}
68
68
  belongs_to :zoo, { :field_name => :batzen}
69
69
  end
70
+
71
+ class ParentFilter < Arrest::RootResource
72
+ attribute :id, String
73
+ attribute :afield, String
74
+
75
+ filter(:nnn) {|s| afield == s}
76
+ has_many :child_filters
77
+ end
78
+
79
+ class ChildFilter < Arrest::RestChild
80
+ attribute :id, String
81
+ attribute :bfield, String
82
+
83
+ parent :parent_filter
84
+
85
+ filter(:child_nnn) {|s| bfield == s}
86
+ end
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
- Arrest::Source.debug = true
8
+ #Arrest::Source.debug = true
9
9
  end
10
10
 
11
11
  def test_mem_src
@@ -15,10 +15,8 @@ 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}"
19
18
  zooname = "Hagenbecks"
20
19
  z = Zoo.new({:name => zooname})
21
- puts "aaa #{z.attribute_values.inspect}"
22
20
  assert_equal zooname, z.name
23
21
  #assert_not_empty Arrest::AbstractResource.all_fields.select {|f| f.name == :id}, "AbstractResource defines the id field itself"
24
22
  #assert_not_empty Arrest::RootResource.all_fields.select {|f| f.name == :id}, "RootResource should inherit id from AbstractResource"
@@ -106,7 +104,6 @@ class FirstTest < Test::Unit::TestCase
106
104
  assert new_animal.id != nil
107
105
 
108
106
  zoo_reloaded = Zoo.find(new_zoo.id)
109
- puts "--1 #{zoo_reloaded.to_hash}"
110
107
  assert_equal new_zoo.id, zoo_reloaded.id
111
108
  assert_equal new_animal.parent.id, zoo_reloaded.id
112
109
 
@@ -140,7 +137,6 @@ class FirstTest < Test::Unit::TestCase
140
137
  end
141
138
 
142
139
  def test_inheritance
143
- puts "NOWWWWWW #{Arrest::Source::source.objects.length}"
144
140
  new_zoo = SpecialZoo.new({:name => "Foo", :is_magic => true})
145
141
  assert_equal "Foo", new_zoo.name
146
142
  assert_equal true, new_zoo.is_magic
@@ -274,5 +270,32 @@ class FirstTest < Test::Unit::TestCase
274
270
  assert_equal true, Zoo.all.first.animals.males_only.first.male
275
271
 
276
272
  end
273
+
274
+ def test_para_filter
275
+ p1 = ParentFilter.new({:afield => "Foo"})
276
+ p2 = ParentFilter.new({:afield => "Bar"})
277
+ p1.save
278
+ p2.save
279
+
280
+ nnn = ParentFilter.nnn("Foo")
281
+ assert_equal ["Foo"], nnn.map(&:afield)
282
+ end
283
+
284
+ def test_para_filter_child
285
+ p1 = ParentFilter.new({:afield => "ParentFoo"})
286
+ p1.save
287
+
288
+ c1 = ChildFilter.new(p1, :bfield => "Foo")
289
+ c1.save
290
+ c2 = ChildFilter.new(p1, :bfield => "Bar")
291
+ c2.save
292
+
293
+ reloaded_parent = ParentFilter.find(p1.id)
294
+ assert_not_nil reloaded_parent
295
+ assert_equal "ParentFoo", reloaded_parent.afield
296
+ assert_equal 2, reloaded_parent.child_filters.length
297
+
298
+ assert_equal ["Foo"], reloaded_parent.child_filters.child_nnn("Foo").map(&:bfield)
299
+ end
277
300
  end
278
301
 
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.8
4
+ version: 0.0.9
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-07 00:00:00.000000000Z
12
+ date: 2011-12-16 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &2154088800 !ruby/object:Gem::Requirement
16
+ requirement: &12008120 !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: *2154088800
24
+ version_requirements: *12008120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &2154087020 !ruby/object:Gem::Requirement
27
+ requirement: &12006980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
@@ -32,10 +32,21 @@ dependencies:
32
32
  version: 0.7.5
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2154087020
35
+ version_requirements: *12006980
36
+ - !ruby/object:Gem::Dependency
37
+ name: activemodel
38
+ requirement: &12006180 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '3'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *12006180
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: bundler
38
- requirement: &2154085760 !ruby/object:Gem::Requirement
49
+ requirement: &12005700 !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,10 @@ dependencies:
43
54
  version: 1.0.0
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *2154085760
57
+ version_requirements: *12005700
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rake
49
- requirement: &2154084940 !ruby/object:Gem::Requirement
60
+ requirement: &12005200 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,10 +65,10 @@ dependencies:
54
65
  version: '0'
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *2154084940
68
+ version_requirements: *12005200
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: rdoc
60
- requirement: &2154083900 !ruby/object:Gem::Requirement
71
+ requirement: &12004460 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: '0'
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *2154083900
79
+ version_requirements: *12004460
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: rspec
71
- requirement: &2154082700 !ruby/object:Gem::Requirement
82
+ requirement: &12003960 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ~>
@@ -76,10 +87,10 @@ dependencies:
76
87
  version: '2'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *2154082700
90
+ version_requirements: *12003960
80
91
  - !ruby/object:Gem::Dependency
81
92
  name: rr
82
- requirement: &2154082100 !ruby/object:Gem::Requirement
93
+ requirement: &12003540 !ruby/object:Gem::Requirement
83
94
  none: false
84
95
  requirements:
85
96
  - - ! '>='
@@ -87,10 +98,10 @@ dependencies:
87
98
  version: '0'
88
99
  type: :development
89
100
  prerelease: false
90
- version_requirements: *2154082100
101
+ version_requirements: *12003540
91
102
  - !ruby/object:Gem::Dependency
92
103
  name: simplecov
93
- requirement: &2154081620 !ruby/object:Gem::Requirement
104
+ requirement: &12003020 !ruby/object:Gem::Requirement
94
105
  none: false
95
106
  requirements:
96
107
  - - ! '>='
@@ -98,10 +109,10 @@ dependencies:
98
109
  version: '0'
99
110
  type: :development
100
111
  prerelease: false
101
- version_requirements: *2154081620
112
+ version_requirements: *12003020
102
113
  - !ruby/object:Gem::Dependency
103
114
  name: rack
104
- requirement: &2154081180 !ruby/object:Gem::Requirement
115
+ requirement: &12002560 !ruby/object:Gem::Requirement
105
116
  none: false
106
117
  requirements:
107
118
  - - ! '>='
@@ -109,7 +120,7 @@ dependencies:
109
120
  version: '0'
110
121
  type: :development
111
122
  prerelease: false
112
- version_requirements: *2154081180
123
+ version_requirements: *12002560
113
124
  description: Consume a rest API in a AR like fashion
114
125
  email:
115
126
  - axel.tetzlaff@fortytools.com
@@ -169,11 +180,4 @@ rubygems_version: 1.8.10
169
180
  signing_key:
170
181
  specification_version: 3
171
182
  summary: Another ruby rest client
172
- test_files:
173
- - spec/arrest_spec.rb
174
- - spec/spec_helper.rb
175
- - spec/support/models/user.rb
176
- - test/has_attributed.rb
177
- - test/models.rb
178
- - test/nested_resource.rb
179
- - test/unit.rb
183
+ test_files: []