Class: Aequitas::Rule::Numericalness::NonInteger
- Inherits:
-
Aequitas::Rule
- Object
- Aequitas::Rule
- Aequitas::Rule::Numericalness::NonInteger
- Includes:
- Aequitas::Rule::Numericalness
- Defined in:
- lib/aequitas/rule/numericalness/non_integer.rb
Instance Attribute Summary collapse
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#scale ⇒ Object
readonly
Returns the value of attribute scale.
Attributes inherited from Aequitas::Rule
#attribute_name, #custom_message, #guard, #skip_condition
Attributes included from Equalizable
Instance Method Summary collapse
- #expected(precision = @precision, scale = @scale) ⇒ Object
-
#initialize(attribute_name, options) ⇒ NonInteger
constructor
A new instance of NonInteger.
- #valid_numericalness?(value) ⇒ Boolean
- #violation_type(resource) ⇒ Object
Methods included from Aequitas::Rule::Numericalness
included, rules_for, scour_options_of_keys, #valid?
Methods inherited from Aequitas::Rule
#attribute_value, #execute?, rules_for, #skip?, #validate, #violation_data, #violation_info, #violation_values
Methods included from Equalizable
Constructor Details
#initialize(attribute_name, options) ⇒ NonInteger
Returns a new instance of NonInteger.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 15 def initialize(attribute_name, ) super @precision = .fetch(:precision, nil) @scale = .fetch(:scale, nil) unless expected # validate precision and scale attrs raise ArgumentError, "Invalid precision #{precision.inspect} and scale #{scale.inspect} for #{attribute_name}" end end |
Instance Attribute Details
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
12 13 14 |
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 12 def precision @precision end |
#scale ⇒ Object (readonly)
Returns the value of attribute scale.
13 14 15 |
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 13 def scale @scale end |
Instance Method Details
#expected(precision = @precision, scale = @scale) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 26 def expected(precision = @precision, scale = @scale) if precision && scale if precision > scale && scale == 0 /\A[+-]?(?:\d{1,#{precision}}(?:\.0)?)\z/ elsif precision > scale delta = precision - scale /\A[+-]?(?:\d{1,#{delta}}|\d{0,#{delta}}\.\d{1,#{scale}})\z/ elsif precision == scale /\A[+-]?(?:0(?:\.\d{1,#{scale}})?)\z/ else nil end else /\A[+-]?(?:\d+|\d*\.\d+)\z/ end end |
#valid_numericalness?(value) ⇒ Boolean
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 43 def valid_numericalness?(value) # XXX: workaround for jruby. This is needed because the jruby # compiler optimizes a bit too far with magic variables like $~. # the value.send line sends $~. Inserting this line makes sure the # jruby compiler does not optimise here. # see http://jira.codehaus.org/browse/JRUBY-3765 $~ = nil if RUBY_PLATFORM[/java/] value_as_string(value) =~ expected rescue ArgumentError # TODO: figure out better solution for: can't compare String with Integer true end |
#violation_type(resource) ⇒ Object
57 58 59 |
# File 'lib/aequitas/rule/numericalness/non_integer.rb', line 57 def violation_type(resource) :not_a_number end |