Class: Codger::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/codger/generator.rb

Overview

A code generator. The #run method is called to perform code generation; the parameters used (which may have been specified interactively during #run) can be determined afterwards using #params.

Subclasses must implement:

  • a #generate method which will perform the code generation

  • a #help method which returns help text

Methods for use by subclasses:

  • #dest_path

  • #ensure_folder

  • #param

  • #tags

Direct Known Subclasses

Skeleton

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#paramsObject (readonly)

The map of parameters used during the last call to #run.



28
29
30
# File 'lib/codger/generator.rb', line 28

def params
  @params
end

#targetObject (readonly)

The output directory used during the last call to #run.



30
31
32
# File 'lib/codger/generator.rb', line 30

def target
  @target
end

Class Method Details

.format_params(params) ⇒ Object

Given a params map, print one param per line, indented.



20
21
22
23
24
# File 'lib/codger/generator.rb', line 20

def format_params(params)
  YAML.dump(params).lines.drop(1).map do |line|
    "\t#{line}"
  end.join
end

Instance Method Details

#dest_path(path) ⇒ Object

Given a path relative to the output directory root, returns the full path.



44
45
46
# File 'lib/codger/generator.rb', line 44

def dest_path(path)
  File.join(target, path)
end

#ensure_folder(path) ⇒ Object

Given a path relative to the output directory root, creates a folder at that location if one does not yet exist.



50
51
52
# File 'lib/codger/generator.rb', line 50

def ensure_folder(path)
  FileUtils.mkdir_p(File.expand_path(File.join(target, File.dirname(path))))
end

#param(name) ⇒ Object

Returns the value from the params map for the given name. If there is none, asks the user for a value. #help is called the first time the user is asked for a value. The parameter will also be saved in an instance variable of the same name.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/codger/generator.rb', line 58

def param(name)
  until params[name]
    unless @showed_help
      puts help
      puts
      @showed_help = true
    end
    print "Specify #{name}: "
    value = STDIN.gets.chomp
    params[name] = value unless value.empty?
  end
  instance_variable_set("@#{name}", params[name])
  params[name]
end

#run(target, params = {}) ⇒ Object

Perform code generation in the given directory. Any parameters already known (e.g., if we’re repeating a previous run) can be specified; any other parameters needed will be determined interactively.



35
36
37
38
39
40
41
# File 'lib/codger/generator.rb', line 35

def run(target, params = {})
  @showed_help = false
  @target = target
  @params = params.with_indifferent_access

  generate
end

#tags(*tags) ⇒ Object

Sets (with parameters) or returns (without parameters) tags for this generator. The tags will be associated to recorded runs. (This may be useless, we’ll see.)



76
77
78
79
80
81
82
# File 'lib/codger/generator.rb', line 76

def tags(*tags)
  if tags == []
    @tags || []
  else
    @tags = tags.flatten
  end
end