Class: Rubicus::Layers::Prefab

Inherits:
Base
  • Object
show all
Defined in:
lib/rubicus/layers/prefab.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Prefab

Returns a new instance of Prefab.



3
4
5
6
7
8
# File 'lib/rubicus/layers/prefab.rb', line 3

def initialize(options = {})
  @type = options.delete(:prefab)
  @data = options.delete(:data)
  @options = options
  @options[:delim] ||= :comma
end

Instance Method Details

#draw(image_type = "png", options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubicus/layers/prefab.rb', line 10

def draw(image_type = "png", options = {})
  options[image_type] = nil if image_type
  options[:prefab] ||= @type
  
  if @data
    data = if @data[0].kind_of? Array
      @data.map {|row| row.join(delimiter_for_delim(@options[:delim])) }.join("\n")
    else
      @data.join(",")
    end
  else
    data = nil
  end
  
  ploticus_options = options.map { |key, option| "-#{key} #{convert_options(option)}" }.join(" ")
  prefab_options = @options.map { |key, option| "#{key}=\"#{convert_options(option)}\"" }.join(" ")
  io = IO.popen("ploticus #{@data ? "data=stdin" : ""} #{ploticus_options} #{prefab_options}", "w+")
  if @data
    io.write data
    io.close_write
  end
  img = io.read
  io.close
  img
end