Class: Vissen::Parameterized::Parameter
- Inherits:
-
Object
- Object
- Vissen::Parameterized::Parameter
- Extended by:
- Forwardable
- Defined in:
- lib/vissen/parameterized/parameter.rb
Overview
Instance Method Summary collapse
-
#bind(obj) ⇒ self
TODO: validate the value type.
-
#clear! ⇒ self
Unbinds the parameter and resets the value of the to the default of the value class.
- #constant? ⇒ false, true
-
#initialize(value_klass, default_value = nil) ⇒ Parameter
constructor
A new instance of Parameter.
-
#inspect ⇒ String
Produces a readable string representation of the parameter object.
-
#set(value) ⇒ self
Writes a constant value to the parameter.
-
#target ⇒ #value
The value target.
-
#to_s ⇒ String
The parameter value formated as a string wrapped either in ‘()` or `{}` depending on if the value is constant or bound.
-
#type ⇒ Value
The value class of the parameter.
-
#unbind ⇒ Object
Copies the value of the target to the internal constant and unbinds from the target.
Constructor Details
#initialize(value_klass, default_value = nil) ⇒ Parameter
Returns a new instance of Parameter.
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
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.
39 40 41 |
# File 'lib/vissen/parameterized/parameter.rb', line 39 def clear! set @default end |
#constant? ⇒ false, true
45 46 47 |
# File 'lib/vissen/parameterized/parameter.rb', line 45 def constant? @constant.equal? @target end |
#inspect ⇒ String
Produces a readable string representation of the parameter object.
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.
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.
52 53 54 55 |
# File 'lib/vissen/parameterized/parameter.rb', line 52 def target raise RuntimeError if constant? @target end |
#to_s ⇒ String
Returns 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 |
#type ⇒ Value
Returns the value class of the parameter.
85 86 87 |
# File 'lib/vissen/parameterized/parameter.rb', line 85 def type @constant.class end |
#unbind ⇒ Object
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 |