Class: Quarry::Spec::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/quarry/spec/document.rb

Overview

Document

TODO: css and javascripts have fixed location

  • need to make more flexible.

Constant Summary collapse

DEFAULT_TITLE =
"Specifictions"
DEFAULT_CSS =
"../assets/styles/spec.css"
DEFAULT_FILE =
"doc/spec/index.html"
DEFAULT_PATH =
["spec/**/*"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Document

New Spec Document object.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/quarry/spec/document.rb', line 29

def initialize(options)
  options.each do |k,v|
    __send__("#{k}=", v)
  end

  @title  ||= DEFAULT_TITLE
  @css    ||= DEFAULT_CSS
  @output ||= DEFAULT_FILE
  @paths  ||= []

  @paths  = DEFAULT_PATH if @paths.empty?
end

Instance Attribute Details

#cssObject

Returns the value of attribute css.



20
21
22
# File 'lib/quarry/spec/document.rb', line 20

def css
  @css
end

#dryrunObject

Returns the value of attribute dryrun.



22
23
24
# File 'lib/quarry/spec/document.rb', line 22

def dryrun
  @dryrun
end

#outputObject

Ouput file.



26
27
28
# File 'lib/quarry/spec/document.rb', line 26

def output
  @output
end

#pathsObject

Returns the value of attribute paths.



21
22
23
# File 'lib/quarry/spec/document.rb', line 21

def paths
  @paths
end

#quietObject

Returns the value of attribute quiet.



23
24
25
# File 'lib/quarry/spec/document.rb', line 23

def quiet
  @quiet
end

#titleObject

Returns the value of attribute title.



19
20
21
# File 'lib/quarry/spec/document.rb', line 19

def title
  @title
end

Instance Method Details

#generateObject

Generate specification document.



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
# File 'lib/quarry/spec/document.rb', line 46

def generate
  files = []
  paths.each do |path|
    files.concat(Dir.glob(path).select{ |f| File.file?(f) })
  end

  text = ''
  files.each do |file|
    puts file unless quiet?
    case ext = File.extname(file)
    when '.rd', '.rdoc'
      require_rdoc
      markup = SM::SimpleMarkup.new
      formatter = SM::ToHtml.new
      text << markup.convert(File.read(file), formatter)
    when '.md', '.markdown'
      # TODO
    end
    text << "\n"
  end

  temp = Template.new(template, text, title, css)
  html = temp.parse_template

  save(html)
end

#quiet?Boolean

Supress output.

Returns:

  • (Boolean)


43
# File 'lib/quarry/spec/document.rb', line 43

def quiet? ; @quiet ; end

#save(text) ⇒ Object

Save specification document.



82
83
84
85
86
87
88
89
90
91
# File 'lib/quarry/spec/document.rb', line 82

def save(text)
  if dryrun
    puts "\nwrite #{output}"
  else
    FileUtils.mkdir_p(File.dirname(output))
    File.open(output, 'wb') do |f|
      f << text
    end
  end
end

#templateObject

Load specification HTML template.



74
75
76
77
78
79
# File 'lib/quarry/spec/document.rb', line 74

def template
  @template ||= (
    file = File.join(File.dirname(__FILE__), 'template.rhtml')
    File.read(file)
  )
end