Class: Sketch::Builder
- Inherits:
-
Object
- Object
- Sketch::Builder
- 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
Instance Attribute Summary collapse
-
#sketch ⇒ Object
readonly
Returns the value of attribute sketch.
Attributes included from DSL
Accessors collapse
-
#elements ⇒ Object
!@attribute [r] elements @return [Array] The current list of elements.
Command handlers collapse
-
#group(*args, &block) ⇒ Object
Create a Group with an optional name and transformation.
- #polyline(&block) ⇒ Object
-
#push(*args) ⇒ Sketch
Append a new object (with optional transformation) to the Sketch.
- #rectangle(*args) ⇒ Object
-
#translate(*args, &block) ⇒ Object
Create a Group using the given translation.
Instance Method Summary collapse
-
#evaluate(&block) ⇒ Sketch
Evaluate a block and return a new Path Use the trick found here www.dan-manges.com/blog/ruby-dsls-instance-eval-with-delegation to allow the DSL block to call methods in the enclosing lexical scope.
-
#initialize(sketch = nil, &block) ⇒ Builder
constructor
A new instance of Builder.
-
#let(name, &block) ⇒ Object
Define a named parameter.
-
#method_missing(method, *args, &block) ⇒ Object
The second half of the instance_eval delegation trick mentioned at www.dan-manges.com/blog/ruby-dsls-instance-eval-with-delegation.
Methods included from DSL
#hexagon, #layout, #path, #polygon
Constructor Details
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
#sketch ⇒ Object (readonly)
Returns the value of attribute sketch.
7 8 9 |
# File 'lib/sketch/builder.rb', line 7 def sketch @sketch end |
Instance Method Details
#elements ⇒ Object
!@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
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
62 63 64 |
# File 'lib/sketch/builder.rb', line 62 def let name, &block @sketch.define_parameter name, &block end |
#polyline(&block) ⇒ Object
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
80 81 82 |
# File 'lib/sketch/builder.rb', line 80 def push(*args) @sketch.push *args end |