Class: LtdTemplate::Code

Inherits:
Object
  • Object
show all
Defined in:
lib/ltdtemplate/code.rb

Overview

LtdTemplate::Code - Base class for LtdTemplate code/value objects

Author:

Defined Under Namespace

Classes: Call, Code_Block, Parameters, Subscript, Variable

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Code

Initialize the object with a link to the associated template.

Parameters:

  • template (LtdTemplate)

    The associated template object.



25
26
27
28
# File 'lib/ltdtemplate/code.rb', line 25

def initialize (template)
	@template = template
	@tpl_methods = {}
end

Instance Attribute Details

#tpl_methodsArray<LtdTemplate::Value::Code_Block> (readonly)

The code blocks bound to non-array values.

Returns:



14
15
16
# File 'lib/ltdtemplate/code.rb', line 14

def tpl_methods
  @tpl_methods
end

Class Method Details

.instance(*args) ⇒ Object

Return a new factory object instance (or a singleton in some subclasses, e.g. nil).

Parameters:

  • args (Array)

    Class-specific initializer parameters.



20
# File 'lib/ltdtemplate/code.rb', line 20

def self.instance (*args); self.new(*args); end

Instance Method Details

#do_method(opts, class_name = nil) ⇒ Object

Try to execute code-block methods bound to the object or object class. Returns the return value from the code block or t-nil.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ltdtemplate/code.rb', line 80

def do_method (opts, class_name = nil)
	method = nil
	if name = opts[:method]
 if @tpl_methods.has_key? name
		method = @tpl_methods[name]
 elsif class_name
		class_var = @template.factory :variable, class_name
		method = class_var.target.tpl_methods[name] if
class_var.is_set?
 end
	end
	if method
 opts[:target] = self
 method.get_value opts
	else
 @template.factory :nil
	end
end

#do_set(opts) ⇒ LtdTemplate::Value::Nil

Implement “=” (assignment). Note that set_value is a no-op except for variables and array subscripts.

Parameters:

  • opts (Hash)

    A hash of method options.

Options Hash (opts):

  • :parameters (LtdTemplate::Value::Parameters)

    The method parameters.

Returns:



71
72
73
74
75
76
# File 'lib/ltdtemplate/code.rb', line 71

def do_set (opts)
	if params = opts[:parameters]
 set_value(params.scalar? ? params.positional[0] : params)
	end
	@template.factory :nil
end

#get_item(key) ⇒ LtdTemplate::Value::Code_Block

Return a non-array value’s method code block (if set).

Parameters:

  • key (String)

    The (native) string for the method.

Returns:



40
41
42
43
# File 'lib/ltdtemplate/code.rb', line 40

def get_item (key)
	(@tpl_methods.has_key? key) ? @tpl_methods[key] :
	  @template.factory(:nil)
end

#has_item?(key) ⇒ Boolean

Does a non-array value have a particular template method?

Parameters:

  • key (String)

    The (native) string for the method.

Returns:

  • (Boolean)


34
# File 'lib/ltdtemplate/code.rb', line 34

def has_item? (key); @tpl_methods.has_key? key; end

#is_set?true

Is this value set? Always true except for unset variables.

Returns:

  • (true)


62
# File 'lib/ltdtemplate/code.rb', line 62

def is_set?; true; end

#set_item(key, value) ⇒ Object

Set a non-array value’s method code block.

Parameters:



50
# File 'lib/ltdtemplate/code.rb', line 50

def set_item (key, value); @tpl_methods[key] = value; end

#set_value(value) ⇒ LtdTemplate::Code

No-op setting a value. (Typically only variables can change their primary values.)

Parameters:

  • value

    The value to set. (Ignored.)

Returns:



57
# File 'lib/ltdtemplate/code.rb', line 57

def set_value (value); self; end