Class: DNote::Format

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(notes, options = {}) {|_self| ... } ⇒ Format

Returns a new instance of Format.

Yields:

  • (_self)

Yield Parameters:

  • _self (DNote::Format)

    the object that the method was called on



43
44
45
46
47
48
49
50
51
# File 'lib/dnote/format.rb', line 43

def initialize(notes, options={})
  @notes   = notes
  @format  = 'text'
  @subtype = 'label'
  @title   = "Developer's Notes"
  @dryrun  = false
  options.each{ |k,v| __send__("#{k}=", v) if v }
  yield(self) if block_given?
end

Instance Attribute Details

#dryrunObject

Returns the value of attribute dryrun.



40
41
42
# File 'lib/dnote/format.rb', line 40

def dryrun
  @dryrun
end

#formatObject

Returns the value of attribute format.



25
26
27
# File 'lib/dnote/format.rb', line 25

def format
  @format
end

#notesObject (readonly)

Returns the value of attribute notes.



22
23
24
# File 'lib/dnote/format.rb', line 22

def notes
  @notes
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

#subtypeObject

Returns the value of attribute subtype.



28
29
30
# File 'lib/dnote/format.rb', line 28

def subtype
  @subtype
end

#templateObject

Returns the value of attribute template.



34
35
36
# File 'lib/dnote/format.rb', line 34

def template
  @template
end

#titleObject

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)

Returns:

  • (Boolean)


130
131
132
# File 'lib/dnote/format.rb', line 130

def debug?
  $DEBUG
end

#dryrun?Boolean (private)

Returns:

  • (Boolean)


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

#fuObject (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

#renderObject



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_customObject



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_templateObject



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