Class: TaskJuggler::Painter::Group

Inherits:
Object
  • Object
show all
Includes:
Primitives, SVGSupport
Defined in:
lib/taskjuggler/Painter/Group.rb

Overview

The Group can be used to group Elements together and define common attributes in a single place.

Constant Summary

Constants included from Primitives

Primitives::FillAndStrokeAttrs, Primitives::FillAttrs, Primitives::StrokeAttrs, Primitives::TextAttrs

Instance Method Summary collapse

Methods included from SVGSupport

#valuesToSVG

Methods included from Primitives

#circle, #color, #ellipse, #group, #line, #points, #polyline, #rect, #text

Constructor Details

#initialize(values, &block) ⇒ Group

Returns a new instance of Group.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/taskjuggler/Painter/Group.rb', line 28

def initialize(values, &block)
  @attributes = [
    :fill, :font_family, :font_size, :stroke, :stroke_width
  ]
  values.each do |k, v|
    unless @attributes.include?(k)
      raise ArgumentError, "Unsupported attribute #{k}. " +
                           "Use one of #{@attributes.join(', ')}."
    end
  end

  @values = values
  @elements = []

  if block
    if block.arity == 1
      # This is the traditional case where self is passed to the block.
      # All Primitives methods now must be prefixed with the block
      # variable to call them.
      yield self
    else
      # In order to have the primitives easily available in the block,
      # we use instance_eval to switch self to this object. But this
      # makes the methods of the original self no longer accessible. We
      # work around this by saving the original self and using
      # method_missing to delegate the method call to the original self.
      @originalSelf = eval('self', block.binding)
      instance_eval(&block)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegator to @originalSelf.



61
62
63
# File 'lib/taskjuggler/Painter/Group.rb', line 61

def method_missing(method, *args, &block)
  @originalSelf.send(method, *args, &block)
end

Instance Method Details

#to_svgObject

Convert the Group into an XMLElement tree using SVG syntax.



66
67
68
69
70
# File 'lib/taskjuggler/Painter/Group.rb', line 66

def to_svg
  XMLElement.new('g', valuesToSVG) do
    @elements.map { |el| el.to_svg }
  end
end