Module: Vissen::Parameterized::Value
- Defined in:
- lib/vissen/parameterized/value.rb,
lib/vissen/parameterized/value/int.rb,
lib/vissen/parameterized/value/vec.rb,
lib/vissen/parameterized/value/bool.rb,
lib/vissen/parameterized/value/real.rb
Overview
The Value module implements the basic functionallity of a value type. Class implementations are encouraged to override #write to provide type checking or coercion.
Usage
The following example implements an integer type by calling #to_i on objects before they are written.
class Int
include Value
DEFAULT = 0
def write(new_value)
super new_value.to_i
end
end
Defined Under Namespace
Constant Summary collapse
- DEFAULT =
Returns the default value that will be used when ‘.new` is called without arguments, or with nil.
nil
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
The internal value object.
Class Method Summary collapse
-
.canonicalize(klass) ⇒ String
Converts a class name to a string.
-
.types ⇒ Array<Module>
An array of the modules and classes that include the ‘Value` module.
Instance Method Summary collapse
- #initialize(value = nil) ⇒ Object
-
#scope ⇒ Scope
Values are always considered part of the global scope.
- #tainted? ⇒ true, false
-
#to_s ⇒ String
The value formated as a string, with an appended ‘*’ if the value is tainted.
-
#untaint! ⇒ false
Marks the value as untainted.
-
#write(new_value) ⇒ true, false
Updates the internally stored value.
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the internal value object.
24 25 26 |
# File 'lib/vissen/parameterized/value.rb', line 24 def value @value end |
Class Method Details
.canonicalize(klass) ⇒ String
Converts a class name to a string.
Vissen::Parameterized::Value::Real -> "real"
83 84 85 86 87 88 |
# File 'lib/vissen/parameterized/value.rb', line 83 def self.canonicalize(klass) klass.name .split('::').last .gsub(/([a-z\d])([A-Z])/, '\1_\2') .downcase end |
.types ⇒ Array<Module>
Returns an array of the modules and classes that include the ‘Value` module.
73 74 75 |
# File 'lib/vissen/parameterized/value.rb', line 73 def self.types @types end |
Instance Method Details
#initialize(value = nil) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/vissen/parameterized/value.rb', line 31 def initialize(value = nil) @value = nil @tainted = true write(value.nil? ? self.class::DEFAULT : value) end |
#scope ⇒ Scope
Values are always considered part of the global scope.
67 68 69 |
# File 'lib/vissen/parameterized/value.rb', line 67 def scope GlobalScope.instance end |
#tainted? ⇒ true, false
53 54 55 |
# File 'lib/vissen/parameterized/value.rb', line 53 def tainted? @tainted end |
#to_s ⇒ String
Returns the value formated as a string, with an appended ‘*’ if the value is tainted.
92 93 94 95 |
# File 'lib/vissen/parameterized/value.rb', line 92 def to_s base = @value.to_s tainted? ? base + '*' : base end |
#untaint! ⇒ false
Marks the value as untainted.
60 61 62 |
# File 'lib/vissen/parameterized/value.rb', line 60 def untaint! @tainted = false end |
#write(new_value) ⇒ true, false
Updates the internally stored value. The object will be marked as tainted if the new value differs from the previous.
44 45 46 47 48 |
# File 'lib/vissen/parameterized/value.rb', line 44 def write(new_value) return false if new_value == @value @value = new_value taint! end |