Class: DYI::Script::EcmaScript::Function

Inherits:
SimpleScript show all
Defined in:
lib/dyi/script/ecmascript.rb

Overview

Class representing a function of ECMAScript. The scripting becomes effective only when it is output by SVG format.

Since:

  • 1.0.0

Direct Known Subclasses

EventListener

Instance Attribute Summary collapse

Attributes inherited from SimpleScript

#body, #content_type

Instance Method Summary collapse

Methods inherited from SimpleScript

#append_body, #has_uri_reference?, #include_external_file?, #write_as

Constructor Details

#initialize(body, name = nil, *arguments) ⇒ Function

Returns a new instance of Function.

Parameters:

  • body (String)

    function body on ECMAScript

  • name (String) (defaults to: nil)

    a function identifier on ECMAScript

  • arguments (Array<String>)

    a list of formal parameter identifiers on ECMAScript

Since:

  • 1.0.0



346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/dyi/script/ecmascript.rb', line 346

def initialize(body, name=nil, *arguments)
  super(body)
  if name && name !~ /\A[\$A-Z_a-z][\$0-9A-Z_a-z]*\z/
    raise ArgumentError, "illegal identifier: `#{name}'"
  end
  @name = name
  @arguments = arguments.map do |arg|
    if arg.to_s !~ /\A[\$A-Z_a-z][\$0-9A-Z_a-z]*\z/
      raise ArgumentError, "illegal identifier: `#{arg}'"
    end
    arg.to_s
  end
end

Instance Attribute Details

#argumentsArray<String> (readonly)

Returns an array of formal parameter identifiers on ECMAScript.

Returns:

  • (Array<String>)

    an array of formal parameter identifiers

Since:

  • 1.0.0



340
341
342
# File 'lib/dyi/script/ecmascript.rb', line 340

def arguments
  @arguments
end

#nameString? (readonly)

Returns a function identifier on ECMAScript.

Returns:

  • (String, nil)

    a function identifier if the function has a identifier on ECMAScript, nil otherwise

Since:

  • 1.0.0



337
338
339
# File 'lib/dyi/script/ecmascript.rb', line 337

def name
  @name
end

Instance Method Details

#contentsString

Returns string expression of this function in ECMAScript

Returns:

  • (String)

    expression in ECMAScript

Since:

  • 1.0.3



363
364
365
366
367
368
369
370
371
372
373
# File 'lib/dyi/script/ecmascript.rb', line 363

def contents
  parts = []
  parts << 'function'
  parts << " #{name}" if name
  parts << '('
  parts << arguments.join(', ')
  parts << "){\n"
  parts << @body
  parts << "\n}"
  parts.join
end