Class: Woa::Energon::Document

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

Overview

This class is the fronthead class for Energon. It’s the main class and it executes the hight level stuffs. It deals with text files. Subclasses deal with other kind of templates:

  • ExcelDocument for Excel templates

  • WordDocument for Word templates

  • OdDocument for OpenDocument (OpenOffice write and spreadsheet)

:include: rdoc-header

Examples

require 'energon'
include Woa::Energon
document = Document.new("@author@, @date@, @country@")
document.add_values({:author => "Woa! Kft", :date => Time.now})
document.add_value(:country, "Hungary")
puts document.write #"Woa! Kft, Wed Nov 29 11:31:52 CET 2006, Hungary"

Direct Known Subclasses

ExcelDocument, OdDocument, WordDocument

Constant Summary collapse

DefaultDelimiter =
'@'
DefaultNewLine =
"\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, delimiter = DefaultDelimiter) ⇒ Document

Just pass the template and specify if the delimiter is not the one by default (“@”)

Raises:



35
36
37
38
39
40
41
42
43
# File 'lib/energon/document.rb', line 35

def initialize(template, delimiter=DefaultDelimiter)
  raise EnergonError if delimiter.to_s.empty?
  @delimiter = delimiter.to_s
  @new_line = DefaultNewLine
  @datas = {}
  @template = template
  @placeholders = []
  extract_placeholders
end

Instance Attribute Details

#delimiterObject

Returns the value of attribute delimiter.



31
32
33
# File 'lib/energon/document.rb', line 31

def delimiter
  @delimiter
end

#new_lineObject

Returns the value of attribute new_line.



32
33
34
# File 'lib/energon/document.rb', line 32

def new_line
  @new_line
end

Instance Method Details

#add_value(key, value) ⇒ Object

Add a data

  • key is the name of the data

  • value is the data



48
49
50
# File 'lib/energon/document.rb', line 48

def add_value(key, value)
  @datas[key] = value
end

#add_values(hash) ⇒ Object

Add several datas from a Hash.

  • the key is the name of the data

  • the value is the data



55
56
57
# File 'lib/energon/document.rb', line 55

def add_values(hash)
  hash.each {|key, value| self.add_value(key, value)}
end

#valid?Boolean

Is the template valid ? It’s valid when

  • there is at least one data

  • there is at least one placeholder in the template

  • there is at least one data for each placeholder

It will return true or false.

Returns:

  • (Boolean)

Raises:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/energon/document.rb', line 65

def valid?
  raise NoPlaceholderFound if @placeholders.empty?
  @placeholders.each do |placeholder|
    begin
      Parser.merge(placeholder, @datas)
    rescue NoDataFound
      return(false)
    end
  end
  true
end

#write(output = nil) ⇒ Object Also known as: close

Generate the final document. Raise an exception if an error occurs

It’s possible to specify the ouput to write directly in a specific stream.

Raises:



81
82
83
84
85
86
87
88
89
90
# File 'lib/energon/document.rb', line 81

def write(output=nil)
  raise NoPlaceholderFound if @placeholders.empty?
  @placeholders.each do |placeholder|
    data = Parser.merge(placeholder, @datas)
    data = data.join(@new_line) if data.is_a?(Array)
    @template.gsub!("#{@delimiter}#{placeholder}#{@delimiter}", data.to_s)
  end
  return(@template) if output.nil?
  output.puts @template
end