module Vissen::Parameterized
A parameterized object should have
-
a set of parameters,
-
a (possibly expensive) function that transforms the parameters to an output, and
-
an output value.
Constants
- INSPECT_FORMAT
- VERSION
The version number of the
Parameterized
library.@return [String] a semantic version number.
Public Class Methods
Forwards all arguments to super.
@param args [Array<Object>] the arguments to forward to super. @param parameters [Hash<Symbol, Parameter>] the input parameters. @param output [Value] the output value object. @param scope [Scope] the scope of the object. @param setup [Hash<Symbol, Object>] the initial setup.
# File lib/vissen/parameterized.rb, line 53 def initialize(*args, parameters:, output:, scope: GlobalScope.instance, setup: {}) @_accessor = Accessor.new parameters @_params = parameters @_scope = scope @_value = output @_checked = false load_initial setup super(*args) end
Public Instance Methods
Binds a parameter to a target value.
@see Parameter#bind
@raise [KeyError] if the parameter is not found. @raise [ScopeError] if the parameter is out of scope.
@param param [Symbol] the parameter to bind. @param target [#value] the value object to bind to. @return [Parameter] the parameter that was bound.
# File lib/vissen/parameterized.rb, line 133 def bind(param, target) raise ScopeError unless scope.include? target @_params.fetch(param).bind target end
@raise [NotImplementedError] if not implemented by descendent.
@param _parameters [Accessor] the parameters of the parameterized object. @return [Object] an object compatible with the output value type should be
returned.
# File lib/vissen/parameterized.rb, line 74 def call(_parameters) raise NotImplementedError end
Iterates over the parameterized objects currently bound to the parameters.
@return [Enumerable] if no block is given.
# File lib/vissen/parameterized.rb, line 176 def each_parameterized return to_enum(__callee__) unless block_given? @_params.each do |_, param| next if param.constant? target = param.target yield target if target.is_a? Parameterized end end
Produces a readable string representation of the parameterized object.
@return [String] a string representation.
# File lib/vissen/parameterized.rb, line 166 def inspect format INSPECT_FORMAT, name: self.class.name, object_id: object_id, params: params_with_types, type: Value.canonicalize(@_value.class) end
@return [true] if the parameterized object has the given parameter. @return [false] otherwise.
# File lib/vissen/parameterized.rb, line 120 def parameter?(key) @_params.key? key end
@return [Accessor] a proxy object that provides access to parameters via
method calls instead of hash lookups.
# File lib/vissen/parameterized.rb, line 152 def parameters @_accessor end
@return [Scope] the scope to which the parameterized object belongs.
# File lib/vissen/parameterized.rb, line 159 def scope @_scope end
Sets the constant value of a parameter.
@see Parameter#set
@raise [KeyError] if the parameter is not found.
@param param [Symbol] the parameter to bind. @param value [Object] the value to set. @return [Parameter] the parameter that was set.
# File lib/vissen/parameterized.rb, line 146 def set(param, value) @_params.fetch(param).set value end
Checks if the output value of the parameterized object has changed. If any of the input parameters have changed since last calling `#untaint!` the `#call` method will be evaluated in order to determine the state of the output value.
Note that `#call` is only evaluated once after the object has been untainted. Subsequent calls to `#tainted?` will refer to the result of the first operation.
@return [true] if the output value has changed since last calling
`#untaint!`.
@return [false] otherwise.
# File lib/vissen/parameterized.rb, line 105 def tainted? return @_value.tainted? if @_checked @_checked = true params_tainted = @_params.reduce(false) do |a, (_, param)| param.tainted? || a end return false unless params_tainted @_value.write call(@_accessor) end
Marks the output value and all input parameters as untainted.
@return [false]
# File lib/vissen/parameterized.rb, line 81 def untaint! # ASUMPTION: if the value has not been taint checked # there should be no untainted values in # this part of the graph. This does not # hold initially. return unless @_checked @_checked = false @_params.each { |_, param| param.untaint! } @_value.untaint! end
Private Instance Methods
# File lib/vissen/parameterized.rb, line 187 def load_initial(setup) setup.each do |key, value| @_params.fetch(key) .send(value.respond_to?(:value) ? :bind : :set, value) end end
# File lib/vissen/parameterized.rb, line 194 def params_with_types @_params.map { |k, v| "#{k}:#{Value.canonicalize(v.type)}" }.join(', ') end