class Vissen::Parameterized::Parameter

Parameter respond to value and can either return a locally stored constant or the value of a target object.

Usage

parameter = Parameter.new Value::Real
parameter.set 42
parameter.value # => 42

target = Value::Real.new 4.2
parameter.bind target
parameter.value # => 4.2

Constants

INSPECT_FORMAT

Public Class Methods

new(value_klass, default_value = nil) click to toggle source

@param value_klass [Class] the value type supported by the parameter. @param default_value [Object] the default constant value. It defaults

to the default of the given value class.
# File lib/vissen/parameterized/parameter.rb, line 29
def initialize(value_klass, default_value = nil)
  @default  = default_value.nil? ? value_klass::DEFAULT : default_value
  @constant = value_klass.new @default
  @target   = @constant
end

Public Instance Methods

bind(obj) click to toggle source

TODO: validate the value type

@param obj [#value] the value object to bind to. @return [self]

# File lib/vissen/parameterized/parameter.rb, line 71
def bind(obj)
  raise TypeError unless obj.respond_to?(:value)
  @target = obj
  self
end
clear!() click to toggle source

Unbinds the parameter and resets the value of the to the default of the

value class.

@return [self]

# File lib/vissen/parameterized/parameter.rb, line 39
def clear!
  set @default
end
constant?() click to toggle source

@return [false] if the parameter is bound to a value object. @return [true] otherwise.

# File lib/vissen/parameterized/parameter.rb, line 45
def constant?
  @constant.equal? @target
end
inspect() click to toggle source

Produces a readable string representation of the parameter object.

@return [String] a string representation.

# File lib/vissen/parameterized/parameter.rb, line 99
def inspect
  format INSPECT_FORMAT, name: self.class.name,
                         object_id: object_id,
                         type: Value.canonicalize(type),
                         value: to_s
end
set(value) click to toggle source

Writes a constant value to the parameter.

@param value [Object] the value to set. @return [self]

# File lib/vissen/parameterized/parameter.rb, line 61
def set(value)
  @constant.write value
  @target = @constant
  self
end
target() click to toggle source

@raise [RuntimeError] if the parameter is constant.

@return [#value] the value target.

# File lib/vissen/parameterized/parameter.rb, line 52
def target
  raise RuntimeError if constant?
  @target
end
to_s() click to toggle source

@return [String] the parameter value formated as a string wrapped either

in `()` or `{}` depending on if the value is constant or bound.
# File lib/vissen/parameterized/parameter.rb, line 91
def to_s
  base = @target.to_s
  constant? ? "(#{base})" : "{#{base}}"
end
type() click to toggle source

@return [Value] the value class of the parameter.

# File lib/vissen/parameterized/parameter.rb, line 85
def type
  @constant.class
end
unbind() click to toggle source

Copies the value of the target to the internal constant and unbinds from the target.

# File lib/vissen/parameterized/parameter.rb, line 79
def unbind
  raise 'cannot unbind constant' if constant?
  set @target.value
end