Class: TrianglePattern::TriangleTask

Inherits:
RakeTask
  • Object
show all
Defined in:
lib/triangle_pattern/triangle_task.rb

Instance Attribute Summary collapse

Attributes inherited from RakeTask

#description, #name, #verbose

Instance Method Summary collapse

Methods inherited from RakeTask

#include, #instance_binding

Constructor Details

#initialize(opts = {}) ⇒ TriangleTask

Returns a new instance of TriangleTask.

Raises:

  • (ArgumentError)


7
8
9
10
11
# File 'lib/triangle_pattern/triangle_task.rb', line 7

def initialize(opts = {})
  super
  raise ArgumentError, :data if @options[:data].nil?
  @data             = @options[:data]
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/triangle_pattern/triangle_task.rb', line 6

def data
  @data
end

Instance Method Details

#run_task(_verbose) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/triangle_pattern/triangle_task.rb', line 13

def run_task(_verbose)
  data.each do |path, string|
    opts = {}
    path = File.expand_path(path)

    if string.is_a?(Hash)
      opts[:colors]   = string[:colors] if string.key? :colors
      opts[:width]      = string[:width] if string.key? :width
      opts[:height] = string[:height] if string.key? :height
      opts[:cell_size] = string[:cell_size] if string.key? :cell_size
      opts[:seed] = string[:seed] if string.key? :seed
      opts[:variance] = string[:variance] if string.key? :variance
    else
      raise 'Invalid data structure for Rake Task'
    end

    pattern = TrianglePattern.generate(opts)

    logger.info "Creating pattern at \"#{path}\"."
    FileUtils.mkdir_p File.dirname(path)
    File.write(path, pattern.to_svg)
  end
end