class CTioga2::Commands::Function

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.

Attributes

code[RW]

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

description[RW]

Long description, ie a help text like the rest

name[RW]

The name of the function. Probably better lowercase ?

short_description[RW]

A short description

Public Class Methods

functions() click to toggle source

Returns the functions hash

# File lib/ctioga2/commands/function.rb, line 82
def self.functions
  return @functions
end
named_function(name) click to toggle source

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

# File lib/ctioga2/commands/function.rb, line 77
def self.named_function(name)
  return @functions[name]
end
new(name, short_desc, &blk) click to toggle source

Registers a function.

@todo Have self-documenting capacities !

# 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
register(func) click to toggle source

Registers the given function definition

# File lib/ctioga2/commands/function.rb, line 70
def self.register(func)
  @functions ||= {}
  @functions[func.name] = func
end

Public Instance Methods

describe(txt) click to toggle source
# File lib/ctioga2/commands/function.rb, line 52
def describe(txt)
  @description = txt
end
expand(string, interpreter) click to toggle source

Expands the function, and returns the corresponding string.

# 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