Class: Workbook::Template

Inherits:
Object
  • Object
show all
Includes:
Modules::RawObjectsStorage
Defined in:
lib/workbook/template.rb

Overview

Workbook::Template is a container for different Workbook::Format’s and the storage of raw template data that isn’t really supported by Workbook, but should survive a typical read/write cyclus.

Instance Method Summary collapse

Methods included from Modules::RawObjectsStorage

#add_raw, #available_raws, #has_raw_for?, #raws, #remove_all_raws!, #return_raw_for

Constructor Details

#initializeTemplate

Initialize Workbook::Template



13
14
15
16
# File 'lib/workbook/template.rb', line 13

def initialize
  @formats = {}
  @has_header = true
end

Instance Method Details

#add_format(format) ⇒ Object

Add a Workbook::Format to the template

Parameters:



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/workbook/template.rb', line 25

def add_format format
  if format.is_a? Workbook::Format
    if format.name
      @formats[format.name]=format
    else
      @formats[@formats.keys.count]=format
    end
  else
    raise ArgumentError, "format should be a Workboot::Format"
  end
end

#create_or_find_format_by(name, variant = :default) ⇒ Workbook::Format

Create or find a format by name

Parameters:

  • name (String)

    of the format (e.g. whatever you want, in diff names such as ‘destroyed’, ‘updated’ and ‘created’ are being used)

  • variant (Symbol) (defaults to: :default)

    can also be a strftime formatting string (e.g. “%Y-%m-%d”)

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/workbook/template.rb', line 47

def create_or_find_format_by name, variant=:default
  fs = @formats[name]
  fs = @formats[name] = {} if fs.nil?
  f = fs[variant]
  if f.nil?
    f = Workbook::Format.new
    if variant != :default and fs[:default]
      f = fs[:default].clone
    end
    @formats[name][variant] = f
  end
  return @formats[name][variant]
end

#formatsHash

Return the list of associated formats

Returns:

  • (Hash)

    A keyed-hash of named formats



39
40
41
# File 'lib/workbook/template.rb', line 39

def formats
  @formats
end

#has_header?Boolean

Whether the template has a predefined header (headers are used )

Returns:

  • (Boolean)


19
20
21
# File 'lib/workbook/template.rb', line 19

def has_header?
  @has_header
end

#set_default_formats!Object



61
62
63
64
# File 'lib/workbook/template.rb', line 61

def set_default_formats!
  header_fmt = create_or_find_format_by :header
  header_fmt[:font_weight] = 'bold'
end