Class: Scruffy::Components::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/scruffy/components/base.rb

Overview

Scruffy::Components::Base

Common attributes for all components, and a standard render method that calls draw after setting up the drawing transformations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, options = {}) ⇒ Base

Options:

stroke_width

numeric value for width of line (0.1 - 10, default: 1)



18
19
20
21
22
23
24
25
# File 'lib/scruffy/components/base.rb', line 18

def initialize(id, options = {})
  @id = id.to_sym
  @position = options[:position] || [0, 0]
  @size = options[:size] || [100, 100]
  @visible = options[:visible] || true
  
  @options = options
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/scruffy/components/base.rb', line 8

def id
  @id
end

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/scruffy/components/base.rb', line 13

def options
  @options
end

#positionObject

In terms of percentages: [10, 10] == 10% by 10%



11
12
13
# File 'lib/scruffy/components/base.rb', line 11

def position
  @position
end

#sizeObject

Returns the value of attribute size.



12
13
14
# File 'lib/scruffy/components/base.rb', line 12

def size
  @size
end

#visibleObject

Returns the value of attribute visible.



14
15
16
# File 'lib/scruffy/components/base.rb', line 14

def visible
  @visible
end

Instance Method Details

#draw(svg, bounds, options = {}) ⇒ Object



43
44
45
# File 'lib/scruffy/components/base.rb', line 43

def draw(svg, bounds, options={})
  # Override this if visual component
end

#process(svg, options = {}) ⇒ Object



47
48
49
# File 'lib/scruffy/components/base.rb', line 47

def process(svg, options={})
  # Override this NOT a visual component
end

#render(svg, bounds, options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/scruffy/components/base.rb', line 27

def render(svg, bounds, options={})
  if @visible
    unless bounds.nil?
      @render_height = bounds[:height]
  
      svg.g(:id => id.to_s, 
            :transform => "translate(#{bounds.delete(:x)}, #{bounds.delete(:y)})") {

        draw(svg, bounds, options.merge(@options))
      }
    else
      process(svg, options.merge(@options))
    end
  end
end