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
48
49
50
51
52
53
54
# File 'lib/scale/scheme.rb', line 43

def initialize(options = {})
  @input = options[:input]
  @destination = nil
  @source = nil

  unless options[:source].nil?
    @source = Source.new(options[:source])
  end
  unless options[:destination].nil?
    @destination = Destination.new(options[:destination])
  end
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:



62
63
64
65
# File 'lib/scale/scheme.rb', line 62

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

#resultNumeric?

Get the result of this scaling scenario

Returns:



112
113
114
# File 'lib/scale/scheme.rb', line 112

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:



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

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



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

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:



73
74
75
76
# File 'lib/scale/scheme.rb', line 73

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:



85
86
87
88
89
# File 'lib/scale/scheme.rb', line 85

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