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.

Returns:

  • (String)

    a semantic version number.

'0.1.1'

Instance Method Summary collapse

Instance Method Details

#bind(param, target) ⇒ Parameter

Binds a parameter to a target value.

Parameters:

  • param (Symbol)

    the parameter to bind.

  • target (#value)

    the value object to bind to.

Returns:

  • (Parameter)

    the parameter that was bound.

Raises:

  • (KeyError)

    if the parameter is not found.

  • (ScopeError)

    if the parameter is out of scope.

See Also:



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.

Parameters:

  • _parameters (Accessor)

    the parameters of the parameterized object.

Returns:

  • (Object)

    an object compatible with the output value type should be returned.

Raises:

  • (NotImplementedError)

    if not implemented by descendent.



74
75
76
# File 'lib/vissen/parameterized.rb', line 74

def call(_parameters)
  raise NotImplementedError
end

#each_parameterizedEnumerable

Iterates over the parameterized objects currently bound to the parameters.

Returns:

  • (Enumerable)

    if no block is given.



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.

Parameters:

  • args (Array<Object>)

    the arguments to forward to super.

  • parameters (Hash<Symbol, Parameter>)

    the input parameters.

  • output (Value)

    the output value object.

  • scope (Scope) (defaults to: GlobalScope.instance)

    the scope of the object.

  • setup (Hash<Symbol, Object>) (defaults to: {})

    the initial setup.



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

#inspectString

Produces a readable string representation of the parameterized object.

Returns:

  • (String)

    a string representation.



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

Returns:

  • (true)

    if the parameterized object has the given parameter.

  • (false)

    otherwise.



120
121
122
# File 'lib/vissen/parameterized.rb', line 120

def parameter?(key)
  @_params.key? key
end

#parametersAccessor Also known as: params

Returns a proxy object that provides access to parameters via method calls instead of hash lookups.

Returns:

  • (Accessor)

    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.

Parameters:

  • value_klass (Class)

    the class to test.

Returns:

  • (true)

    if the output value is of the given class.

  • (false)

    otherwise.



44
# File 'lib/vissen/parameterized.rb', line 44

def_delegator :@_value, :is_a?, :returns_a?

#scopeScope

Returns the scope to which the parameterized object belongs.

Returns:

  • (Scope)

    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.

Parameters:

  • param (Symbol)

    the parameter to bind.

  • value (Object)

    the value to set.

Returns:

  • (Parameter)

    the parameter that was set.

Raises:

  • (KeyError)

    if the parameter is not found.

See Also:



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.

Returns:

  • (true)

    if the output value has changed since last calling ‘#untaint!`.

  • (false)

    otherwise.



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.

Returns:

  • (false)


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

#valueObject

Returns the output value.

Returns:

  • (Object)

    the output value.



35
# File 'lib/vissen/parameterized.rb', line 35

def_delegators :@_value, :value, :to_s