Class: Stretto::Value::VariableValue

Inherits:
Object
  • Object
show all
Includes:
Stretto::Variables
Defined in:
lib/stretto/music_elements/modifiers/value.rb

Overview

Wraps a variable value, that is, holds a reference to a value that is going to be evaluated until requested.

See Also:

Constant Summary

Constants included from Stretto::Variables

Stretto::Variables::CONTROLLER_VARIABLES, Stretto::Variables::INSTRUMENT_VARIABLES, Stretto::Variables::PERCUSSION_VARIABLES, Stretto::Variables::PREDEFINED_VARIABLES, Stretto::Variables::TEMPO_VARIABLES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ VariableValue

Returns a new instance of VariableValue.

Parameters:

  • The (String)

    name that references the variable



57
58
59
# File 'lib/stretto/music_elements/modifiers/value.rb', line 57

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



54
55
56
# File 'lib/stretto/music_elements/modifiers/value.rb', line 54

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object

Returns whether the other object is the same class and holds the same variable name



93
94
95
# File 'lib/stretto/music_elements/modifiers/value.rb', line 93

def ==(other)
  other.kind_of?(VariableValue) && other.name == @name
end

#to_f(pattern) ⇒ Float

Returns the value of the variable as a float

Returns:

  • (Float)

Raises:

  • (VariableContextException)

    if the variable is not one of the predefined values and there is no pattern attached

  • (VariableNotDefinedException)

    if the variable is not defined in the pattern



73
74
75
# File 'lib/stretto/music_elements/modifiers/value.rb', line 73

def to_f(pattern)
  get_numeric_value(pattern){ |value| value.to_f(pattern) }
end

#to_i(pattern) ⇒ Integer

Returns the value of the variable as an integer

Returns:

  • (Integer)

Raises:

  • (VariableContextException)

    if the variable is not one of the predefined values and there is no pattern attached

  • (VariableNotDefinedException)

    if the variable is not defined in the pattern



65
66
67
# File 'lib/stretto/music_elements/modifiers/value.rb', line 65

def to_i(pattern)
  get_numeric_value(pattern){ |value| value.to_i(pattern) }
end

#to_sString

Name of the variable, enclosed in brackets

Returns:



100
101
102
# File 'lib/stretto/music_elements/modifiers/value.rb', line 100

def to_s
  "[#{@name}]"
end

#value(pattern) ⇒ Object

Returns either the variable value if attached to a pattern, or raises an exception if there is no pattern attached and the variable is not one of the predefined ones.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/stretto/music_elements/modifiers/value.rb', line 79

def value(pattern)
  if pattern
    pattern.variable(@name)
  else
    predefined = PREDEFINED_VARIABLES[name.upcase]
    unless predefined
      raise Stretto::Exceptions::VariableContextException.new(
          "A pattern is needed to access variable #{@name}")
    end
    Value.new(Value::NumericValue.new(predefined))
  end
end