Class: Shaf::Generator::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.



29
30
31
# File 'lib/shaf/generator.rb', line 29

def initialize(*args)
  @args = args.dup
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



13
14
15
# File 'lib/shaf/generator.rb', line 13

def args
  @args
end

Class Method Details

.identifier(*ids) ⇒ Object



20
21
22
# File 'lib/shaf/generator.rb', line 20

def identifier(*ids)
  @identifiers = ids.flatten
end

.inherited(child) ⇒ Object



16
17
18
# File 'lib/shaf/generator.rb', line 16

def inherited(child)
  Factory.register(child)
end

.usage(str = nil, &block) ⇒ Object



24
25
26
# File 'lib/shaf/generator.rb', line 24

def usage(str = nil, &block)
  @usage = str || block
end

Instance Method Details

#read_template(file, directory = nil) ⇒ Object



37
38
39
40
41
42
# File 'lib/shaf/generator.rb', line 37

def read_template(file, directory = nil)
  directory ||= template_dir
  filename = File.join(directory, file)
  filename << ".erb" unless filename.end_with?(".erb")
  File.read(filename)
end

#render(template, locals = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/shaf/generator.rb', line 44

def render(template, locals = {})
  str = read_template(template)
  locals[:changes] ||= []
  b = OpenStruct.new(locals).instance_eval { binding }
  ERB.new(str, 0, '%-<>').result(b)
rescue SystemCallError => e
  puts "Failed to render template #{template}: #{e.message}"
  raise
end

#template_dirObject



33
34
35
# File 'lib/shaf/generator.rb', line 33

def template_dir
  File.expand_path('../generator/templates', __FILE__)
end

#write_output(file, content) ⇒ Object



54
55
56
57
58
59
# File 'lib/shaf/generator.rb', line 54

def write_output(file, content)
  dir = File.dirname(file)
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
  File.write(file, content)
  puts "Added:      #{file}"
end