Class: Pure::PureModule

Inherits:
Module
  • Object
show all
Defined in:
lib/pure/pure_module.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parser, &block) ⇒ PureModule

:nodoc:



4
5
6
7
8
# File 'lib/pure/pure_module.rb', line 4

def initialize(parser, &block)  #:nodoc:
  @parsing_active = true
  @parser = parser
  super(&block)
end

Instance Attribute Details

#parserObject (readonly)

:nodoc:



116
117
118
# File 'lib/pure/pure_module.rb', line 116

def parser
  @parser
end

Instance Method Details

#compute(*args) ⇒ Object

call-seq: compute(overrides = {})

compute(num_parallel, overrides = {})
compute(worker, overrides = {})

Initialize a computation.

All three forms take an optional hash for overriding pure functions.

In the first form, Pure.worker is the worker for the computation and Pure.worker decides the number of parallel computations.

In the second form, Pure.worker is the worker for the computation and num_parallel is passed as a hint to Pure.worker, which may accept or ignore the hint.

In the third form, worker is the worker for the computation and worker decides the number of parallel computations.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pure/pure_module.rb', line 30

def compute(*args)
  overrides = args.last.is_a?(Hash) ? args.pop : Hash.new
  worker, num_parallel = (
    case args.size
    when 0
      [Pure.worker, nil]
    when 1
      if args[0].is_a? Integer
        [Pure.worker, args[0]]
      else
        [args[0], nil]
      end
    else
      raise ArgumentError, "wrong number of arguments"
    end
  )
  driver = Driver.new(worker, self, num_parallel, overrides)
  delegate = Delegate.new(driver)
  if block_given?
    yield delegate
  else
    delegate
  end
end

#deactivate_parsingObject

:nodoc:



118
119
120
121
122
123
124
125
# File 'lib/pure/pure_module.rb', line 118

def deactivate_parsing  #:nodoc:
  @parsing_active = false
  begin
    yield
  ensure
    @parsing_active = true
  end
end

#define_method(*args, &block) ⇒ Object

:nodoc:



108
109
110
111
112
113
114
# File 'lib/pure/pure_module.rb', line 108

def define_method(*args, &block)  #:nodoc:
  if @parsing_active
    raise DefineMethodError.new(*Util.file_line(caller.first))
  else
    super
  end
end

#each_functionObject

:nodoc:



127
128
129
130
131
132
133
134
135
# File 'lib/pure/pure_module.rb', line 127

def each_function  #:nodoc:
  ancestors.each { |ancestor|
    if defs = ExtractedFunctions[parser][ancestor]
      defs.each_pair { |name, spec|
        yield name, spec
      }
    end
  }
end

#fun(*args, &block) ⇒ Object

call-seq: fun name => [name_a, name_b,…] do |arg_a, arg_b,…|

  ...
end

Define a pure function whose name and/or argument names are not known at compile time.

The name of the pure function is the value of name. The names of the function arguments are name_a, name_b,… The respective values of the function arguments are arg_a,arg_b,…

See README.rdoc for examples.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/pure/pure_module.rb', line 70

def fun(*args, &block)
  function_str, arg_data = (
    if args.size == 1
      arg = args.first
      if arg.is_a? Hash
        unless arg.size == 1
          raise ArgumentError, "`fun' given hash of size != 1"
        end
        arg.to_a.first
      else
        [arg, []]
      end
    else
      raise ArgumentError, "wrong number of arguments (#{args.size} for 1)"
    end
  )
  arg_names = (
    if arg_data.is_a? Enumerable
      arg_data.map { |t| t.to_sym }
    else
      [arg_data.to_sym]
    end
  )
  function_name = function_str.to_sym
  deactivate_parsing {
    define_method(function_name, &block)
  }
  Extractor.record_function(self, :fun, function_name, arg_names, caller)
  nil
end

#method_added(function_name) ⇒ Object

:nodoc:



101
102
103
104
105
106
# File 'lib/pure/pure_module.rb', line 101

def method_added(function_name)  #:nodoc:
  super
  if @parsing_active
    Extractor.record_function(self, :def, function_name, nil, caller)
  end
end