Class: Alonzo::Generator

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

Defined Under Namespace

Classes: RbClassGenerator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options = {}) ⇒ Generator

Returns a new instance of Generator.



92
93
94
95
96
97
98
99
100
# File 'lib/alonzo/generator.rb', line 92

def initialize(args, options = {})
  self.args   = args
  self.output = options.delete(:output) || STDOUT
  self.root   = options.delete(:root) || Dir.pwd

  generator_klass_name = "#{options[:type]}_generator".classify
  @generator_class = "#{self.class.name}::#{generator_klass_name}".constantize
  @generator       = generator_class.new(args, root)
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



84
85
86
# File 'lib/alonzo/generator.rb', line 84

def args
  @args
end

#generatorObject (readonly)

Returns the value of attribute generator.



85
86
87
# File 'lib/alonzo/generator.rb', line 85

def generator
  @generator
end

#generator_classObject (readonly)

Returns the value of attribute generator_class.



85
86
87
# File 'lib/alonzo/generator.rb', line 85

def generator_class
  @generator_class
end

#outputObject

Returns the value of attribute output.



84
85
86
# File 'lib/alonzo/generator.rb', line 84

def output
  @output
end

#rootObject

Returns the value of attribute root.



84
85
86
# File 'lib/alonzo/generator.rb', line 84

def root
  @root
end

Class Method Details

.generate(args, options = {}) ⇒ Object



87
88
89
90
# File 'lib/alonzo/generator.rb', line 87

def self.generate(args, options = {})
  generator = new(args, options)
  generator.generate
end

Instance Method Details

#generateObject



102
103
104
105
106
107
108
109
# File 'lib/alonzo/generator.rb', line 102

def generate
  output.puts "Generating #{generator_class} files"
  generator.files.each do |file|
    output.puts "    #{file[:name]}"
    FileUtils.mkdir_p(File.dirname(file[:name]))
    File.open(file[:name], "w") { |f| f << file[:content] }
  end
end