Module: LeapSalesforce::Generator
- Included in:
- SoqlEnums, SoqlObjects
- Defined in:
- lib/leap_salesforce/generator/generator.rb,
lib/leap_salesforce/generator/soql_enums.rb,
lib/leap_salesforce/generator/soql_objects.rb
Overview
Generators for creating code
Defined Under Namespace
Classes: SoqlEnums, SoqlObjects
Instance Method Summary collapse
- #create_inner_file(folder, name, binding) ⇒ Object
-
#generate_file(filename, content, overwrite: true) ⇒ Object
Generate file creating necessary folder if necessary.
- #generate_files(binding, list_of_files, folder: nil) ⇒ Object
-
#generate_from_template(template_name, binding) ⇒ Object
Generate file from template.
-
#read_template(filename, binding, folder: nil) ⇒ String
Interpreted file after calculating ERB for template file passed.
-
#template_loc(filename, folder: nil) ⇒ String
Location of template file.
Instance Method Details
#create_inner_file(folder, name, binding) ⇒ Object
62 63 64 65 66 |
# File 'lib/leap_salesforce/generator/generator.rb', line 62 def create_inner_file(folder, name, binding) item = name.to_s path = folder ? File.join(folder, item) : item generate_from_template path, binding end |
#generate_file(filename, content, overwrite: true) ⇒ Object
Generate file creating necessary folder if necessary
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/leap_salesforce/generator/generator.rb', line 30 def generate_file(filename, content, overwrite: true) FileUtils.mkdir_p File.dirname filename unless File.directory? File.dirname(filename) action = if File.exist?(filename) return puts "File '#{filename}' already exists, skipping...".colorize :red unless overwrite 'Updated' else 'Created' end File.write filename, content puts "\u2713 #{action} #{filename}".colorize :green end |
#generate_files(binding, list_of_files, folder: nil) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/leap_salesforce/generator/generator.rb', line 46 def generate_files(binding, list_of_files, folder: nil) return create_inner_file(folder, list_of_files, binding) unless list_of_files.respond_to? :each list_of_files.each do |item| if item.is_a? Hash # Go one level deeper item.each do |inner_folder, sub_files| inner_folder = inner_folder.to_s current_folder = folder ? File.join(folder, inner_folder) : inner_folder generate_files binding, sub_files, folder: current_folder end else create_inner_file folder, item, binding end end end |
#generate_from_template(template_name, binding) ⇒ Object
Generate file from template
71 72 73 74 |
# File 'lib/leap_salesforce/generator/generator.rb', line 71 def generate_from_template(template_name, binding) content = read_template "#{template_name}.erb", binding generate_file template_name, content, overwrite: false end |
#read_template(filename, binding, folder: nil) ⇒ String
Returns Interpreted file after calculating ERB for template file passed.
23 24 25 |
# File 'lib/leap_salesforce/generator/generator.rb', line 23 def read_template(filename, binding, folder: nil) ERB.new(File.read(template_loc(filename, folder: folder))).result(binding) end |
#template_loc(filename, folder: nil) ⇒ String
Returns Location of template file.
14 15 16 17 |
# File 'lib/leap_salesforce/generator/generator.rb', line 14 def template_loc(filename, folder: nil) template_dir = folder || __dir__ File.join(template_dir, 'templates', filename) end |