require 'validators/interval_validator.rb'
require 'validators/range_overlapping_validator.rb'

class KalibroRange < ActiveRecord::Base
  belongs_to :reading
  belongs_to :metric_configuration

  validates :beginning, :end, :reading, :metric_configuration, presence: true
  validates :beginning, uniqueness: {scope: :metric_configuration_id, message: "Should be unique within a Metric Configuration"}
  validates :beginning, :end, numericality: true
  validates_with IntervalValidator, fields: [:beginning, :end], if: "beginning && self.end"
  validates_with RangeOverlappingValidator, fields: [:beginning, :end], if: "beginning && self.end"

  def as_json(options={})
    options[:except] = [:beginning, :end]
    json = super(options)
    json['beginning'] = (beginning == -Float::INFINITY ? "-INF" : (beginning == Float::INFINITY ? "INF" : beginning))
    json['end'] = (self.end == -Float::INFINITY ? "-INF" : (self.end == Float::INFINITY ? "INF" : self.end))

    return json
  end

  def beginning=(value)
    super(extract_infinity value)
  end

  def end=(value)
    super(extract_infinity value)
  end

  private

  def extract_infinity(parameter)
    if parameter == "INF"
      Float::INFINITY
    elsif parameter == "-INF"
      -Float::INFINITY
    else
      parameter
    end
  end
end

require 'validators/interval_validator.rb'
require 'validators/range_overlapping_validator.rb'

class KalibroRange < ActiveRecord::Base
  belongs_to :reading
  belongs_to :metric_configuration

  validates :beginning, :end, :reading, :metric_configuration, presence: true
  validates :beginning, uniqueness: {scope: :metric_configuration_id, message: "Should be unique within a Metric Configuration"}
  validates :beginning, :end, numericality: true
  validates_with IntervalValidator, fields: [:beginning, :end], if: "beginning && self.end && beginning.is_a?(Numeric) && self.end.is_a?(Numeric)"
  validates_with RangeOverlappingValidator, fields: [:beginning, :end], if: "beginning && self.end && beginning.is_a?(Numeric) && self.end.is_a?(Numeric)"

  def as_json(options={})
    options[:except] = [:beginning, :end]
    json = super(options)
    json['beginning'] = (beginning == -Float::INFINITY ? "-INF" : (beginning == Float::INFINITY ? "INF" : beginning))
    json['end'] = (self.end == -Float::INFINITY ? "-INF" : (self.end == Float::INFINITY ? "INF" : self.end))

    return json
  end

  def beginning=(value)
    super(extract_infinity value)
  end

  def end=(value)
    super(extract_infinity value)
  end

  private

  def extract_infinity(parameter)
    if parameter == "INF"
      Float::INFINITY
    elsif parameter == "-INF"
      -Float::INFINITY
    else
      parameter
    end
  end
end

Add constraints for running custom range validators
Add constraints for running custom range validators

In spite of having the numericality validator the IntervalValidator and
RangeOverlappingValidtor would always run, due to rails conventions.

When the beginning or end of the range were not numerical, an exception
was risen. We now prevent our custom validators to run when the
beginning or the end of the range are not numerical.
