Class: Scale::Scheme

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

Overview

Describes a particular scaling scenario

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Scheme

Returns a new instance of Scheme.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :destination (::Enumerable)

    The destination for this scaling scenario

  • :source (::Enumerable)

    The source for this scaling scenario



43
44
45
46
47
# File 'lib/scale/scheme.rb', line 43

def initialize(options = {})
  @input = options[:input]
  @source = Source.new(options[:source]) unless options[:source].nil?
  @destination = Destination.new(options[:destination]) unless options[:destination].nil?
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



38
39
40
# File 'lib/scale/scheme.rb', line 38

def destination
  @destination
end

#inputObject (readonly)

Returns the value of attribute input.



38
39
40
# File 'lib/scale/scheme.rb', line 38

def input
  @input
end

#sourceObject (readonly)

Returns the value of attribute source.



38
39
40
# File 'lib/scale/scheme.rb', line 38

def source
  @source
end

Instance Method Details

#from(source) ⇒ Numeric, Scale::Scheme

Set the source for this scaling scenario. If on calling this method, the scenario has all of its needed properties, the scaled value will be returned. Otherwise this method will return the updated Scheme object.

Parameters:

  • source (::Enumerable)

Returns:



55
56
57
58
# File 'lib/scale/scheme.rb', line 55

def from(source)
  @source = Source.new(source)
  scale? ? result : self
end

#resultNumeric?

Get the result of this scaling scenario

Returns:



105
106
107
# File 'lib/scale/scheme.rb', line 105

def result
  @result ||= @destination.scale(@input, @source)
end

#scale(number) ⇒ Numeric, Scale::Scheme Also known as: transform

Set the input of this scaling scenario. If on calling this method, the scenario has all of its needed properties, the scaled value will be returned.

Otherwise this method will return the updated Scheme object.

Parameters:

Returns:



91
92
93
94
# File 'lib/scale/scheme.rb', line 91

def scale(number)
  @input = number
  scale? ? result : self
end

#scale?Boolean

Scan this scaling scenario be run?

Returns:

  • (Boolean)

    Whether this scaling scenario can be run



99
100
101
# File 'lib/scale/scheme.rb', line 99

def scale?
  !@input.nil? && !@source.nil? && !@destination.nil?
end

#to(destination) ⇒ Numeric, Scale::Scheme

Set the destination for this scaling scenario. If on calling this method, the scenario has all of its needed properties, the scaled value will be returned.

Otherwise this method will return the updated Scheme object.

Parameters:

  • destination (::Enumerable)

Returns:



66
67
68
69
# File 'lib/scale/scheme.rb', line 66

def to(destination)
  @destination = Destination.new(destination)
  scale? ? result : self
end

#using(source, destination) ⇒ Numeric, Scale::Scheme

Set both the source and destination on this scheme. If on calling this method, the scenario has all of its needed properties, the scaled value will be returned.

Otherwise this method will return the updated Scheme object.

Parameters:

  • source (::Enumerable)
  • destination (::Enumerable)

Returns:



78
79
80
81
82
# File 'lib/scale/scheme.rb', line 78

def using(source, destination)
  @source = Source.new(source)
  @destination = Destination.new(destination)
  scale? ? result : self
end