class Sapristi::AttributeNormalizer

Attributes

key[R]
monitor[R]
raw[R]

Public Class Methods

new(key, raw, monitor) click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 5
def initialize(key, raw, monitor)
  @key = key
  @raw = raw
  @monitor = monitor
end

Public Instance Methods

normalize() click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 11
def normalize
  if percentage?
    apply_percentage
  elsif not_a_percentage_but_includes_symbol?
    raise Error, "key=#{key}, invalid percentage=#{raw}"
  elsif numeric_field?
    raw&.to_i
  else
    raw
  end
end

Private Instance Methods

apply_percentage() click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 39
def apply_percentage
  validate_percentage_field

  (monitor_absolute * percentage - 1).to_i + offset
end
monitor_absolute() click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 58
def monitor_absolute
  translated_key = Definition::TRANSLATIONS[key]
  monitor[translated_key]
end
not_a_percentage_but_includes_symbol?() click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 31
def not_a_percentage_but_includes_symbol?
  raw.to_s.include?('%')
end
numeric_field?() click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 35
def numeric_field?
  Definition::NUMERIC_FIELDS.include?(key)
end
offset() click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 45
def offset
  work_area = monitor['work_area']

  case key
  when 'X'
    work_area[0]
  when 'Y'
    work_area[1]
  else
    0
  end
end
percentage() click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 63
def percentage
  value = raw.to_s.match(/^([0-9]+)%$/)[1].to_i
  value / 100.0
end
percentage?() click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 27
def percentage?
  raw&.to_s&.match(/^([0-9]+)%$/)
end
validate_percentage_field() click to toggle source
# File lib/sapristi/attribute_normalizer.rb, line 68
def validate_percentage_field
  min_percentage = { 'Height' => 0.05, 'Width' => 0.05 }.fetch(key, 0)
  unless Definition::TRANSLATIONS.include? key
    raise "#{key}=#{raw}, using percentage in invalid field, valid=#{Definition::TRANSLATIONS.keys.join(', ')}"
  end

  raise Error, "#{key} percentage is invalid=#{raw}, valid=5%-100%" if percentage < min_percentage || percentage > 1
end