Class: Elastics::ClassProxy::Templates::Doc::Output
- Inherits:
-
Object
- Object
- Elastics::ClassProxy::Templates::Doc::Output
- Defined in:
- lib/elastics/class_proxy/templates/doc.rb
Instance Attribute Summary collapse
-
#method_call ⇒ Object
readonly
Returns the value of attribute method_call.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
- #comments_to_s(*comments) ⇒ Object
-
#initialize(name, proxy) ⇒ Output
constructor
A new instance of Output.
- #render ⇒ Object
- #render_api ⇒ Object
- #render_custom ⇒ Object
- #render_sources ⇒ Object
- #render_stub ⇒ Object
- #render_usage(indent = '') ⇒ Object
- #to_code(name) ⇒ Object
Constructor Details
#initialize(name, proxy) ⇒ Output
Returns a new instance of Output.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 11 def initialize(name, proxy) @name, @template = proxy.templates.find do |n, t| t.is_a?(Elastics::Template::Api) && t.aliases.include?(name) end || [ name, proxy.templates[name]] @method_call = [proxy.context, @name].join('.') sources = [] sources << { :class => @template.class, :source => @template.to_source } @template.partials.each do |name| partial = proxy.partials[name] sources << { :class => partial.class, :source => partial.to_source } end @sources = sources end |
Instance Attribute Details
#method_call ⇒ Object (readonly)
Returns the value of attribute method_call.
9 10 11 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 9 def method_call @method_call end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
9 10 11 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 9 def template @template end |
Instance Method Details
#comments_to_s(*comments) ⇒ Object
60 61 62 63 64 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 60 def comments_to_s(*comments) comments = comments.compact return '' if comments == [] "# #{comments.join(' ')}" end |
#render ⇒ Object
126 127 128 129 130 131 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 126 def render output = (@template.is_a?(Elastics::Template::Api) ? render_api : render_custom) output = output.split("\n").map{ |l| '# ' + l }.join("\n") output << "\n#{render_stub}\n\n" output end |
#render_api ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 87 def render_api <<-output.gsub(/^ {12}/m,'') ########## #{@method_call} ########## API Name: #{@template.references['api_name']} API URL: #{@template.references['api_url']} #{render_sources} Usage: #{render_usage(' ' * 12)} output end |
#render_custom ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 101 def render_custom <<-output.gsub(/^ {12}/m,'') ########## #{@method_call} ########## #{render_sources} Usage: #{render_usage(' ' * 12)} output end |
#render_sources ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 76 def render_sources @sources.map do |source| <<-output.gsub(/^ {14}/m,'') #{'-' * source[:class].name.length} #{source[:class]} #{source[:source]} output end.join("\n") end |
#render_stub ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 113 def render_stub aliased = if @template.is_a?(Elastics::Template::Api) && @template.references['aliases'] "\n# also aliased by: #{@template.aliases.map(&:inspect).join(', ')}" end <<-output.gsub(/^ {12}/m,'') def self.#{@name}(*vars) ## this is a stub, used for reference super end#{ aliased } output end |
#render_usage(indent = '') ⇒ Object
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 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 29 def render_usage(indent='') variables = @template.instance_eval do interpolate @base_variables.deep_merge @host_elastics && @host_elastics.variables, @temp_variables end = @template. + @template.partials return @method_call if .size == 0 lines = .map do |t| comments = 'partial' if t.to_s[0] == '_' line = ['', t.inspect] line + if variables.has_key?(t) ["#{variables[t].inspect},", comments_to_s(comments)] else ["#{to_code(t)},", comments_to_s(comments, 'required')] end end lines.sort! { |a,b| b[3] <=> a[3] } lines.first[0] = @method_call lines.last[2].chop! max = lines.transpose.map{ |c| c.map(&:length).max } formatted = lines.map{ |line| "%-#{max[0]}s %-#{max[1]}s => %-#{max[2]}s %s" % line } indented = formatted.shift indented = [indented] + formatted.map{ |line| indent + line } indented = indented.join("\n") + "\n " notice = if @template.is_a?(Elastics::Template::Api) && @template.references['notice'] "\nNotice: #{@template.references['notice']}\n " end indented + notice end |
#to_code(name) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/elastics/class_proxy/templates/doc.rb', line 67 def to_code(name) keys = name.to_s.split('.').map{ |s| s =~ /^[0..9]+$/ ? s.to_i : s.to_sym } code = keys.shift.to_s return code if keys.empty? keys.each { |k| code << "[#{k.inspect}]" } code end |