Module: Vissen::Parameterized::Value

Included in:
Bool, Int, Real, Vec
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

Classes: Bool, Int, Real, Vec

Constant Summary collapse

DEFAULT =

Returns the default value that will be used when ‘.new` is called without arguments, or with nil.

Returns:

  • (Object)

    the default value that will be used when ‘.new` is called without arguments, or with nil.

nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#valueObject (readonly)

Returns the internal value object.

Returns:

  • (Object)

    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"

Parameters:

  • klass (Class)

    the class to canonicalize.

Returns:

  • (String)

    a string version of the class name.



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

.typesArray<Module>

Returns an array of the modules and classes that include the ‘Value` module.

Returns:

  • (Array<Module>)

    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

Parameters:

  • value (Object) (defaults to: nil)

    the initial value to use. Ignored if nil.



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

#scopeScope

Values are always considered part of the global scope.

Returns:

  • (Scope)

    the scope of the value.



67
68
69
# File 'lib/vissen/parameterized/value.rb', line 67

def scope
  GlobalScope.instance
end

#tainted?true, false

Returns:

  • (true)

    if the value has been written to since the last call to ‘#untaint!`.

  • (false)

    otherwise.



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

def tainted?
  @tainted
end

#to_sString

Returns the value formated as a string, with an appended ‘*’ if the value is tainted.

Returns:

  • (String)

    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.

Returns:

  • (false)


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.

Parameters:

  • new_value (Object)

    the new value to write.

Returns:

  • (true)

    if the value was changed.

  • (false)

    otherwise.



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