croesus 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +121 -0
- data/.ruby-version +1 -0
- data/API_operation.txt +197 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +0 -0
- data/README.md +146 -0
- data/Rakefile +26 -0
- data/bin/console_cmd.rb +133 -0
- data/croesus.gemspec +39 -0
- data/lib/croesus/associations.rb +46 -0
- data/lib/croesus/attribute.rb +41 -0
- data/lib/croesus/attributes.rb +93 -0
- data/lib/croesus/coerce.rb +110 -0
- data/lib/croesus/coercions/boolean_definitions.rb +32 -0
- data/lib/croesus/coercions/date_definitions.rb +30 -0
- data/lib/croesus/coercions/date_time_definitions.rb +30 -0
- data/lib/croesus/coercions/fixnum_definitions.rb +32 -0
- data/lib/croesus/coercions/float_definitions.rb +30 -0
- data/lib/croesus/coercions/hash_definitions.rb +27 -0
- data/lib/croesus/coercions/integer_definitions.rb +29 -0
- data/lib/croesus/coercions/string_definitions.rb +43 -0
- data/lib/croesus/coercions/time_definitions.rb +30 -0
- data/lib/croesus/core_ext/blank.rb +123 -0
- data/lib/croesus/core_ext/hash.rb +185 -0
- data/lib/croesus/dsl/dsl.rb +35 -0
- data/lib/croesus/dsl/helpers.rb +43 -0
- data/lib/croesus/dsl/mod_factory.rb +191 -0
- data/lib/croesus/dsl/resource_dsl.rb +59 -0
- data/lib/croesus/dsl/route_dsl.rb +42 -0
- data/lib/croesus/identity_map.rb +98 -0
- data/lib/croesus/platform.rb +47 -0
- data/lib/croesus/querying.rb +63 -0
- data/lib/croesus/resources/about.rb +15 -0
- data/lib/croesus/resources/basic_methods.rb +36 -0
- data/lib/croesus/resources/connectivity.rb +42 -0
- data/lib/croesus/resources/container.rb +135 -0
- data/lib/croesus/resources/fault.rb +38 -0
- data/lib/croesus/resources/fault_effect.rb +24 -0
- data/lib/croesus/resources/group.rb +37 -0
- data/lib/croesus/resources/host.rb +26 -0
- data/lib/croesus/resources/job.rb +27 -0
- data/lib/croesus/resources/namespace.rb +39 -0
- data/lib/croesus/resources/policy.rb +38 -0
- data/lib/croesus/resources/source.rb +86 -0
- data/lib/croesus/resources/source_config.rb +54 -0
- data/lib/croesus/resources/source_environment.rb +58 -0
- data/lib/croesus/resources/source_repository.rb +14 -0
- data/lib/croesus/resources/system_info.rb +21 -0
- data/lib/croesus/resources/timeflow.rb +40 -0
- data/lib/croesus/resources/timeflow_snapshot.rb +38 -0
- data/lib/croesus/utils.rb +262 -0
- data/lib/croesus/validations/many.rb +27 -0
- data/lib/croesus/validations/optional.rb +27 -0
- data/lib/croesus/validations.rb +91 -0
- data/lib/croesus/validators/base.rb +47 -0
- data/lib/croesus/validators/boolean_validator.rb +32 -0
- data/lib/croesus/validators/email_validator.rb +36 -0
- data/lib/croesus/validators/enumerable_validator.rb +40 -0
- data/lib/croesus/validators/hash_validator.rb +50 -0
- data/lib/croesus/validators/lambda_validator.rb +54 -0
- data/lib/croesus/validators/many_validator.rb +57 -0
- data/lib/croesus/validators/optional_validator.rb +41 -0
- data/lib/croesus/validators/presence_validator.rb +36 -0
- data/lib/croesus/validators/simple_type_validators.rb +38 -0
- data/lib/croesus/validators/simple_validator.rb +40 -0
- data/lib/croesus/version.rb +43 -0
- data/lib/croesus/web_client/web_client.rb +153 -0
- data/lib/croesus/web_client/web_request.rb +77 -0
- data/lib/croesus/web_client/web_response.rb +70 -0
- data/lib/croesus.rb +250 -0
- metadata +325 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Croesus
|
21
|
+
class Base
|
22
|
+
attr_accessor :hash, :validations, :errors
|
23
|
+
|
24
|
+
def initialize(hash, validations)
|
25
|
+
self.errors = {}
|
26
|
+
self.hash = hash
|
27
|
+
self.validations = validations.inject({
|
28
|
+
}){ |memo,(k,v)| memo[k] = v.to_s.downcase; memo }
|
29
|
+
validate
|
30
|
+
end
|
31
|
+
|
32
|
+
def valid?
|
33
|
+
errors.empty?
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.validate(hash, validations)
|
37
|
+
new(hash, validations)
|
38
|
+
end
|
39
|
+
|
40
|
+
private # P R O P R I E T À P R I V A T A Vietato L'accesso
|
41
|
+
|
42
|
+
def validate
|
43
|
+
Croesus.validator_for(hash).validate(
|
44
|
+
:base, self.hash, self.validations, self.errors
|
45
|
+
)
|
46
|
+
self.errors = errors[:base]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
@@validators = []
|
51
|
+
|
52
|
+
def self.append_validator(validator)
|
53
|
+
unless validator.is_a?(Croesus::Validator::Base)
|
54
|
+
raise ValidationError.new(
|
55
|
+
'Validators inherit from Croesus::Validator::Base'
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
if @@validators.detect { |v| v.name == validator.name }
|
60
|
+
raise ValidationError.new('Validators must have unique names.')
|
61
|
+
end
|
62
|
+
|
63
|
+
@@validators << validator
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.validator_for(item)
|
67
|
+
@@validators.detect { |v| v.should_validate?(item) } ||
|
68
|
+
raise(ValidationError.new("Could not find valid validator for: #{item}"))
|
69
|
+
end
|
70
|
+
|
71
|
+
module Validator
|
72
|
+
end
|
73
|
+
|
74
|
+
module Validations
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Load validators
|
79
|
+
require 'croesus/validations/optional'
|
80
|
+
require 'croesus/validations/many'
|
81
|
+
require 'croesus/validators/base'
|
82
|
+
require 'croesus/validators/simple_validator'
|
83
|
+
require 'croesus/validators/hash_validator'
|
84
|
+
require 'croesus/validators/presence_validator'
|
85
|
+
require 'croesus/validators/simple_type_validators'
|
86
|
+
require 'croesus/validators/boolean_validator'
|
87
|
+
require 'croesus/validators/email_validator'
|
88
|
+
require 'croesus/validators/enumerable_validator'
|
89
|
+
require 'croesus/validators/lambda_validator'
|
90
|
+
require 'croesus/validators/optional_validator'
|
91
|
+
require 'croesus/validators/many_validator'
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
class Croesus::Validator::Base
|
21
|
+
attr_accessor :name
|
22
|
+
|
23
|
+
def initialize(name)
|
24
|
+
unless name.is_a?(String) && name.size > 0
|
25
|
+
raise StandardError.new(
|
26
|
+
'Validator must be initialized with a valid name '\
|
27
|
+
'(string with length greater than zero).'
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
self.name = name
|
32
|
+
end
|
33
|
+
|
34
|
+
def should_validate?(name)
|
35
|
+
self.name == name
|
36
|
+
end
|
37
|
+
|
38
|
+
def presence_error_message
|
39
|
+
"#{self.name} required"
|
40
|
+
end
|
41
|
+
|
42
|
+
def validate(key, value, validations, errors)
|
43
|
+
raise ValidationError.new(
|
44
|
+
'Validate should not be called directly on Croesus::Validator::Base'
|
45
|
+
)
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
class Croesus::Validator::BooleanValidator < Croesus::Validator::Base
|
21
|
+
def initialize
|
22
|
+
super('boolean') # The name of the validator
|
23
|
+
end
|
24
|
+
|
25
|
+
def validate(key, value, validations, errors)
|
26
|
+
unless [TrueClass, FalseClass].include?(value.class)
|
27
|
+
errors[key] = presence_error_message
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
Croesus.append_validator(Croesus::Validator::BooleanValidator.new)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
class Croesus::Validator::EmailValidator < Croesus::Validator::Base
|
21
|
+
def initialize
|
22
|
+
super('email') # The name of the validator
|
23
|
+
end
|
24
|
+
|
25
|
+
def presence_error_message
|
26
|
+
'is not a valid email'
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate(key, value, validations, errors)
|
30
|
+
unless value.is_a?(String) && value.include?("@")
|
31
|
+
errors[key] = presence_error_message
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Croesus.append_validator(Croesus::Validator::EmailValidator.new)
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
class Croesus::Validator::EnumerableValidator < Croesus::Validator::Base
|
21
|
+
def initialize
|
22
|
+
super('_enumerable') # The name of the validator, underscored as it won't usually be directly invoked (invoked through use of validator)
|
23
|
+
end
|
24
|
+
|
25
|
+
def should_validate?(item)
|
26
|
+
item.is_a?(Enumerable)
|
27
|
+
end
|
28
|
+
|
29
|
+
def presence_error_message
|
30
|
+
'value from list required'
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate(key, value, validations, errors)
|
34
|
+
unless validations.include?(value)
|
35
|
+
errors[key] = presence_error_message
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
Croesus.append_validator(Croesus::Validator::EnumerableValidator.new)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
class Croesus::Validator::HashValidator < Croesus::Validator::Base
|
21
|
+
def initialize
|
22
|
+
super('hash')
|
23
|
+
end
|
24
|
+
|
25
|
+
def should_validate?(item)
|
26
|
+
item.is_a?(Hash)
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate(key, value, validations, errors)
|
30
|
+
# Validate hash
|
31
|
+
unless value.is_a?(Hash)
|
32
|
+
errors[key] = presence_error_message
|
33
|
+
return
|
34
|
+
end
|
35
|
+
|
36
|
+
# Hashes can contain sub-elements, attempt to validator those
|
37
|
+
errors = (errors[key] = {})
|
38
|
+
|
39
|
+
validations.each do |v_key, v_value|
|
40
|
+
Croesus.validator_for(v_value).validate(
|
41
|
+
v_key, value[v_key], v_value, errors
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Cleanup errors (remove any empty nested errors)
|
46
|
+
errors.delete_if { |k,v| v.empty? }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
Croesus.append_validator(Croesus::Validator::HashValidator.new)
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
class Croesus::Validator::LambdaValidator < Croesus::Validator::Base
|
21
|
+
def initialize
|
22
|
+
super('_lambda') # The name of the validator, underscored as it won't usually be directly invoked (invoked through use of validator)
|
23
|
+
end
|
24
|
+
|
25
|
+
def should_validate?(item)
|
26
|
+
if item.is_a?(Proc)
|
27
|
+
if item.arity == 1
|
28
|
+
true
|
29
|
+
else
|
30
|
+
raise InvalidArgumentCount.new(
|
31
|
+
"Lambda validator should only accept one argument; " \
|
32
|
+
"supplied lambda accepts #{item.arity}."
|
33
|
+
)
|
34
|
+
end
|
35
|
+
else
|
36
|
+
false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def presence_error_message
|
41
|
+
'is not valid'
|
42
|
+
end
|
43
|
+
|
44
|
+
def validate(key, value, lambda, errors)
|
45
|
+
unless lambda.call(value)
|
46
|
+
errors[key] = presence_error_message
|
47
|
+
end
|
48
|
+
|
49
|
+
rescue
|
50
|
+
errors[key] = presence_error_message
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Croesus.append_validator(Croesus::Validator::LambdaValidator.new)
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Croesus::Validator
|
21
|
+
class ManyValidator < Base
|
22
|
+
def initialize
|
23
|
+
super('_many') # The name of the validator, underscored as it won't usually be directly invoked (invoked through use of validator)
|
24
|
+
end
|
25
|
+
|
26
|
+
def should_validate?(validation)
|
27
|
+
validation.is_a?(Croesus::Validations::Many)
|
28
|
+
end
|
29
|
+
|
30
|
+
def presence_error_message
|
31
|
+
"enumerable required"
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate(key, value, validations, errors)
|
35
|
+
unless value.is_a?(Enumerable)
|
36
|
+
errors[key] = presence_error_message
|
37
|
+
return
|
38
|
+
end
|
39
|
+
|
40
|
+
element_errors = Array.new
|
41
|
+
|
42
|
+
value.each_with_index do |element, i|
|
43
|
+
::Croesus.validator_for(validations.validation).validate(i, element, validations.validation, element_errors)
|
44
|
+
end
|
45
|
+
|
46
|
+
element_errors.each_with_index do |e, i|
|
47
|
+
if e.respond_to?(:empty?) && e.empty?
|
48
|
+
element_errors[i] = nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
errors[key] = element_errors unless element_errors.all?(&:nil?)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
Croesus.append_validator(Croesus::Validator::ManyValidator.new)
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
module Croesus::Validator
|
21
|
+
class OptionalValidator < Base
|
22
|
+
def initialize
|
23
|
+
# The name of the validator, underscored as it won't usually be directly
|
24
|
+
# invoked (invoked through use of validator).
|
25
|
+
super('_optional')
|
26
|
+
end
|
27
|
+
|
28
|
+
def should_validate?(validation)
|
29
|
+
validation.is_a?(Croesus::Validations::Optional)
|
30
|
+
end
|
31
|
+
|
32
|
+
def validate(key, value, validations, errors)
|
33
|
+
if value
|
34
|
+
::Croesus.validator_for(validations.validation).validate(key, value, validations.validation, errors)
|
35
|
+
errors.delete(key) if errors[key].respond_to?(:empty?) && errors[key].empty?
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Croesus.append_validator(Croesus::Validator::OptionalValidator.new)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
class Croesus::Validator::PresenceValidator < Croesus::Validator::Base
|
21
|
+
def initialize
|
22
|
+
super('required')
|
23
|
+
end
|
24
|
+
|
25
|
+
def presence_error_message
|
26
|
+
'is required'
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate(key, value, validations, errors)
|
30
|
+
unless value
|
31
|
+
errors[key] = presence_error_message
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
Croesus.append_validator(Croesus::Validator::PresenceValidator.new)
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
[
|
21
|
+
Array,
|
22
|
+
Complex,
|
23
|
+
Enumerable,
|
24
|
+
Float,
|
25
|
+
Integer,
|
26
|
+
Numeric,
|
27
|
+
Range,
|
28
|
+
Rational,
|
29
|
+
Regexp,
|
30
|
+
String,
|
31
|
+
Symbol,
|
32
|
+
Time
|
33
|
+
].each do |type|
|
34
|
+
name = type.to_s.gsub(/(.)([A-Z])/,'\1_\2').downcase
|
35
|
+
Croesus.append_validator(Croesus::Validator::SimpleValidator.new(
|
36
|
+
name, lambda { |v| v.is_a?(type) })
|
37
|
+
)
|
38
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
class Croesus::Validator::SimpleValidator < Croesus::Validator::Base
|
21
|
+
attr_accessor :lambda
|
22
|
+
|
23
|
+
def initialize(name, lambda)
|
24
|
+
# lambda must accept one argument (the value)
|
25
|
+
if lambda.arity != 1
|
26
|
+
raise ValidationError.new(
|
27
|
+
"Lambda should take only one argument; passed #{lambda.arity}"
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
super(name)
|
32
|
+
self.lambda = lambda
|
33
|
+
end
|
34
|
+
|
35
|
+
def validate(key, value, validations, errors)
|
36
|
+
unless lambda.call(value)
|
37
|
+
errors[key] = presence_error_message
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Author: Stefano Harding <riddopic@gmail.com>
|
4
|
+
#
|
5
|
+
# Copyright (C) 2014 Stefano Harding
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
16
|
+
# implied. See the License for the specific language governing
|
17
|
+
# permissions and limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
# The version number of the Croesus Gem
|
21
|
+
#
|
22
|
+
# @return [String]
|
23
|
+
#
|
24
|
+
# @api public
|
25
|
+
module Croesus
|
26
|
+
# Contains information about this gem's version
|
27
|
+
module Version
|
28
|
+
MAJOR = 0
|
29
|
+
MINOR = 1
|
30
|
+
PATCH = 3
|
31
|
+
|
32
|
+
# Returns a version string by joining MAJOR, MINOR, and PATCH with '.'
|
33
|
+
#
|
34
|
+
# Example
|
35
|
+
#
|
36
|
+
# Version.string # '1.0.3'
|
37
|
+
def self.string
|
38
|
+
[MAJOR, MINOR, PATCH].join('.')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
VERSION = Croesus::Version.string
|
43
|
+
end
|