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

Returns a new instance of Base.



16
17
18
19
20
21
22
# File 'lib/scruffy/components/base.rb', line 16

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



40
41
42
# File 'lib/scruffy/components/base.rb', line 40

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

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



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

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

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



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

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