Class: CTioga2::Commands::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/commands/function.rb

Overview

A Function is a makefile-like “macro” or “function” that takes one or more arguments (no argumentless functions for now).

This class provides both the definition and handling of a function and the global registry of functions.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, short_desc, &blk) ⇒ Function

TODO:

Have self-documenting capacities !

Registers a function.



44
45
46
47
48
49
50
# File 'lib/ctioga2/commands/function.rb', line 44

def initialize(name, short_desc, &blk)
  @code = blk
  @name = name
  @short_description = short_desc
  
  Function.register(self)
end

Instance Attribute Details

#codeObject

The underlying proc object. The first argument to the code is always the plotmaker object.



30
31
32
# File 'lib/ctioga2/commands/function.rb', line 30

def code
  @code
end

#descriptionObject

Long description, ie a help text like the rest



39
40
41
# File 'lib/ctioga2/commands/function.rb', line 39

def description
  @description
end

#nameObject

The name of the function. Probably better lowercase ?



33
34
35
# File 'lib/ctioga2/commands/function.rb', line 33

def name
  @name
end

#short_descriptionObject

A short description



36
37
38
# File 'lib/ctioga2/commands/function.rb', line 36

def short_description
  @short_description
end

Class Method Details

.functionsObject

Returns the functions hash



82
83
84
# File 'lib/ctioga2/commands/function.rb', line 82

def self.functions
  return @functions
end

.named_function(name) ⇒ Object

Returns the named function definition, or nil if there isn’t such.



77
78
79
# File 'lib/ctioga2/commands/function.rb', line 77

def self.named_function(name)
  return @functions[name]
end

.register(func) ⇒ Object

Registers the given function definition



70
71
72
73
# File 'lib/ctioga2/commands/function.rb', line 70

def self.register(func)
  @functions ||= {}
  @functions[func.name] = func
end

Instance Method Details

#describe(txt) ⇒ Object



52
53
54
# File 'lib/ctioga2/commands/function.rb', line 52

def describe(txt)
  @description = txt
end

#expand(string, interpreter) ⇒ Object

Expands the function, and returns the corresponding string.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ctioga2/commands/function.rb', line 57

def expand(string, interpreter)
  if @code.arity == 2
    args = [string.expand_to_string(interpreter)]
  else
    args = string.expand_and_split(/\s+/, interpreter)
  end
  if (@code.arity > 0) and (args.size != (@code.arity - 1))
    raise "Function #{@name} expects #{@code.arity} arguments, but was given #{args.size}"
  end
  return @code.call(interpreter.plotmaker_target, *args).to_s
end