Class: Code
- Inherits:
-
Object
- Object
- Code
- Defined in:
- lib/code.rb
Overview
– Copyright © 2010 weizhao
Direct Known Subclasses
Instance Attribute Summary collapse
-
#replace_values ⇒ Object
Returns the value of attribute replace_values.
-
#target_file_name ⇒ Object
Returns the value of attribute target_file_name.
-
#template_file_name ⇒ Object
Returns the value of attribute template_file_name.
Instance Method Summary collapse
- #execute ⇒ Object
- #generate_code ⇒ Object
-
#initialize(template_file_name, target_file_bame, replace_values) ⇒ Code
constructor
A new instance of Code.
- #replace_template ⇒ Object
Constructor Details
#initialize(template_file_name, target_file_bame, replace_values) ⇒ Code
Returns a new instance of Code.
6 7 8 9 10 |
# File 'lib/code.rb', line 6 def initialize(template_file_name,target_file_bame,replace_values) @template_file_name = template_file_name @target_file_name = target_file_bame @replace_values = replace_values end |
Instance Attribute Details
#replace_values ⇒ Object
Returns the value of attribute replace_values.
5 6 7 |
# File 'lib/code.rb', line 5 def replace_values @replace_values end |
#target_file_name ⇒ Object
Returns the value of attribute target_file_name.
5 6 7 |
# File 'lib/code.rb', line 5 def target_file_name @target_file_name end |
#template_file_name ⇒ Object
Returns the value of attribute template_file_name.
5 6 7 |
# File 'lib/code.rb', line 5 def template_file_name @template_file_name end |
Instance Method Details
#execute ⇒ Object
31 32 33 |
# File 'lib/code.rb', line 31 def execute generate_code end |
#generate_code ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/code.rb', line 20 def generate_code if File.exist?(@target_file_name) puts "* File #{@target_file_name} exist." return false end File.open(@target_file_name,"w") do |file| puts "* Create new file #{@target_file_name}." file.puts replace_template end end |
#replace_template ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/code.rb', line 11 def replace_template template_string = File.open(@template_file_name).readlines.join @replace_values.each do |key,value| template_string = template_string.gsub("##{key}#",value) end return template_string end |