Module: Vissen::Parameterized
- Extended by:
- Forwardable
- Included in:
- Conditional
- Defined in:
- lib/vissen/parameterized.rb,
lib/vissen/parameterized/dsl.rb,
lib/vissen/parameterized/error.rb,
lib/vissen/parameterized/graph.rb,
lib/vissen/parameterized/scope.rb,
lib/vissen/parameterized/value.rb,
lib/vissen/parameterized/version.rb,
lib/vissen/parameterized/accessor.rb,
lib/vissen/parameterized/parameter.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,
lib/vissen/parameterized/conditional.rb,
lib/vissen/parameterized/scope_error.rb,
lib/vissen/parameterized/global_scope.rb
Overview
A parameterized object should have
-
a set of parameters,
-
a (possibly expensive) function that transforms the parameters to an output, and
-
an output value.
Defined Under Namespace
Modules: DSL, Value Classes: Accessor, Conditional, Error, GlobalScope, Graph, Parameter, Scope, ScopeError
Constant Summary collapse
- VERSION =
The version number of the Parameterized library.
'0.1.1'
Instance Method Summary collapse
-
#bind(param, target) ⇒ Parameter
Binds a parameter to a target value.
-
#call(_parameters) ⇒ Object
An object compatible with the output value type should be returned.
-
#each_parameterized ⇒ Enumerable
Iterates over the parameterized objects currently bound to the parameters.
-
#initialize(*args, parameters:, output:, scope: GlobalScope.instance, setup: {}) ⇒ Object
Forwards all arguments to super.
-
#inspect ⇒ String
Produces a readable string representation of the parameterized object.
- #parameter?(key) ⇒ true, false
-
#parameters ⇒ Accessor
(also: #params)
A proxy object that provides access to parameters via method calls instead of hash lookups.
-
#returns_a?(value_klass) ⇒ true, false
Checks if the parameterized object returns a value of the given value class.
-
#scope ⇒ Scope
The scope to which the parameterized object belongs.
-
#set(param, value) ⇒ Parameter
Sets the constant value of a parameter.
-
#tainted? ⇒ true, false
Checks if the output value of the parameterized object has changed.
-
#untaint! ⇒ false
Marks the output value and all input parameters as untainted.
-
#value ⇒ Object
The output value.
Instance Method Details
#bind(param, target) ⇒ Parameter
Binds a parameter to a target value.
133 134 135 136 |
# File 'lib/vissen/parameterized.rb', line 133 def bind(param, target) raise ScopeError unless scope.include? target @_params.fetch(param).bind target end |
#call(_parameters) ⇒ Object
An object compatible with the output value type should be returned.
74 75 76 |
# File 'lib/vissen/parameterized.rb', line 74 def call(_parameters) raise NotImplementedError end |
#each_parameterized ⇒ Enumerable
Iterates over the parameterized objects currently bound to the parameters.
176 177 178 179 180 181 182 183 |
# 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 |
#initialize(*args, parameters:, output:, scope: GlobalScope.instance, setup: {}) ⇒ Object
Forwards all arguments to super.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# 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 |
#inspect ⇒ String
Produces a readable string representation of the parameterized object.
166 167 168 169 170 171 |
# 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 |
#parameter?(key) ⇒ true, false
120 121 122 |
# File 'lib/vissen/parameterized.rb', line 120 def parameter?(key) @_params.key? key end |
#parameters ⇒ Accessor Also known as: params
Returns a proxy object that provides access to parameters via method calls instead of hash lookups.
152 153 154 |
# File 'lib/vissen/parameterized.rb', line 152 def parameters @_accessor end |
#returns_a?(value_klass) ⇒ true, false
Checks if the parameterized object returns a value of the given value class.
44 |
# File 'lib/vissen/parameterized.rb', line 44 def_delegator :@_value, :is_a?, :returns_a? |
#scope ⇒ Scope
Returns the scope to which the parameterized object belongs.
159 160 161 |
# File 'lib/vissen/parameterized.rb', line 159 def scope @_scope end |
#set(param, value) ⇒ Parameter
Sets the constant value of a parameter.
146 147 148 |
# File 'lib/vissen/parameterized.rb', line 146 def set(param, value) @_params.fetch(param).set value end |
#tainted? ⇒ true, false
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.
105 106 107 108 109 110 111 112 113 114 115 116 |
# 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 |
#untaint! ⇒ false
Marks the output value and all input parameters as untainted.
81 82 83 84 85 86 87 88 89 90 91 |
# 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 |
#value ⇒ Object
Returns the output value.
35 |
# File 'lib/vissen/parameterized.rb', line 35 def_delegators :@_value, :value, :to_s |