Class: Stretto::Value

Inherits:
Object show all
Defined in:
lib/stretto/music_elements/modifiers/value.rb

Overview

This class acts as a placeholder for values contained by musical elements.

It is needed in order to retrieve values held by a variable until evaluation, looking for them in the pattern the element belongs, or one of the predefined variables.

Defined Under Namespace

Classes: NumericValue, VariableValue

Instance Method Summary collapse

Constructor Details

#initialize(value, variation = nil) ⇒ Value

Initializes with the value passed in, and an optional variation.

and have it evaluated until requested

Parameters:

  • value (NumericValue, VariableValue)
  • variation (Number) (defaults to: nil)

    Works as a delayed addition, so you can do things like ‘Value.new(VariableValue.new(“SOME_VAR”)) + 1`



129
130
131
132
# File 'lib/stretto/music_elements/modifiers/value.rb', line 129

def initialize(value, variation = nil)
  @value = value
  @variation = variation
end

Instance Method Details

#+(variation) ⇒ Value

Adds a variation to the Value, returning a new one with the sum of variations

Returns:



159
160
161
162
# File 'lib/stretto/music_elements/modifiers/value.rb', line 159

def +(variation)
  new_variation = [@variation, variation].compact.sum
  self.class.new(@value, new_variation)
end

#to_f(pattern) ⇒ Float?

Converts the value to float

Returns:

  • (Float, nil)


148
149
150
151
152
153
154
# File 'lib/stretto/music_elements/modifiers/value.rb', line 148

def to_f(pattern)
  if @value
    result = @value.to_f(pattern)
    result += @variation if @variation
    result
  end
end

#to_i(pattern) ⇒ Integer?

Converts the value to integer

Returns:

  • (Integer, nil)


137
138
139
140
141
142
143
# File 'lib/stretto/music_elements/modifiers/value.rb', line 137

def to_i(pattern)
  if @value
    result = @value.to_i(pattern)
    result += @variation if @variation
    result
  end
end

#to_sObject

String representation for this value, either a number or the name of the variable.



165
166
167
168
169
# File 'lib/stretto/music_elements/modifiers/value.rb', line 165

def to_s
  output = @value.to_s
  output += "+#{@variation}" if @variation
  output
end