Class: Vissen::Parameterized::Conditional

Inherits:
Object
  • Object
show all
Includes:
Vissen::Parameterized
Defined in:
lib/vissen/parameterized/conditional.rb

Overview

The conditional is just a specialized form of a parameterized object. It takes just one input and, through a given block, transforms it to a boolean value. The aliased method ‘#met?` is nothing more than syntactic suger for and equivalent to calling `#value`.

Usage

The following exable sets up a conditional, binds it to a value and checks if the condition is met for two different values. The update proceedure is the same as with other parameterized objects.

less_than_two = Conditional.new { |value| value < 2 }
value = Value::Real.new 1
less_than_two.bind :input, value

less_than_two.tainted? # => true
less_than_two.met? # => true
less_than_two.untaint!

value.write 3
less_than_two.tainted? # => true
less_than_two.met? # => false

Constant Summary

Constants included from Vissen::Parameterized

VERSION

Instance Method Summary collapse

Methods included from Vissen::Parameterized

#bind, #call, #each_parameterized, #inspect, #parameter?, #parameters, #returns_a?, #scope, #set, #tainted?, #untaint!, #value

Constructor Details

#initialize(input_klass = Value::Real, **opts) ⇒ Conditional

Returns a new instance of Conditional.

Parameters:

  • input_klass (Class) (defaults to: Value::Real)

    the value class of the input parameter.



36
37
38
39
40
41
42
43
44
# File 'lib/vissen/parameterized/conditional.rb', line 36

def initialize(input_klass = Value::Real, **opts)
  super(parameters: { input: Parameter.new(input_klass) },
        output: Value::Bool.new,
        **opts)

  define_singleton_method :call do |params|
    yield params.input
  end
end

Instance Method Details

#force!(value = true) ⇒ true, false

Forces the state of the output to the given value. The input is unbound and untainted to prevent it from affecting the output further.

Parameters:

  • value (true, false) (defaults to: true)

    the value to force.

Returns:

  • (true)

    if the output was changed.

  • (false)

    otherwise.



52
53
54
55
56
57
58
# File 'lib/vissen/parameterized/conditional.rb', line 52

def force!(value = true)
  input = @_params[:input]
  input.unbind unless input.constant?
  input.untaint!

  @_value.write value
end