Class: Stretto::Value::VariableValue
- 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.
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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns whether the other object is the same class and holds the same variable name.
-
#initialize(name) ⇒ VariableValue
constructor
A new instance of VariableValue.
-
#to_f(pattern) ⇒ Float
Returns the value of the variable as a float.
-
#to_i(pattern) ⇒ Integer
Returns the value of the variable as an integer.
-
#to_s ⇒ String
Name of the variable, enclosed in brackets.
-
#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.
Constructor Details
#initialize(name) ⇒ VariableValue
Returns a new instance of VariableValue.
57 58 59 |
# File 'lib/stretto/music_elements/modifiers/value.rb', line 57 def initialize(name) @name = name end |
Instance Attribute Details
#name ⇒ Object (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
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
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_s ⇒ String
Name of the variable, enclosed in brackets
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 |