Class: Sketch::Builder

Inherits:
Object
  • Object
show all
Includes:
DSL
Defined in:
lib/sketch/builder.rb,
lib/sketch/builder/path.rb,
lib/sketch/builder/polygon.rb,
lib/sketch/builder/polyline.rb

Defined Under Namespace

Classes: Path, Polygon, Polyline

Convenience constants collapse

Point =
Geometry::Point
Rectangle =
Geometry::Rectangle
Size =
Geometry::Size

Instance Attribute Summary collapse

Attributes included from DSL

#first, #last

Accessors collapse

Command handlers collapse

Instance Method Summary collapse

Methods included from DSL

#hexagon, #layout, #path, #polygon

Constructor Details

#initialize(sketch = nil, &block) ⇒ Builder

Returns a new instance of Builder.



17
18
19
20
# File 'lib/sketch/builder.rb', line 17

def initialize(sketch=nil, &block)
    @sketch = sketch || Sketch.new
    evaluate(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

The second half of the instance_eval delegation trick mentioned at

http://www.dan-manges.com/blog/ruby-dsls-instance-eval-with-delegation


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sketch/builder.rb', line 36

def method_missing(method, *args, &block)
    add_symbol = ('add_' + method.to_s).to_sym
    if @sketch.respond_to? add_symbol
	@sketch.send(add_symbol, *args, &block)
    elsif @sketch.respond_to? method
	@sketch.send method, *args, &block
    elsif @self_before_instance_eval.respond_to? method
	@self_before_instance_eval.send method, *args, &block
    else
	super if defined?(super)
    end
end

Instance Attribute Details

#sketchObject (readonly)

Returns the value of attribute sketch.



7
8
9
# File 'lib/sketch/builder.rb', line 7

def sketch
  @sketch
end

Instance Method Details

#elementsObject

!@attribute [r] elements

@return [Array] The current list of elements


53
54
55
# File 'lib/sketch/builder.rb', line 53

def elements
    @sketch.elements
end

#evaluate(&block) ⇒ Sketch

Evaluate a block and return a new Path

Use the trick found here http://www.dan-manges.com/blog/ruby-dsls-instance-eval-with-delegation
to allow the DSL block to call methods in the enclosing *lexical* scope

Returns:

  • (Sketch)

    A new Sketch initialized with the given block



26
27
28
29
30
31
32
# File 'lib/sketch/builder.rb', line 26

def evaluate(&block)
    if block_given?
	@self_before_instance_eval = eval "self", block.binding
	self.instance_eval &block
    end
    @sketch
end

#group(*args, &block) ⇒ Object

Create a Group with an optional name and transformation



69
70
71
# File 'lib/sketch/builder.rb', line 69

def group(*args, &block)
    @sketch.push Sketch::Builder.new(Group.new(*args)).evaluate(&block)
end

#let(name, &block) ⇒ Object

Define a named parameter

Parameters:

  • name (Symbol)

    The name of the parameter

  • block (Proc)

    A block that evaluates to the value of the parameter



62
63
64
# File 'lib/sketch/builder.rb', line 62

def let name, &block
    @sketch.define_parameter name, &block
end

#polyline(&block) ⇒ Object

Use the given block to build a Polyline and then append it to the Sketch



74
75
76
# File 'lib/sketch/builder.rb', line 74

def polyline(&block)
    push Builder::Polyline.new.evaluate(&block)
end

#push(*args) ⇒ Sketch

Append a new object (with optional transformation) to the Sketch

Returns:



80
81
82
# File 'lib/sketch/builder.rb', line 80

def push(*args)
    @sketch.push *args
end

#rectangle(*args) ⇒ Object

Create a Rectangle from the given arguments and append it to the Sketch



85
86
87
88
# File 'lib/sketch/builder.rb', line 85

def rectangle(*args)
    @sketch.push Rectangle.new(*args)
    last
end

#translate(*args, &block) ⇒ Object

Create a Group using the given translation

Parameters:

  • point (Point)

    The distance by which to translate the enclosed geometry

Raises:

  • (ArgumentError)


92
93
94
95
96
# File 'lib/sketch/builder.rb', line 92

def translate(*args, &block)
    point = Point[*args]
    raise ArgumentError, 'Translation is limited to 2 dimensions' if point.size > 2
    group(origin:point, &block)
end