Class: Code

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

Overview

– Copyright © 2010 weizhao

Direct Known Subclasses

CompositeCode, PatternCode

Instance Attribute Summary collapse

Instance Method Summary collapse

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_valuesObject

Returns the value of attribute replace_values.



5
6
7
# File 'lib/code.rb', line 5

def replace_values
  @replace_values
end

#target_file_nameObject

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_nameObject

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

#executeObject



31
32
33
# File 'lib/code.rb', line 31

def execute
  generate_code
end

#generate_codeObject



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_templateObject



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