aequitas 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/aequitas/contextual_rule_set.rb +23 -29
- data/lib/aequitas/macros.rb +6 -0
- data/lib/aequitas/message_transformer.rb +23 -8
- data/lib/aequitas/rule.rb +2 -23
- data/lib/aequitas/rule/block.rb +5 -0
- data/lib/aequitas/rule/format.rb +24 -28
- data/lib/aequitas/rule/{formats/email.rb → format/email_address.rb} +7 -3
- data/lib/aequitas/rule/format/proc.rb +4 -10
- data/lib/aequitas/rule/format/regexp.rb +4 -6
- data/lib/aequitas/rule/{formats → format}/url.rb +7 -3
- data/lib/aequitas/rule/guard.rb +2 -2
- data/lib/aequitas/rule/length.rb +29 -30
- data/lib/aequitas/rule/length/equal.rb +4 -4
- data/lib/aequitas/rule/length/maximum.rb +4 -6
- data/lib/aequitas/rule/length/minimum.rb +4 -6
- data/lib/aequitas/rule/length/range.rb +4 -6
- data/lib/aequitas/rule/method.rb +6 -1
- data/lib/aequitas/rule/numericalness.rb +19 -40
- data/lib/aequitas/rule/numericalness/integer.rb +3 -19
- data/lib/aequitas/rule/numericalness/non_integer.rb +2 -18
- data/lib/aequitas/rule/primitive_type.rb +17 -6
- data/lib/aequitas/rule/primitive_type/virtus.rb +25 -0
- data/lib/aequitas/rule/skip_condition.rb +2 -2
- data/lib/aequitas/rule/value.rb +64 -0
- data/lib/aequitas/rule/{numericalness → value}/equal.rb +5 -7
- data/lib/aequitas/rule/{numericalness → value}/greater_than.rb +5 -7
- data/lib/aequitas/rule/{numericalness → value}/greater_than_or_equal.rb +5 -7
- data/lib/aequitas/rule/{numericalness → value}/less_than.rb +5 -7
- data/lib/aequitas/rule/{numericalness → value}/less_than_or_equal.rb +5 -7
- data/lib/aequitas/rule/{numericalness → value}/not_equal.rb +5 -7
- data/lib/aequitas/rule/value/range.rb +25 -0
- data/lib/aequitas/rule/within.rb +25 -13
- data/lib/aequitas/rule_set.rb +2 -2
- data/lib/aequitas/support/{equalizable.rb → value_object.rb} +2 -3
- data/lib/aequitas/version.rb +1 -1
- data/lib/aequitas/violation.rb +4 -4
- data/lib/aequitas/violation_set.rb +2 -3
- data/lib/aequitas/virtus_integration.rb +31 -0
- data/lib/aequitas/virtus_integration/inline_attribute_rule_extractor.rb +30 -0
- data/lib/aequitas/virtus_integration/inline_attribute_rule_extractor/array.rb +29 -0
- data/lib/aequitas/virtus_integration/inline_attribute_rule_extractor/boolean.rb +17 -0
- data/lib/aequitas/virtus_integration/inline_attribute_rule_extractor/numeric.rb +22 -0
- data/lib/aequitas/virtus_integration/inline_attribute_rule_extractor/object.rb +38 -0
- data/lib/aequitas/virtus_integration/inline_attribute_rule_extractor/string.rb +35 -0
- data/spec/integration/aequitas/macros/validates_numericalness_of_spec.rb +77 -8
- data/spec/integration/aequitas/macros/validates_value_of_spec.rb +27 -0
- data/spec/integration/aequitas/macros/validates_within.rb +10 -74
- data/spec/integration/shared/macros/integration_spec.rb +10 -0
- data/spec/integration/virtus/array/length/equal_spec.rb +49 -0
- data/spec/integration/virtus/array/length/range_spec.rb +59 -0
- data/spec/integration/virtus/boolean/presence_spec.rb +4 -4
- data/spec/integration/virtus/integer/value/equal_to_spec.rb +45 -0
- data/spec/integration/virtus/integer/value/greater_than_or_equal.rb +45 -0
- data/spec/integration/virtus/integer/value/greater_than_spec.rb +45 -0
- data/spec/integration/virtus/integer/value/less_than_or_equal.rb +45 -0
- data/spec/integration/virtus/integer/value/less_than_spec.rb +45 -0
- data/spec/integration/virtus/integer/value/not_equal_to_spec.rb +45 -0
- data/spec/integration/virtus/string/format/email_address_spec.rb +3 -3
- data/spec/integration/virtus/string/format/regexp_spec.rb +2 -2
- data/spec/integration/virtus/string/format/url_spec.rb +2 -2
- data/spec/integration/virtus/string/length/equal_spec.rb +3 -3
- data/spec/integration/virtus/string/length/range_spec.rb +2 -2
- data/spec/integration/virtus/string/presence_spec.rb +2 -2
- data/spec/unit/aequitas/support/{equalizable → value_object}/equalizer_spec.rb +4 -4
- data/spec/unit/aequitas/support/{equalizable_spec.rb → value_object_spec.rb} +11 -11
- data/spec/unit/aequitas/violation_set_spec.rb +1 -1
- metadata +46 -149
- data/.gitignore +0 -4
- data/.rvmrc +0 -1
- data/aequitas.gemspec +0 -20
- data/lib/aequitas/rule/within/range.rb +0 -53
- data/lib/aequitas/rule/within/range/bounded.rb +0 -25
- data/lib/aequitas/rule/within/range/unbounded_begin.rb +0 -25
- data/lib/aequitas/rule/within/range/unbounded_end.rb +0 -25
- data/lib/aequitas/rule/within/set.rb +0 -39
- data/lib/aequitas/virtus.rb +0 -29
- data/lib/aequitas/virtus/inline_attribute_rule_extractor.rb +0 -41
- data/lib/aequitas/virtus/inline_attribute_rule_extractor/boolean.rb +0 -17
- data/lib/aequitas/virtus/inline_attribute_rule_extractor/object.rb +0 -25
- data/lib/aequitas/virtus/inline_attribute_rule_extractor/string.rb +0 -27
- data/spec_legacy/fixtures/barcode.rb +0 -40
- data/spec_legacy/fixtures/basketball_court.rb +0 -58
- data/spec_legacy/fixtures/basketball_player.rb +0 -34
- data/spec_legacy/fixtures/beta_tester_account.rb +0 -33
- data/spec_legacy/fixtures/bill_of_landing.rb +0 -47
- data/spec_legacy/fixtures/boat_dock.rb +0 -26
- data/spec_legacy/fixtures/city.rb +0 -24
- data/spec_legacy/fixtures/company.rb +0 -93
- data/spec_legacy/fixtures/corporate_world.rb +0 -39
- data/spec_legacy/fixtures/country.rb +0 -24
- data/spec_legacy/fixtures/ethernet_frame.rb +0 -56
- data/spec_legacy/fixtures/event.rb +0 -44
- data/spec_legacy/fixtures/g3_concert.rb +0 -57
- data/spec_legacy/fixtures/jabberwock.rb +0 -27
- data/spec_legacy/fixtures/kayak.rb +0 -28
- data/spec_legacy/fixtures/lernean_hydra.rb +0 -39
- data/spec_legacy/fixtures/llama_spaceship.rb +0 -15
- data/spec_legacy/fixtures/mathematical_function.rb +0 -34
- data/spec_legacy/fixtures/memory_object.rb +0 -30
- data/spec_legacy/fixtures/mittelschnauzer.rb +0 -39
- data/spec_legacy/fixtures/motor_launch.rb +0 -21
- data/spec_legacy/fixtures/multibyte.rb +0 -16
- data/spec_legacy/fixtures/page.rb +0 -32
- data/spec_legacy/fixtures/phone_number.rb +0 -28
- data/spec_legacy/fixtures/pirogue.rb +0 -28
- data/spec_legacy/fixtures/programming_language.rb +0 -83
- data/spec_legacy/fixtures/reservation.rb +0 -38
- data/spec_legacy/fixtures/scm_operation.rb +0 -56
- data/spec_legacy/fixtures/sms_message.rb +0 -22
- data/spec_legacy/fixtures/udp_packet.rb +0 -49
- data/spec_legacy/integration/absent_field_validator/absent_field_validator_spec.rb +0 -90
- data/spec_legacy/integration/absent_field_validator/spec_helper.rb +0 -7
- data/spec_legacy/integration/acceptance_validator/acceptance_validator_spec.rb +0 -196
- data/spec_legacy/integration/acceptance_validator/spec_helper.rb +0 -7
- data/spec_legacy/integration/automatic_validation/custom_messages_for_inferred_validation_spec.rb +0 -57
- data/spec_legacy/integration/automatic_validation/disabling_inferred_validation_spec.rb +0 -49
- data/spec_legacy/integration/automatic_validation/inferred_boolean_properties_validation_spec.rb +0 -100
- data/spec_legacy/integration/automatic_validation/inferred_float_property_validation_spec.rb +0 -45
- data/spec_legacy/integration/automatic_validation/inferred_format_validation_spec.rb +0 -35
- data/spec_legacy/integration/automatic_validation/inferred_integer_properties_validation_spec.rb +0 -70
- data/spec_legacy/integration/automatic_validation/inferred_length_validation_spec.rb +0 -142
- data/spec_legacy/integration/automatic_validation/inferred_presence_validation_spec.rb +0 -45
- data/spec_legacy/integration/automatic_validation/inferred_primitive_validation_spec.rb +0 -22
- data/spec_legacy/integration/automatic_validation/inferred_uniqueness_validation_spec.rb +0 -48
- data/spec_legacy/integration/automatic_validation/inferred_within_validation_spec.rb +0 -35
- data/spec_legacy/integration/automatic_validation/spec_helper.rb +0 -57
- data/spec_legacy/integration/block_validator/block_validator_spec.rb +0 -32
- data/spec_legacy/integration/block_validator/spec_helper.rb +0 -5
- data/spec_legacy/integration/conditional_validation/if_condition_spec.rb +0 -63
- data/spec_legacy/integration/conditional_validation/spec_helper.rb +0 -5
- data/spec_legacy/integration/confirmation_validator/confirmation_validator_spec.rb +0 -76
- data/spec_legacy/integration/confirmation_validator/spec_helper.rb +0 -5
- data/spec_legacy/integration/datamapper_models/association_validation_spec.rb +0 -29
- data/spec_legacy/integration/datamapper_models/inheritance_spec.rb +0 -82
- data/spec_legacy/integration/dirty_attributes/dirty_attributes_spec.rb +0 -13
- data/spec_legacy/integration/duplicated_validations/duplicated_validations_spec.rb +0 -24
- data/spec_legacy/integration/duplicated_validations/spec_helper.rb +0 -5
- data/spec_legacy/integration/format_validator/email_format_validator_spec.rb +0 -139
- data/spec_legacy/integration/format_validator/format_validator_spec.rb +0 -64
- data/spec_legacy/integration/format_validator/regexp_validator_spec.rb +0 -33
- data/spec_legacy/integration/format_validator/spec_helper.rb +0 -5
- data/spec_legacy/integration/format_validator/url_format_validator_spec.rb +0 -93
- data/spec_legacy/integration/length_validator/default_value_spec.rb +0 -14
- data/spec_legacy/integration/length_validator/equality_spec.rb +0 -87
- data/spec_legacy/integration/length_validator/error_message_spec.rb +0 -22
- data/spec_legacy/integration/length_validator/maximum_spec.rb +0 -49
- data/spec_legacy/integration/length_validator/minimum_spec.rb +0 -54
- data/spec_legacy/integration/length_validator/range_spec.rb +0 -87
- data/spec_legacy/integration/length_validator/spec_helper.rb +0 -7
- data/spec_legacy/integration/method_validator/method_validator_spec.rb +0 -242
- data/spec_legacy/integration/method_validator/spec_helper.rb +0 -5
- data/spec_legacy/integration/numeric_validator/equality_with_float_type_spec.rb +0 -65
- data/spec_legacy/integration/numeric_validator/equality_with_integer_type_spec.rb +0 -41
- data/spec_legacy/integration/numeric_validator/float_type_spec.rb +0 -90
- data/spec_legacy/integration/numeric_validator/gt_with_float_type_spec.rb +0 -37
- data/spec_legacy/integration/numeric_validator/gte_with_float_type_spec.rb +0 -37
- data/spec_legacy/integration/numeric_validator/integer_only_true_spec.rb +0 -91
- data/spec_legacy/integration/numeric_validator/integer_type_spec.rb +0 -86
- data/spec_legacy/integration/numeric_validator/lt_with_float_type_spec.rb +0 -37
- data/spec_legacy/integration/numeric_validator/lte_with_float_type_spec.rb +0 -37
- data/spec_legacy/integration/numeric_validator/spec_helper.rb +0 -5
- data/spec_legacy/integration/primitive_validator/primitive_validator_spec.rb +0 -92
- data/spec_legacy/integration/primitive_validator/spec_helper.rb +0 -5
- data/spec_legacy/integration/pure_ruby_objects/plain_old_ruby_object_validation_spec.rb +0 -118
- data/spec_legacy/integration/required_field_validator/association_spec.rb +0 -69
- data/spec_legacy/integration/required_field_validator/boolean_type_value_spec.rb +0 -164
- data/spec_legacy/integration/required_field_validator/date_type_value_spec.rb +0 -127
- data/spec_legacy/integration/required_field_validator/datetime_type_value_spec.rb +0 -127
- data/spec_legacy/integration/required_field_validator/float_type_value_spec.rb +0 -131
- data/spec_legacy/integration/required_field_validator/integer_type_value_spec.rb +0 -99
- data/spec_legacy/integration/required_field_validator/plain_old_ruby_object_spec.rb +0 -35
- data/spec_legacy/integration/required_field_validator/shared_examples.rb +0 -26
- data/spec_legacy/integration/required_field_validator/spec_helper.rb +0 -7
- data/spec_legacy/integration/required_field_validator/string_type_value_spec.rb +0 -167
- data/spec_legacy/integration/required_field_validator/text_type_value_spec.rb +0 -49
- data/spec_legacy/integration/shared/default_validation_context.rb +0 -13
- data/spec_legacy/integration/shared/valid_and_invalid_model.rb +0 -35
- data/spec_legacy/integration/uniqueness_validator/spec_helper.rb +0 -5
- data/spec_legacy/integration/uniqueness_validator/uniqueness_validator_spec.rb +0 -116
- data/spec_legacy/integration/within_validator/spec_helper.rb +0 -5
- data/spec_legacy/integration/within_validator/within_validator_spec.rb +0 -168
- data/spec_legacy/public/resource_spec.rb +0 -105
- data/spec_legacy/spec.opts +0 -4
- data/spec_legacy/spec_helper.rb +0 -29
- data/spec_legacy/unit/contextual_validators/emptiness_spec.rb +0 -50
- data/spec_legacy/unit/contextual_validators/execution_spec.rb +0 -48
- data/spec_legacy/unit/contextual_validators/spec_helper.rb +0 -37
- data/spec_legacy/unit/generic_validator/equality_operator_spec.rb +0 -26
- data/spec_legacy/unit/generic_validator/optional_spec.rb +0 -54
- data/spec_legacy/unit/validators/within_validator_spec.rb +0 -23
- data/spec_legacy/unit/violation_set/adding_spec.rb +0 -54
- data/spec_legacy/unit/violation_set/emptiness_spec.rb +0 -38
- data/spec_legacy/unit/violation_set/enumerable_spec.rb +0 -32
- data/spec_legacy/unit/violation_set/reading_spec.rb +0 -35
- data/spec_legacy/unit/violation_set/respond_to_spec.rb +0 -15
- data/tasks/spec.rake +0 -38
- data/tasks/yard.rake +0 -9
- data/tasks/yardstick.rake +0 -19
@@ -79,7 +79,7 @@ module Aequitas
|
|
79
79
|
# @param [Symbol] attribute_name
|
80
80
|
# The name of the field you want an violation for.
|
81
81
|
#
|
82
|
-
# @return [Array(Violation, String)
|
82
|
+
# @return [Array(Violation, String)]
|
83
83
|
# Array of Violations, if there are violations on +attribute_name+
|
84
84
|
# nil if there are no violations on +attribute_name+
|
85
85
|
#
|
@@ -87,8 +87,7 @@ module Aequitas
|
|
87
87
|
#
|
88
88
|
# TODO: use a data structure that ensures uniqueness
|
89
89
|
def on(attribute_name)
|
90
|
-
|
91
|
-
attribute_violations.empty? ? nil : attribute_violations.uniq
|
90
|
+
violations[attribute_name].uniq
|
92
91
|
end
|
93
92
|
|
94
93
|
# @api public
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'aequitas'
|
2
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor'
|
3
|
+
|
4
|
+
# module Virtus
|
5
|
+
# class Attribute
|
6
|
+
# Object.accept_options :required# , :prohibited, :forbidden # hmm...
|
7
|
+
# String.accept_options :length, :format
|
8
|
+
# Decimal.accept_options :precision, :scale
|
9
|
+
# Float.accept_options :precision, :scale
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
|
13
|
+
module Aequitas
|
14
|
+
module ClassMethods
|
15
|
+
def self.extended(base)
|
16
|
+
super
|
17
|
+
base.extend Aequitas::VirtusIntegration::ClassMethods
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module VirtusIntegration
|
22
|
+
module ClassMethods
|
23
|
+
# WARNING: overriding Virtus' private API here, this is fragile
|
24
|
+
def virtus_add_attribute(attribute)
|
25
|
+
super
|
26
|
+
inline_attribute_rules = InlineAttributeRuleExtractor.extract(attribute)
|
27
|
+
validation_rules.context(:default).concat(inline_attribute_rules)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
module Aequitas
|
4
|
+
module VirtusIntegration
|
5
|
+
module InlineAttributeRuleExtractor
|
6
|
+
|
7
|
+
def self.extract(attribute)
|
8
|
+
type =
|
9
|
+
case attribute
|
10
|
+
when ::Virtus::Attribute::Boolean; self::Boolean
|
11
|
+
when ::Virtus::Attribute::String; self::String
|
12
|
+
when ::Virtus::Attribute::Array; self::Array
|
13
|
+
# when ::Virtus::Attribute::Decimal; self::Decimal
|
14
|
+
# when ::Virtus::Attribute::Float; self::Float
|
15
|
+
when ::Virtus::Attribute::Numeric; self::Numeric
|
16
|
+
when ::Virtus::Attribute::Object; self::Object
|
17
|
+
end
|
18
|
+
|
19
|
+
type.new(attribute).extract
|
20
|
+
end
|
21
|
+
|
22
|
+
end # module InlineAttributeRuleExtractor
|
23
|
+
end # module VirtusIntegration
|
24
|
+
end # module Aequitas
|
25
|
+
|
26
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor/object'
|
27
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor/array'
|
28
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor/boolean'
|
29
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor/string'
|
30
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor/numeric'
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor/object'
|
4
|
+
|
5
|
+
module Aequitas
|
6
|
+
module VirtusIntegration
|
7
|
+
module InlineAttributeRuleExtractor
|
8
|
+
class Array < Object
|
9
|
+
|
10
|
+
def extract
|
11
|
+
rules = super
|
12
|
+
rules.concat Array(extract_length_rules)
|
13
|
+
end
|
14
|
+
|
15
|
+
def extract_length_rules
|
16
|
+
length = options.fetch(:length, false)
|
17
|
+
|
18
|
+
case length
|
19
|
+
when ::Integer; Rule::Length::Equal.new(attribute.name, :expected => length)
|
20
|
+
when ::Range; Rule::Length::Range.new(attribute.name, :range => length)
|
21
|
+
when ::FalseClass;
|
22
|
+
else raise ArgumentError, "expected Integer or Range :length, got: #{length.inspect}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end # class Array
|
27
|
+
end # module InlineAttributeRuleExtractor
|
28
|
+
end # module VirtusIntegration
|
29
|
+
end # module Aequitas
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor/object'
|
4
|
+
|
5
|
+
module Aequitas
|
6
|
+
module VirtusIntegration
|
7
|
+
module InlineAttributeRuleExtractor
|
8
|
+
class Boolean < Object
|
9
|
+
|
10
|
+
def extract_presence_rule
|
11
|
+
Rule::Presence::NotNil.new(attribute.name)
|
12
|
+
end
|
13
|
+
|
14
|
+
end # class Boolean
|
15
|
+
end # module InlineAttributeRuleExtractor
|
16
|
+
end # module VirtusIntegration
|
17
|
+
end # module Aequitas
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor/object'
|
4
|
+
|
5
|
+
module Aequitas
|
6
|
+
module VirtusIntegration
|
7
|
+
module InlineAttributeRuleExtractor
|
8
|
+
class Numeric < Object
|
9
|
+
|
10
|
+
def extract
|
11
|
+
rules = super
|
12
|
+
rules.concat Array(extract_value_rules)
|
13
|
+
end
|
14
|
+
|
15
|
+
def extract_value_rules
|
16
|
+
Rule::Value.rules_for(attribute.name, options)
|
17
|
+
end
|
18
|
+
|
19
|
+
end # class Numeric
|
20
|
+
end # module InlineAttributeRuleExtractor
|
21
|
+
end # module VirtusIntegration
|
22
|
+
end # module Aequitas
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor'
|
4
|
+
|
5
|
+
module Aequitas
|
6
|
+
module VirtusIntegration
|
7
|
+
module InlineAttributeRuleExtractor
|
8
|
+
class Object
|
9
|
+
|
10
|
+
attr_reader :attribute
|
11
|
+
|
12
|
+
def initialize(attribute)
|
13
|
+
@attribute = attribute
|
14
|
+
end
|
15
|
+
|
16
|
+
def options
|
17
|
+
attribute.options
|
18
|
+
end
|
19
|
+
|
20
|
+
def extract
|
21
|
+
inline_rules = []
|
22
|
+
inline_rules.concat Array(extract_presence_rule)
|
23
|
+
# inline_rules.concat Array(extract_primitive_type_rule)
|
24
|
+
end
|
25
|
+
|
26
|
+
def extract_presence_rule
|
27
|
+
required = options.fetch(:required, false)
|
28
|
+
Rule::Presence::NotBlank.new(attribute.name) if required
|
29
|
+
end
|
30
|
+
|
31
|
+
def extract_primitive_type_rule
|
32
|
+
Rule::PrimitiveType::Virtus.new(attribute.name, :attribute => attribute)
|
33
|
+
end
|
34
|
+
|
35
|
+
end # class Object
|
36
|
+
end # module InlineAttributeRuleExtractor
|
37
|
+
end # module VirtusIntegration
|
38
|
+
end # module Aequitas
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'aequitas/virtus_integration/inline_attribute_rule_extractor/object'
|
4
|
+
|
5
|
+
module Aequitas
|
6
|
+
module VirtusIntegration
|
7
|
+
module InlineAttributeRuleExtractor
|
8
|
+
class String < Object
|
9
|
+
|
10
|
+
def extract
|
11
|
+
rules = super
|
12
|
+
rules.concat(Array(extract_length_rules))
|
13
|
+
rules.concat(Array(extract_format_rules))
|
14
|
+
end
|
15
|
+
|
16
|
+
def extract_length_rules
|
17
|
+
length = options.fetch(:length, false)
|
18
|
+
|
19
|
+
case length
|
20
|
+
when ::Integer; Rule::Length::Equal.new(attribute.name, :expected => length)
|
21
|
+
when ::Range; Rule::Length::Range.new(attribute.name, :range => length)
|
22
|
+
when ::FalseClass;
|
23
|
+
else raise ArgumentError, "expected Integer or Range :length, got: #{length.inspect}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def extract_format_rules
|
28
|
+
format = options.fetch(:format, false)
|
29
|
+
Rule::Format.rules_for(attribute.name, :with => format) if format
|
30
|
+
end
|
31
|
+
|
32
|
+
end # class String
|
33
|
+
end # module InlineAttributeRuleExtractor
|
34
|
+
end # module VirtusIntegration
|
35
|
+
end # module Aequitas
|
@@ -8,7 +8,7 @@ Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_numeric
|
|
8
8
|
class_under_test.validates_numericalness_of attribute_name
|
9
9
|
end
|
10
10
|
|
11
|
-
describe 'when validated attribute is a
|
11
|
+
describe 'when validated attribute is a float' do
|
12
12
|
let(:attribute_value) { 1.0 }
|
13
13
|
|
14
14
|
it_should_be_a_valid_instance
|
@@ -38,7 +38,13 @@ Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_numeric
|
|
38
38
|
it_should_be_a_valid_instance
|
39
39
|
end
|
40
40
|
|
41
|
-
describe 'when validated attribute is
|
41
|
+
describe 'when validated attribute is a float' do
|
42
|
+
let(:attribute_value) { 1.0 }
|
43
|
+
|
44
|
+
it_should_be_an_invalid_instance
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'when validated attribute is non-numeric' do
|
42
48
|
let(:attribute_value) { 'a' }
|
43
49
|
|
44
50
|
it_should_be_an_invalid_instance
|
@@ -52,7 +58,7 @@ Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_numeric
|
|
52
58
|
|
53
59
|
let(:bound) { 1 }
|
54
60
|
let(:class_of_violated_validation_rule) do
|
55
|
-
Aequitas::Rule::
|
61
|
+
Aequitas::Rule::Value::Equal
|
56
62
|
end
|
57
63
|
|
58
64
|
describe 'when validated attribute is greater than bound' do
|
@@ -81,7 +87,7 @@ Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_numeric
|
|
81
87
|
|
82
88
|
let(:bound) { 1 }
|
83
89
|
let(:class_of_violated_validation_rule) do
|
84
|
-
Aequitas::Rule::
|
90
|
+
Aequitas::Rule::Value::NotEqual
|
85
91
|
end
|
86
92
|
|
87
93
|
describe 'when validated attribute is greater than bound' do
|
@@ -110,7 +116,7 @@ Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_numeric
|
|
110
116
|
|
111
117
|
let(:bound) { 1 }
|
112
118
|
let(:class_of_violated_validation_rule) do
|
113
|
-
Aequitas::Rule::
|
119
|
+
Aequitas::Rule::Value::GreaterThan
|
114
120
|
end
|
115
121
|
|
116
122
|
describe 'when validated attribute is greater than bound' do
|
@@ -139,7 +145,7 @@ Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_numeric
|
|
139
145
|
|
140
146
|
let(:bound) { 1 }
|
141
147
|
let(:class_of_violated_validation_rule) do
|
142
|
-
Aequitas::Rule::
|
148
|
+
Aequitas::Rule::Value::LessThan
|
143
149
|
end
|
144
150
|
|
145
151
|
describe 'when validated attribute is greater than bound' do
|
@@ -168,7 +174,7 @@ Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_numeric
|
|
168
174
|
|
169
175
|
let(:bound) { 1 }
|
170
176
|
let(:class_of_violated_validation_rule) do
|
171
|
-
Aequitas::Rule::
|
177
|
+
Aequitas::Rule::Value::GreaterThanOrEqual
|
172
178
|
end
|
173
179
|
|
174
180
|
describe 'when validated attribute is greater than bound' do
|
@@ -197,7 +203,7 @@ Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_numeric
|
|
197
203
|
|
198
204
|
let(:bound) { 1 }
|
199
205
|
let(:class_of_violated_validation_rule) do
|
200
|
-
Aequitas::Rule::
|
206
|
+
Aequitas::Rule::Value::LessThanOrEqual
|
201
207
|
end
|
202
208
|
|
203
209
|
describe 'when validated attribute is greater than bound' do
|
@@ -218,4 +224,67 @@ Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_numeric
|
|
218
224
|
it_should_be_a_valid_instance
|
219
225
|
end
|
220
226
|
end
|
227
|
+
|
228
|
+
describe 'with :in or :within options' do
|
229
|
+
describe 'with lower and upper bounds' do
|
230
|
+
before do
|
231
|
+
class_under_test.validates_numericalness_of attribute_name, :in => bound
|
232
|
+
end
|
233
|
+
|
234
|
+
let(:bound) { 1..5 }
|
235
|
+
|
236
|
+
describe 'when validated attribute value is within the range' do
|
237
|
+
let(:attribute_value) { bound.begin + 1 }
|
238
|
+
|
239
|
+
it_should_be_a_valid_instance
|
240
|
+
end
|
241
|
+
|
242
|
+
describe 'when validated attribute value is not within the range' do
|
243
|
+
let(:attribute_value) { bound.end + 1 }
|
244
|
+
|
245
|
+
it_should_be_an_invalid_instance
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
# describe 'with no lower bound' do
|
250
|
+
# before do
|
251
|
+
# class_under_test.validates_within attribute_name, :range => range
|
252
|
+
# end
|
253
|
+
#
|
254
|
+
# let(:range) { -(1.0/0)..5 }
|
255
|
+
#
|
256
|
+
# describe 'when validated attribute value is within the range' do
|
257
|
+
# let(:attribute_value) { range.end - 1 }
|
258
|
+
#
|
259
|
+
# it_should_be_a_valid_instance
|
260
|
+
# end
|
261
|
+
#
|
262
|
+
# describe 'when validated attribute value is not within the range' do
|
263
|
+
# let(:attribute_value) { range.end + 1 }
|
264
|
+
#
|
265
|
+
# it_should_be_an_invalid_instance
|
266
|
+
# end
|
267
|
+
# end
|
268
|
+
|
269
|
+
# describe 'with no upper bound' do
|
270
|
+
# before do
|
271
|
+
# class_under_test.validates_within attribute_name, :range => range
|
272
|
+
# end
|
273
|
+
#
|
274
|
+
# let(:range) { 1..(1.0/0) }
|
275
|
+
#
|
276
|
+
# describe 'when validated attribute value is within the range' do
|
277
|
+
# let(:attribute_value) { range.begin + 1 }
|
278
|
+
#
|
279
|
+
# it_should_be_a_valid_instance
|
280
|
+
# end
|
281
|
+
#
|
282
|
+
# describe 'when validated attribute value is not within the range' do
|
283
|
+
# let(:attribute_value) { range.begin - 1 }
|
284
|
+
#
|
285
|
+
# it_should_be_an_invalid_instance
|
286
|
+
# end
|
287
|
+
# end
|
288
|
+
end
|
289
|
+
|
221
290
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative '../../../spec_helper'
|
2
|
+
require_relative '../../shared/macros/integration_spec'
|
3
|
+
require 'aequitas'
|
4
|
+
|
5
|
+
Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_value_of' do
|
6
|
+
require 'date'
|
7
|
+
|
8
|
+
describe 'with a lambda that returns lower and upper bounds' do
|
9
|
+
before do
|
10
|
+
class_under_test.validates_value_of attribute_name, :in => bound
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:bound) { lambda { (Date.today - 5)..Date.today } }
|
14
|
+
|
15
|
+
describe 'when validated attribute value is within the range' do
|
16
|
+
let(:attribute_value) { bound.call.begin + 1 }
|
17
|
+
|
18
|
+
it_should_be_a_valid_instance
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'when validated attribute value is not within the range' do
|
22
|
+
let(:attribute_value) { bound.call.end + 1 }
|
23
|
+
|
24
|
+
it_should_be_an_invalid_instance
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -3,85 +3,21 @@ require_relative '../../shared/macros/integration_spec'
|
|
3
3
|
require 'aequitas'
|
4
4
|
|
5
5
|
Aequitas::Macros::IntegrationSpec.describe Aequitas::Macros, '#validates_within' do
|
6
|
-
|
7
|
-
|
8
|
-
before do
|
9
|
-
class_under_test.validates_within attribute_name, :set => set
|
10
|
-
end
|
11
|
-
|
12
|
-
let(:set) { 1..5 }
|
13
|
-
|
14
|
-
describe 'when validated attribute value is within the range' do
|
15
|
-
let(:attribute_value) { set.begin + 1 }
|
16
|
-
|
17
|
-
it_should_be_a_valid_instance
|
18
|
-
end
|
19
|
-
|
20
|
-
describe 'when validated attribute value is not within the range' do
|
21
|
-
let(:attribute_value) { set.end + 1 }
|
22
|
-
|
23
|
-
it_should_be_an_invalid_instance
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
describe 'with no lower bound' do
|
28
|
-
before do
|
29
|
-
class_under_test.validates_within attribute_name, :set => set
|
30
|
-
end
|
31
|
-
|
32
|
-
let(:set) { -(1.0/0)..5 }
|
33
|
-
|
34
|
-
describe 'when validated attribute value is within the range' do
|
35
|
-
let(:attribute_value) { set.end - 1 }
|
36
|
-
|
37
|
-
it_should_be_a_valid_instance
|
38
|
-
end
|
39
|
-
|
40
|
-
describe 'when validated attribute value is not within the range' do
|
41
|
-
let(:attribute_value) { set.end + 1 }
|
42
|
-
|
43
|
-
it_should_be_an_invalid_instance
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe 'with no upper bound' do
|
48
|
-
before do
|
49
|
-
class_under_test.validates_within attribute_name, :set => set
|
50
|
-
end
|
51
|
-
|
52
|
-
let(:set) { 1..(1.0/0) }
|
53
|
-
|
54
|
-
describe 'when validated attribute value is within the range' do
|
55
|
-
let(:attribute_value) { set.begin + 1 }
|
56
|
-
|
57
|
-
it_should_be_a_valid_instance
|
58
|
-
end
|
59
|
-
|
60
|
-
describe 'when validated attribute value is not within the range' do
|
61
|
-
let(:attribute_value) { set.begin - 1 }
|
62
|
-
|
63
|
-
it_should_be_an_invalid_instance
|
64
|
-
end
|
65
|
-
end
|
6
|
+
before do
|
7
|
+
class_under_test.validates_within attribute_name, :set => Set[*set]
|
66
8
|
end
|
67
9
|
|
68
|
-
|
69
|
-
before do
|
70
|
-
class_under_test.validates_within attribute_name, :set => Set[*set]
|
71
|
-
end
|
10
|
+
let(:set) { [:a, :b, :c] }
|
72
11
|
|
73
|
-
|
12
|
+
describe 'when validated attribute value is included in the set' do
|
13
|
+
let(:attribute_value) { set.first }
|
74
14
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
it_should_be_a_valid_instance
|
79
|
-
end
|
15
|
+
it_should_be_a_valid_instance
|
16
|
+
end
|
80
17
|
|
81
|
-
|
82
|
-
|
18
|
+
describe 'when validated attribute value is not included in the set' do
|
19
|
+
let(:attribute_value) { :foo }
|
83
20
|
|
84
|
-
|
85
|
-
end
|
21
|
+
it_should_be_an_invalid_instance
|
86
22
|
end
|
87
23
|
end
|