Class: PageTemplate::ValueCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/PageTemplate/commands.rb

Overview

ValueCommand will print out a variable name, possibly passing its value through a preprocessor.

% var variable [:processor

%]

variable is first plucked out of the Namespace,

The to_s of the output from Namespace#get is passed through parser's preprocessor. If :processor is defined, then parser.preprocessor.send(:processor,body) is called. This allows the designer to choose a particular format for the output. If :processor is not given, then parser's default processor is used.

Instance Attribute Summary

Attributes inherited from Command

#called_as

Instance Method Summary collapse

Constructor Details

#initialize(value, preprocessor) ⇒ ValueCommand

Creates the ValueCommand, with value as the name of the variable to be inserted during output. parser is the Parser object, which we save so we can access its Preprocessor and its default preprocessor


274
275
276
277
# File 'lib/PageTemplate/commands.rb', line 274

def initialize(value,preprocessor)
  @value = value
  @processor = preprocessor
end

Instance Method Details

#output(namespace = nil) ⇒ Object

Requests the value of this object's saved value name from namespace, and returns the string representation of that value to the caller, after passing it through the preprocessor.


282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/PageTemplate/commands.rb', line 282

def output(namespace = nil)
  parser = namespace.parser if namespace
  parser = Parser.recent_parser unless parser
  namespace ||= parser
  preprocessor = parser.preprocessor
  processor = @processor || parser.default_processor
  if preprocessor.respond_to?(processor)
    preprocessor.send(processor,namespace.get(@value).to_s)
  else
    "[ ValueCommand: unknown preprocessor #{@processor} ]"
  end
end

#to_sObject


295
296
297
298
299
300
# File 'lib/PageTemplate/commands.rb', line 295

def to_s
  str = "[ Value: #{@value} "
  str << ":#{@processor} " if (@processor)
  str << ']'
  str
end