public
Description: PLEASE GO TO http://yard.soen.ca FOR IMPORTANT NEWS ABOUT YARD / THE AUTHOR!!!
Homepage: http://yard.soen.ca / IRC: #yard on irc.freenode.net
Clone URL: git://github.com/lsegal/yard.git
Click here to lend your support to: yard and make a donation at www.pledgie.com !
yard / lib / yard / generators / base.rb
100644 104 lines (90 sloc) 2.828 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require 'erb'
 
module YARD
  module Generators
    class Base
      class << self
        def generator_name
          self.class.to_s.gsub(/Generator$/, '').downcase
        end
        
        def template_paths
          @template_paths ||= [YARD_TEMPLATE_ROOT]
        end
 
        ##
        # Convenience method to registering a template path.
        # Equivalent to calling:
        # GeneratorName.template_paths.unshift(path)
        #
        # @param [String] path
        # the pathname to look for the template
        #
        # @see template_paths
        def register_template_path(path)
          template_paths.unshift(path)
        end
        
        def generate(*args)
          new(*args).generate
        end
      end
 
      attr_accessor :format, :template, :serializer
      attr_reader :options
      
      def initialize(opts = {})
        opts = SymbolHash.new({
          :format => :html,
          :template => :default,
          :serializer => nil
        }).update(opts)
        
        self.format = opts[:format]
        self.template = opts[:template]
        self.serializer = opts[:serializer]
        self.options = opts
      end
      
      def generate(*list)
        serializer.before_serialize if serializer
        list.flatten.each do |object|
          (sections_for(object) || []).each do |section|
            data = render_section(section, object)
            serializer.serialize(object, data) if serializer
          end
        end
        serializer.after_serialize if serializer
      end
      
      protected
 
      def sections_for(object); [] end
 
      def render_section(section, object)
        begin
          if section == Generators::Base
            opts = options.dup.update(:serializer => nil)
            sobj = section.new(opts)
            sobj.generate(object)
          elsif section.is_a?(Generators::Base)
            sobj.generate(object)
          elsif section.is_a?(Symbol)
            if respond_to?(section)
              send(section, object)
            else
              raise ArgumentError
            end
          elsif section.is_a?(String)
            render(object, section)
          else
            raise ArgumentError
          end
        rescue ArgumentError
          YARD.logger.debug "Ignoring invalid section in #{self.class}"
        end
      end
      
      def render(object, file = nil)
        path = template_path(file)
        ERB.new(File.read(find_template(path))).result(binding)
      end
      
      def template_path(meth)
        File.join(template.to_s, generator_name, format.to_s, meth.to_s + ".erb")
      end
      
      def find_template(path)
        self.class.template_paths.find do |basepath|
          File.exist? File.join(basepath, path)
        end
      end
    end
  end
end