Class: Calculi::FunctionSet

Inherits:
Module
  • Object
show all
Includes:
Base, Enumerable, TSort
Defined in:
lib/calculi/function_set.rb

Overview

A set that can hold arbitrarily-computed Functions with a dependency graph

Constant Summary collapse

DEPENDENCIES_NOT_CALCULATED =
Object.new

Constants included from Utility

Utility::NON_IVAR, Utility::NULL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Attributes

#configure, #initialize

Methods included from HasOptionSet

#option_set

Methods included from Utility

#at_prefixed, #callable?, #constantly, #eval_or_value, #instance_variable_compute, #procable?, #set_attribute

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



13
14
15
# File 'lib/calculi/function_set.rb', line 13

def target
  @target
end

Instance Method Details

#[](function_key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/calculi/function_set.rb', line 21

def [](function_key)
  case function_key
  when Calculi::Function
    functions.detect { |fn, deps| fn.eql? function_key }
  when Symbol, String
    function_key = function_key.to_s

    functions.detect { |fn, deps| fn.key.to_s == function_key }
  else
    raise KeyError, "Don't know how to find function with `#{function_key.inspect}'"
  end
end

#define_functions!Object



72
73
74
75
76
77
78
79
80
# File 'lib/calculi/function_set.rb', line 72

def define_functions!
  tsort.each do |fn|
    function_names[fn.key] = fn.realized_name

    redefine_method(fn.realized_name, &fn.realized_body)
  end

  target.send :include, self
end

#fetch(fn) ⇒ Object



34
35
36
# File 'lib/calculi/function_set.rb', line 34

def fetch(fn)
  self[fn].try(:first) or raise KeyError, "Unknown function: #{fn}"
end

#function(key, options = {}, &function_configurator) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/calculi/function_set.rb', line 46

def function(key, options = {}, &function_configurator)
  options.merge! function_set: self, key: key

  new_function = Calculi::Function.new(options, &function_configurator)

  functions[new_function] = DEPENDENCIES_NOT_CALCULATED
end

#function_namesObject



42
43
44
# File 'lib/calculi/function_set.rb', line 42

def function_names
  @function_names ||= OpenStruct.new
end

#functionsObject



38
39
40
# File 'lib/calculi/function_set.rb', line 38

def functions
  @functions ||= {}
end

#inspectObject



54
55
56
57
58
59
60
# File 'lib/calculi/function_set.rb', line 54

def inspect
  function_hash = functions.each_key.each_with_object({}) do |fn, fn_hsh|
    fn_hsh[fn.key] = fn.inspect_dependency_names
  end

  "Calculi::FunctionSet(#{function_hash.inspect})"
end

#tsort_each_child(node, &block) ⇒ Object



66
67
68
69
70
# File 'lib/calculi/function_set.rb', line 66

def tsort_each_child(node, &block)
  fetch(node).dependency_names.each do |dependency_name|
    yield fetch(dependency_name)
  end
end

#tsort_each_node(&block) ⇒ Object



62
63
64
# File 'lib/calculi/function_set.rb', line 62

def tsort_each_node(&block)
  functions.each_key(&block)
end