Class: CTioga2::Commands::Function
- Inherits:
-
Object
- Object
- CTioga2::Commands::Function
- 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
-
#code ⇒ Object
The underlying proc object.
-
#description ⇒ Object
Long description, ie a help text like the rest.
-
#name ⇒ Object
The name of the function.
-
#short_description ⇒ Object
A short description.
Class Method Summary collapse
-
.functions ⇒ Object
Returns the functions hash.
-
.named_function(name) ⇒ Object
Returns the named function definition, or nil if there isn’t such.
-
.register(func) ⇒ Object
Registers the given function definition.
Instance Method Summary collapse
- #describe(txt) ⇒ Object
-
#expand(string, interpreter) ⇒ Object
Expands the function, and returns the corresponding string.
-
#initialize(name, short_desc, &blk) ⇒ Function
constructor
Registers a function.
Constructor Details
#initialize(name, short_desc, &blk) ⇒ Function
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
#code ⇒ Object
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 |
#description ⇒ Object
Long description, ie a help text like the rest
39 40 41 |
# File 'lib/ctioga2/commands/function.rb', line 39 def description @description end |
#name ⇒ Object
The name of the function. Probably better lowercase ?
33 34 35 |
# File 'lib/ctioga2/commands/function.rb', line 33 def name @name end |
#short_description ⇒ Object
A short description
36 37 38 |
# File 'lib/ctioga2/commands/function.rb', line 36 def short_description @short_description end |
Class Method Details
.functions ⇒ Object
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 (string, interpreter) if @code.arity == 2 args = [string.(interpreter)] else args = string.(/\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 |