Sha256: 9ce0a518f11d93c2ef6afde070f0a879efa8326034e589f80f59cb4825c2608e
Contents?: true
Size: 1.93 KB
Versions: 2
Compression:
Stored size: 1.93 KB
Contents
require 'canpe/template_binding' module Canpe class TemplateRenderer attr_accessor :repository_operation, :injected_hash def initialize(repository_operation, hash = {}.with_indifferent_access) @repository_operation = repository_operation @injected_hash = hash end def repository repository_operation.repository end def prepare(options) if options.key? :variables injected_hash.merge!(options[:variables]) return end if repository.binding_options['variables'].blank? return puts 'skip variable injection.' end puts 'you need to set variables to generate codes!' repository.binding_options['variables'].each.with_index(1) do |entry, index| puts "#{index}: #{entry['name']} (#{entry['type']}) " end puts '' puts 'If you want to stop setting array, let it blank and press enter.' hash = {} repository.binding_options['variables'].each do |entry| if entry['type'] == 'string' print "#{entry['name']} ?) " hash[entry['name']] = STDIN.gets.chomp elsif entry['type'] == 'array' array = [] loop do print "#{entry['name']}[#{array.size}] ?) " input = STDIN.gets.chomp if input.present? array << input else break end end hash[entry['name']] = array end end injected_hash.merge!(hash) puts "finished variable settings: #{hash}" end def render_string(str) template = Tilt::ERBTemplate.new(trim: false) { str.to_s } template.render(Canpe::TemplateBinding.new(injected_hash)) end def render_file(path) Tempfile.new.tap do |file| template = Tilt::ERBTemplate.new(path, trim: false) file.write(template.render(Canpe::TemplateBinding.new(injected_hash))) file.rewind end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
canpe-0.1.1 | lib/canpe/template_renderer.rb |
canpe-0.1.0 | lib/canpe/template_renderer.rb |