Class: Vissen::Parameterized::Conditional
- Inherits:
-
Object
- Object
- Vissen::Parameterized::Conditional
- 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
Instance Method Summary collapse
-
#force!(value = true) ⇒ true, false
Forces the state of the output to the given value.
-
#initialize(input_klass = Value::Real, **opts) ⇒ Conditional
constructor
A new instance of Conditional.
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.
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.
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 |