Class: Scale::Destination::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/scale/destination.rb

Overview

Contains logic for dealing with Ruby’s core ::Range as input

Instance Method Summary collapse

Constructor Details

#initialize(range) ⇒ Range

Returns a new instance of Range.

Parameters:

  • range (::Range)

    A range to operate with



12
13
14
# File 'lib/scale/destination.rb', line 12

def initialize(range)
  @range = range
end

Instance Method Details

#scale(input, source) ⇒ Numeric

Scale the given input and source using this destination

Parameters:

  • input (Numeric)

    A numeric value to scale

  • source (Scale::Source)

    The source for the input value

Returns:



20
21
22
23
24
25
26
27
28
29
# File 'lib/scale/destination.rb', line 20

def scale(input, source)
  to_range_len = (@range.last - @range.first).abs

  proportion = to_range_len.to_f / source.denominator
  abs_output = proportion.to_f * source.numerator(input)
  output = abs_output + @range.first

  float_requested = [@range.first, @range.last].any? { |n| n.kind_of?(::Float) }
  float_requested ? output : output.to_i
end