Class: Vissen::Parameterized::Parameter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/vissen/parameterized/parameter.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(value_klass, default_value = nil) ⇒ Parameter

Returns a new instance of Parameter.

Parameters:

  • value_klass (Class)

    the value type supported by the parameter.

  • default_value (Object) (defaults to: nil)

    the default constant value. It defaults to the default of the given value class.



29
30
31
32
33
# 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

Instance Method Details

#bind(obj) ⇒ self

TODO: validate the value type

Parameters:

  • obj (#value)

    the value object to bind to.

Returns:

  • (self)

Raises:

  • (TypeError)


71
72
73
74
75
# File 'lib/vissen/parameterized/parameter.rb', line 71

def bind(obj)
  raise TypeError unless obj.respond_to?(:value)
  @target = obj
  self
end

#clear!self

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

value class.

Returns:

  • (self)


39
40
41
# File 'lib/vissen/parameterized/parameter.rb', line 39

def clear!
  set @default
end

#constant?false, true

Returns:

  • (false)

    if the parameter is bound to a value object.

  • (true)

    otherwise.



45
46
47
# File 'lib/vissen/parameterized/parameter.rb', line 45

def constant?
  @constant.equal? @target
end

#inspectString

Produces a readable string representation of the parameter object.

Returns:

  • (String)

    a string representation.



99
100
101
102
103
104
# 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) ⇒ self

Writes a constant value to the parameter.

Parameters:

  • value (Object)

    the value to set.

Returns:

  • (self)


61
62
63
64
65
# File 'lib/vissen/parameterized/parameter.rb', line 61

def set(value)
  @constant.write value
  @target = @constant
  self
end

#target#value

Returns the value target.

Returns:

  • (#value)

    the value target.

Raises:

  • (RuntimeError)

    if the parameter is constant.



52
53
54
55
# File 'lib/vissen/parameterized/parameter.rb', line 52

def target
  raise RuntimeError if constant?
  @target
end

#to_sString

Returns the parameter value formated as a string wrapped either in ‘()` or `{}` depending on if the value is constant or bound.

Returns:

  • (String)

    the parameter value formated as a string wrapped either in ‘()` or `{}` depending on if the value is constant or bound.



91
92
93
94
# File 'lib/vissen/parameterized/parameter.rb', line 91

def to_s
  base = @target.to_s
  constant? ? "(#{base})" : "{#{base}}"
end

#typeValue

Returns the value class of the parameter.

Returns:

  • (Value)

    the value class of the parameter.



85
86
87
# File 'lib/vissen/parameterized/parameter.rb', line 85

def type
  @constant.class
end

#unbindObject

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



79
80
81
82
# File 'lib/vissen/parameterized/parameter.rb', line 79

def unbind
  raise 'cannot unbind constant' if constant?
  set @target.value
end