Class: DNote::Format
- Inherits:
-
Object
- Object
- DNote::Format
- Defined in:
- lib/dnote/format.rb
Overview
Notes Formatter
–
TODO: Need good CSS file.
TODO: Need XSL?
++
Defined Under Namespace
Classes: ErbScope
Constant Summary collapse
- EXTENSIONS =
DEFAULT_OUTPUT_DIR = “log/dnote”
{ 'text'=>'txt', 'soap'=>'xml', 'xoxo'=>'xml' }
Instance Attribute Summary collapse
-
#dryrun ⇒ Object
Returns the value of attribute dryrun.
-
#format ⇒ Object
Returns the value of attribute format.
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
-
#output ⇒ Object
Returns the value of attribute output.
-
#subtype ⇒ Object
Returns the value of attribute subtype.
-
#template ⇒ Object
Returns the value of attribute template.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #debug? ⇒ Boolean private
- #dryrun? ⇒ Boolean private
- #erb(file) ⇒ Object private
- #fu ⇒ Object private
-
#initialize(notes, options = {}) {|_self| ... } ⇒ Format
constructor
A new instance of Format.
- #publish(result, fname = nil) ⇒ Object private
- #render ⇒ Object
- #render_custom ⇒ Object
- #render_template ⇒ Object
- #write(result, fname = nil) ⇒ Object private
Constructor Details
#initialize(notes, options = {}) {|_self| ... } ⇒ Format
Returns a new instance of Format.
43 44 45 46 47 48 49 50 51 |
# File 'lib/dnote/format.rb', line 43 def initialize(notes, ={}) @notes = notes @format = 'text' @subtype = 'label' @title = "Developer's Notes" @dryrun = false .each{ |k,v| __send__("#{k}=", v) if v } yield(self) if block_given? end |
Instance Attribute Details
#dryrun ⇒ Object
Returns the value of attribute dryrun.
40 41 42 |
# File 'lib/dnote/format.rb', line 40 def dryrun @dryrun end |
#format ⇒ Object
Returns the value of attribute format.
25 26 27 |
# File 'lib/dnote/format.rb', line 25 def format @format end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
22 23 24 |
# File 'lib/dnote/format.rb', line 22 def notes @notes end |
#output ⇒ Object
Returns the value of attribute output.
31 32 33 |
# File 'lib/dnote/format.rb', line 31 def output @output end |
#subtype ⇒ Object
Returns the value of attribute subtype.
28 29 30 |
# File 'lib/dnote/format.rb', line 28 def subtype @subtype end |
#template ⇒ Object
Returns the value of attribute template.
34 35 36 |
# File 'lib/dnote/format.rb', line 34 def template @template end |
#title ⇒ Object
Returns the value of attribute title.
37 38 39 |
# File 'lib/dnote/format.rb', line 37 def title @title end |
Instance Method Details
#debug? ⇒ Boolean (private)
130 131 132 |
# File 'lib/dnote/format.rb', line 130 def debug? $DEBUG end |
#dryrun? ⇒ Boolean (private)
125 126 127 |
# File 'lib/dnote/format.rb', line 125 def dryrun? @dryrun end |
#erb(file) ⇒ Object (private)
89 90 91 92 |
# File 'lib/dnote/format.rb', line 89 def erb(file) scope = ErbScope.new(:notes=>notes, :title=>title) scope.render(file) end |
#fu ⇒ Object (private)
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/dnote/format.rb', line 135 def fu @fu ||= ( if dryrun? and debug? FileUtils::DryRun elsif dryrun? FileUtils::Noop elsif debug? FileUtils::Verbose else FileUtils end ) end |
#publish(result, fname = nil) ⇒ Object (private)
95 96 97 98 99 100 101 102 |
# File 'lib/dnote/format.rb', line 95 def publish(result, fname=nil) if output write(result, fname) else puts(result) end $stderr << "(" + notes.counts.map{|l,n| "#{n} #{l}s"}.join(', ') + ")\n" end |
#render ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dnote/format.rb', line 54 def render if notes.empty? $stderr << "No #{notes.labels.join(', ')} notes.\n" else case format when 'custom' render_custom else render_template end end end |
#render_custom ⇒ Object
70 71 72 73 74 |
# File 'lib/dnote/format.rb', line 70 def render_custom #raise ArgumentError unless File.exist?(template) result = erb(template) publish(result) end |
#render_template ⇒ Object
79 80 81 82 83 84 |
# File 'lib/dnote/format.rb', line 79 def render_template template = File.join(File.dirname(__FILE__), 'templates', "#{format}.erb") raise "No such format - #{format}" unless File.exist?(template) result = erb(template) publish(result) end |
#write(result, fname = nil) ⇒ Object (private)
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/dnote/format.rb', line 105 def write(result, fname=nil) if output.to_s[-1,1] == '/' || File.directory?(output) fmt = format.split('/').first ext = EXTENSIONS[fmt] || fmt file = File.join(output, fname || "notes.#{ext}") else file = output end if dryrun? puts "mkdir: #{File.dirname(file)}" puts "write: #{file}" else dir = File.dirname(file) fu.mkdir(dir) unless File.exist?(dir) File.open(file, 'w'){ |f| f << result } end return file end |