Class: Tamebou::Writer
- Inherits:
-
Object
- Object
- Tamebou::Writer
- Defined in:
- lib/tamebou/writer.rb
Defined Under Namespace
Modules: DefaultTemplate
Instance Method Summary collapse
-
#initialize(path, template_path = DefaultTemplate::MINITEST, is_warning_parse_failure = false) ⇒ Writer
constructor
A new instance of Writer.
- #print_test_code ⇒ Object
- #set_model_name ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(path, template_path = DefaultTemplate::MINITEST, is_warning_parse_failure = false) ⇒ Writer
Returns a new instance of Writer.
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/tamebou/writer.rb', line 8 def initialize(path, template_path=DefaultTemplate::MINITEST, is_warning_parse_failure=false) @template_path = case template_path when DefaultTemplate::MINITEST File.join(File.dirname(__FILE__), '../templates/minitest.txt.erb') when DefaultTemplate::RSPEC File.join(File.dirname(__FILE__), '../templates/rspec.txt.erb') else template_path end @is_warning_parse_failure = is_warning_parse_failure @path = path set_model_name end |
Instance Method Details
#print_test_code ⇒ Object
66 67 68 69 70 71 |
# File 'lib/tamebou/writer.rb', line 66 def print_test_code puts "=============================================" erb = ::ERB.new(File.read(@template_path)) puts erb.result(binding) puts "=============================================" end |
#set_model_name ⇒ Object
61 62 63 64 |
# File 'lib/tamebou/writer.rb', line 61 def set_model_name @model_name_in_snake_case = @path.match(/\/([^\/]+).rb$/)[1] @model_name = @model_name_in_snake_case.split("_").map{|w| w[0] = w[0].upcase; w}.join end |
#write ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/tamebou/writer.rb', line 22 def write begin File.open(@path) do |file| file.each_line do |line| parse_result = Parser.parse(line) if parse_result.nil? warning_parse_failure line if @is_warning_parse_failure next end @field_name = parse_result[:field_name] unless parse_result[:options].is_a? Hash parse_result if @is_warning_parse_failure @option_name = "unknown" @helper = Module.const_get("Tamebou::Helpers::Base").send(:new, {}) print_test_code next end parse_result[:options].each do |option_name, option_value| @option_name = option_name helper_class_name = option_name.capitalize @helper = begin Module.const_get("Tamebou::Helpers::#{helper_class_name}").send(:new, option_value) rescue NoMethodError, NameError => e warning_not_found_helper if @is_warning_parse_failure Module.const_get("Tamebou::Helpers::Base").send(:new, option_value) end print_test_code end end end rescue Exception => e puts e end end |