Class: Processing::Creator

Inherits:
BaseExporter show all
Defined in:
lib/ruby-processing/exporters/creator.rb

Overview

This class creates blank sketches, with the boilerplate filled in.

Constant Summary collapse

ALL_DIGITS =
/\A\d+\Z/

Constants inherited from BaseExporter

BaseExporter::DEFAULT_DESCRIPTION, BaseExporter::DEFAULT_DIMENSIONS, BaseExporter::NECESSARY_FOLDERS

Instance Method Summary collapse

Methods inherited from BaseExporter

#extract_class_name, #extract_description, #extract_dimension, #extract_information, #extract_libraries, #extract_real_requires, #extract_title, #get_main_file

Instance Method Details

#already_exist(path) ⇒ Object



36
37
38
39
40
41
# File 'lib/ruby-processing/exporters/creator.rb', line 36

def already_exist(path)
  if File.exist?(path) || File.exist?("#{File.dirname(path)}/#{main_file.underscore}.rb")
    puts 'That sketch already exists.'
  end
  exit
end

#create!(path, args, p3d) ⇒ Object

Create a blank sketch, given a path.



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/ruby-processing/exporters/creator.rb', line 10

def create!(path, args, p3d)
  usage if /\?/ =~ path || /--help/ =~ path
  main_file = File.basename(path, '.rb')
  # Check to make sure that the main file doesn't exist already
  already_exists(path)

  # Get the substitutions
  @name           = main_file.camelize
  @file_name      = main_file.underscore
  @title          = main_file.titleize

  @width, @height = args[0], args[1]
  @with_size      = @width && @width.match(ALL_DIGITS) &&
                    @height && @height.match(ALL_DIGITS)

  # Make the file
  dir = File.dirname path
  mkdir_p dir
  template_name = p3d ? 'p3d_sketch.rb.erb' : 'blank_sketch.rb.erb'
  template = File.new("#{RP5_ROOT}/lib/templates/create/#{template_name}")
  rendered = render_erb_from_string_with_binding(template.read, binding)
  full_path = File.join(dir, "#{@file_name}.rb")
  File.open(full_path, 'w') { |f| f.print(rendered) }
  puts "Created a new Sketch in #{full_path.sub(/\A\.\//, '')}"
end

#usageObject

Show the help/usage message for create.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ruby-processing/exporters/creator.rb', line 44

def usage
  puts <<-USAGE

  Usage: script/generate <sketch_to_generate> <width> <height>
  Width and Height are optional.

  Example: script/generate fancy_drawing/app 800 600

  USAGE
  exit
end