arrest 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arrest.rb +0 -4
- data/lib/arrest/abstract_resource.rb +3 -5
- data/lib/arrest/helper/logger.rb +3 -0
- data/lib/arrest/root_resource.rb +4 -0
- data/lib/arrest/source.rb +5 -3
- data/lib/arrest/version.rb +1 -1
- data/test/unit.rb +1 -1
- data/test/validations.rb +28 -20
- metadata +22 -26
- data/lib/arrest/validations/inclusion_of.rb +0 -25
- data/lib/arrest/validations/presence_of.rb +0 -18
- data/lib/arrest/validations/validatable.rb +0 -69
- data/lib/arrest/validations/validator.rb +0 -27
data/lib/arrest.rb
CHANGED
@@ -15,10 +15,6 @@ require "arrest/http_source"
|
|
15
15
|
require "arrest/mem_source"
|
16
16
|
require "arrest/source"
|
17
17
|
require "arrest/nested_resource"
|
18
|
-
require "arrest/validations/validator"
|
19
|
-
require "arrest/validations/presence_of"
|
20
|
-
require "arrest/validations/inclusion_of"
|
21
|
-
require "arrest/validations/validatable"
|
22
18
|
require "arrest/abstract_resource"
|
23
19
|
require "arrest/root_resource"
|
24
20
|
require "arrest/rest_child"
|
@@ -8,12 +8,11 @@ Scope = Struct.new(:name, :block)
|
|
8
8
|
module Arrest
|
9
9
|
class AbstractResource
|
10
10
|
extend ActiveModel::Naming
|
11
|
+
include ActiveModel::Validations
|
12
|
+
include ActiveModel::Conversion
|
11
13
|
include HasAttributes
|
12
|
-
include Validatable
|
13
14
|
attribute :id, String
|
14
15
|
|
15
|
-
attr_accessor :errors
|
16
|
-
|
17
16
|
class << self
|
18
17
|
attr_reader :scopes
|
19
18
|
|
@@ -129,8 +128,7 @@ module Arrest
|
|
129
128
|
end
|
130
129
|
|
131
130
|
def save
|
132
|
-
|
133
|
-
if self.errors.empty?
|
131
|
+
if Source.skip_validations || self.valid?
|
134
132
|
verb = new_record? ? :post : :put
|
135
133
|
!!AbstractResource::source.send(verb, self)
|
136
134
|
else
|
data/lib/arrest/helper/logger.rb
CHANGED
data/lib/arrest/root_resource.rb
CHANGED
@@ -34,6 +34,10 @@ module Arrest
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def find id
|
37
|
+
if id == nil || "" == id
|
38
|
+
Arrest::logger.info "DocumentNotFoundError: no id given"
|
39
|
+
raise Errors::DocumentNotFoundError.new
|
40
|
+
end
|
37
41
|
r = source().get_one "#{self.resource_path}/#{id}"
|
38
42
|
body = body_root(r)
|
39
43
|
if body == nil || body.empty?
|
data/lib/arrest/source.rb
CHANGED
@@ -5,20 +5,21 @@ module Arrest
|
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
8
|
-
class Source
|
8
|
+
class Source
|
9
9
|
class << self
|
10
10
|
attr_accessor :debug
|
11
11
|
attr_reader :source
|
12
12
|
attr_reader :mod
|
13
13
|
attr_reader :header_decorator
|
14
14
|
attr_accessor :json_key_converter
|
15
|
+
attr_accessor :skip_validations
|
15
16
|
|
16
17
|
def source=(host=nil)
|
17
|
-
if
|
18
|
+
if [nil, ""].include?(host)
|
18
19
|
@source = MemSource.new
|
19
20
|
Arrest::logger.info "Setting Arrest host empty in-memory-store"
|
20
21
|
else
|
21
|
-
@source = HttpSource.new host
|
22
|
+
@source = HttpSource.new host
|
22
23
|
Arrest::logger.info "Setting Arrest host to #{host}"
|
23
24
|
end
|
24
25
|
@source
|
@@ -63,5 +64,6 @@ module Arrest
|
|
63
64
|
Source.header_decorator = Source
|
64
65
|
Source.debug = false
|
65
66
|
Source.json_key_converter = Source
|
67
|
+
Source.skip_validations = false
|
66
68
|
|
67
69
|
end
|
data/lib/arrest/version.rb
CHANGED
data/test/unit.rb
CHANGED
@@ -54,7 +54,7 @@ class FirstTest < Test::Unit::TestCase
|
|
54
54
|
new_zoo = Zoo.new(p)
|
55
55
|
assert_equal false, new_zoo.save, "zoo without name shouldnt be persistable"
|
56
56
|
assert_equal zoo_count_before, Zoo.all.length
|
57
|
-
assert_equal :name, new_zoo.errors.first
|
57
|
+
assert_equal :name, new_zoo.errors.first[0]
|
58
58
|
assert_nil new_zoo.id
|
59
59
|
|
60
60
|
new_zoo.name = "Foo"
|
data/test/validations.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'test/unit'
|
2
2
|
require 'arrest'
|
3
|
+
require 'active_model'
|
3
4
|
|
4
|
-
class PresenceOfClass
|
5
|
-
include Arrest::Validatable
|
5
|
+
class PresenceOfClass < Arrest::RootResource
|
6
6
|
attr_accessor :foo
|
7
7
|
|
8
8
|
validates_presence_of :foo
|
@@ -13,7 +13,7 @@ class PresenceOfClass
|
|
13
13
|
end
|
14
14
|
|
15
15
|
class PresenceOfTwo
|
16
|
-
include
|
16
|
+
include ActiveModel::Validations
|
17
17
|
attr_accessor :foo, :bar
|
18
18
|
|
19
19
|
validates_presence_of :foo
|
@@ -37,10 +37,10 @@ class InheritedPresence < PresenceOfClass
|
|
37
37
|
end
|
38
38
|
|
39
39
|
class CustomVal
|
40
|
-
include
|
40
|
+
include ActiveModel::Validations
|
41
41
|
attr_accessor :foo
|
42
42
|
|
43
|
-
|
43
|
+
validate :is_foo
|
44
44
|
|
45
45
|
def initialize foo=nil
|
46
46
|
@foo = foo
|
@@ -48,19 +48,30 @@ class CustomVal
|
|
48
48
|
|
49
49
|
def is_foo
|
50
50
|
if self.foo != "Foo"
|
51
|
-
|
52
|
-
else
|
53
|
-
[]
|
51
|
+
errors.add(:foo, "is not foo")
|
54
52
|
end
|
55
53
|
end
|
56
54
|
end
|
57
55
|
|
56
|
+
class InclusionOfClass
|
57
|
+
include ActiveModel::Validations
|
58
|
+
|
59
|
+
attr_accessor :foo
|
60
|
+
|
61
|
+
validates :foo, :inclusion => { :in => ["Foo", "Bar"] }
|
62
|
+
|
63
|
+
def initialize foo
|
64
|
+
@foo = foo
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
58
68
|
class ValidationsTest < Test::Unit::TestCase
|
59
69
|
|
60
70
|
|
61
71
|
def setup
|
62
72
|
Arrest::Source.source = nil
|
63
73
|
#Arrest::Source.debug = true
|
74
|
+
Arrest::Source.skip_validations = false
|
64
75
|
end
|
65
76
|
|
66
77
|
def test_prsnc
|
@@ -71,6 +82,15 @@ class ValidationsTest < Test::Unit::TestCase
|
|
71
82
|
assert o1.valid?, "Foo is present and valid!"
|
72
83
|
end
|
73
84
|
|
85
|
+
def test_skip_validations
|
86
|
+
Arrest::Source.skip_validations = true
|
87
|
+
o0 = PresenceOfClass.new nil
|
88
|
+
assert o0.valid? == false, "Foo is '#{o0.foo}' -> not present and thus not valid!"
|
89
|
+
assert o0.save, "When skipping validations, in-mem-storage should work"
|
90
|
+
|
91
|
+
Arrest::Source.skip_validations = false
|
92
|
+
end
|
93
|
+
|
74
94
|
def test_presence_of_two
|
75
95
|
o = PresenceOfTwo.new
|
76
96
|
assert o.valid? == false, "Both missing, must not be valid"
|
@@ -122,17 +142,6 @@ class ValidationsTest < Test::Unit::TestCase
|
|
122
142
|
|
123
143
|
# ================ inclusion_of ======
|
124
144
|
|
125
|
-
class InclusionOfClass
|
126
|
-
include Arrest::Validatable
|
127
|
-
|
128
|
-
attr_accessor :foo
|
129
|
-
|
130
|
-
validates_inclusion_of :foo, :in => ["Foo", "Bar"]
|
131
|
-
|
132
|
-
def initialize foo
|
133
|
-
@foo = foo
|
134
|
-
end
|
135
|
-
end
|
136
145
|
|
137
146
|
def test_inclusion_of
|
138
147
|
invalids = [nil, '', "Baz", "foo", 3, true]
|
@@ -142,7 +151,6 @@ class ValidationsTest < Test::Unit::TestCase
|
|
142
151
|
invalids.each do |iv|
|
143
152
|
o = InclusionOfClass.new(iv)
|
144
153
|
assert o.valid? == false, "#{iv} is not valid"
|
145
|
-
assert o.validate.map(&:attribute).include?(:foo), ":foo should be in list of invalid attributes when created with #{iv}"
|
146
154
|
end
|
147
155
|
|
148
156
|
valids.each do |v|
|
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.12
|
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: 2012-01-
|
12
|
+
date: 2012-01-10 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &15777380 !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: *15777380
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: faraday
|
27
|
-
requirement: &
|
27
|
+
requirement: &15776240 !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: *15776240
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activemodel
|
38
|
-
requirement: &
|
38
|
+
requirement: &15775480 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '3'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *15775480
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
requirement: &
|
49
|
+
requirement: &15749340 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.0.0
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *15749340
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &15748420 !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: *15748420
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rdoc
|
71
|
-
requirement: &
|
71
|
+
requirement: &15747940 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *15747940
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rspec
|
82
|
-
requirement: &
|
82
|
+
requirement: &15747400 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '2'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *15747400
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rr
|
93
|
-
requirement: &
|
93
|
+
requirement: &15746680 !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: *15746680
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: simplecov
|
104
|
-
requirement: &
|
104
|
+
requirement: &15746220 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *15746220
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: rack
|
115
|
-
requirement: &
|
115
|
+
requirement: &15745800 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ! '>='
|
@@ -120,7 +120,7 @@ dependencies:
|
|
120
120
|
version: '0'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *15745800
|
124
124
|
description: Consume a rest API in a AR like fashion
|
125
125
|
email:
|
126
126
|
- axel.tetzlaff@fortytools.com
|
@@ -151,10 +151,6 @@ files:
|
|
151
151
|
- lib/arrest/root_resource.rb
|
152
152
|
- lib/arrest/source.rb
|
153
153
|
- lib/arrest/string_utils.rb
|
154
|
-
- lib/arrest/validations/inclusion_of.rb
|
155
|
-
- lib/arrest/validations/presence_of.rb
|
156
|
-
- lib/arrest/validations/validatable.rb
|
157
|
-
- lib/arrest/validations/validator.rb
|
158
154
|
- lib/arrest/version.rb
|
159
155
|
- spec/arrest_spec.rb
|
160
156
|
- spec/spec_helper.rb
|
@@ -1,25 +0,0 @@
|
|
1
|
-
module Arrest
|
2
|
-
module Validations
|
3
|
-
class InclusionOf < Validator
|
4
|
-
|
5
|
-
def initialize attribute, hash
|
6
|
-
super attribute
|
7
|
-
@hash = hash
|
8
|
-
end
|
9
|
-
|
10
|
-
def validate input
|
11
|
-
errors = []
|
12
|
-
if !input.respond_to?(@attribute)
|
13
|
-
errors << ValidationError.new(@attribute, "not_responding")
|
14
|
-
else
|
15
|
-
val = input.send(@attribute)
|
16
|
-
unless @hash[:in] != nil && @hash[:in].include?(val)
|
17
|
-
errors << ValidationError.new(@attribute,"not_included")
|
18
|
-
end
|
19
|
-
end
|
20
|
-
return errors
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Arrest
|
2
|
-
module Validations
|
3
|
-
class PresenceOf < Validator
|
4
|
-
def validate input
|
5
|
-
errors = []
|
6
|
-
if !input.respond_to?(@attribute)
|
7
|
-
errors << ValidationError.new(@attribute, "not_responding")
|
8
|
-
else
|
9
|
-
val = input.send(@attribute)
|
10
|
-
if val == nil || val == ''
|
11
|
-
errors << ValidationError.new(@attribute,"not_present")
|
12
|
-
end
|
13
|
-
end
|
14
|
-
return errors
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
module Arrest
|
2
|
-
module Validations
|
3
|
-
class MethodValidator
|
4
|
-
def initialize method = nil, &blk
|
5
|
-
@method = method
|
6
|
-
end
|
7
|
-
|
8
|
-
def validate input
|
9
|
-
input.send(@method)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
module Validatable
|
15
|
-
def self.included(base) # :nodoc:
|
16
|
-
base.extend ValidatableMethods
|
17
|
-
end
|
18
|
-
|
19
|
-
module ValidatableMethods
|
20
|
-
def validates_presence_of attribute
|
21
|
-
add_validator Validations::PresenceOf.new attribute
|
22
|
-
end
|
23
|
-
|
24
|
-
def validates method_name
|
25
|
-
add_validator Validations::MethodValidator.new method_name
|
26
|
-
end
|
27
|
-
|
28
|
-
def validates_inclusion_of attribute, hash = {}
|
29
|
-
add_validator Validations::InclusionOf.new attribute, hash
|
30
|
-
end
|
31
|
-
|
32
|
-
def add_validator v
|
33
|
-
if @validations == nil
|
34
|
-
@validations = []
|
35
|
-
end
|
36
|
-
@validations << v
|
37
|
-
end
|
38
|
-
|
39
|
-
def validations
|
40
|
-
if self.superclass.respond_to? :validations
|
41
|
-
super_v = self.superclass.validations
|
42
|
-
else
|
43
|
-
super_v = []
|
44
|
-
end
|
45
|
-
if @validations == nil
|
46
|
-
@validations = []
|
47
|
-
end
|
48
|
-
|
49
|
-
@validations + super_v
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
# --------- instance methods ---------
|
54
|
-
def valid?
|
55
|
-
validate.empty?
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
def validate
|
60
|
-
vs = self.class.validations
|
61
|
-
return [] if vs == nil
|
62
|
-
errors = []
|
63
|
-
vs.each do |v|
|
64
|
-
errors += v.validate self
|
65
|
-
end
|
66
|
-
errors
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Arrest
|
2
|
-
module Validations
|
3
|
-
class ValidationError
|
4
|
-
attr_accessor :attribute, :message
|
5
|
-
|
6
|
-
def initialize attribute, message
|
7
|
-
@attribute = attribute
|
8
|
-
@message = message
|
9
|
-
end
|
10
|
-
|
11
|
-
def translate
|
12
|
-
"#{self.attribute} - #{self.message}"
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
class Validator
|
17
|
-
def initialize attribute
|
18
|
-
@attribute = attribute
|
19
|
-
end
|
20
|
-
|
21
|
-
def validate input
|
22
|
-
return []
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|