Class: Osheet::Workbook

Inherits:
Object
  • Object
show all
Includes:
WorkbookApi
Defined in:
lib/osheet/workbook.rb

Defined Under Namespace

Classes: ElementStack

Instance Method Summary collapse

Methods included from WorkbookApi

#border, #cell, #cells, #column, #columns, #format, #meta, #partial, #row, #rows, #style, #style_class, #template, #title, #worksheet, #worksheets

Constructor Details

#initialize(writer = nil, data = {}, &build) ⇒ Workbook

Returns a new instance of Workbook.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/osheet/workbook.rb', line 18

def initialize(writer=nil, data={}, &build)
  # apply :data options to workbook scope
  data ||= {}
  if (data.keys.map(&:to_s) & self.public_methods.map(&:to_s)).size > 0
    raise ArgumentError, "data conflicts with workbook public methods."
  end
  metaclass = class << self; self; end
  data.each {|key, value| metaclass.class_eval { define_method(key){value} }}

  # setup the writer, element stack, and workbook_element
  writer.bind(self) if writer
  @_osheet_writer = writer
  @_osheet_element_stack = Workbook::ElementStack.new
  @_osheet_workbook_element = WorkbookElement.new

  # push the workbook element onto the element stack
  element_stack.push(workbook)

  # run any instance workbook build given
  instance_eval(&build) if build
end

Instance Method Details

#add(partial_name, *args) ⇒ Object

add partials to dynamically add markup to your workbook note: you can also define element templates to add element specific markup to your workbook



76
77
78
79
80
# File 'lib/osheet/workbook.rb', line 76

def add(partial_name, *args)
  workbook.partials.get(partial_name).tap do |p|
    instance_exec(*args, &p) if p
  end
end

#element_stackObject



44
45
46
# File 'lib/osheet/workbook.rb', line 44

def element_stack
  @_osheet_element_stack
end

#to_str(*args) ⇒ Object Also known as: inspect

overriding to make less noisy



83
84
85
86
87
# File 'lib/osheet/workbook.rb', line 83

def to_str(*args)
  "#<Osheet::Workbook:#{self.object_id} @title=#{workbook.title.inspect}, " +
  "worksheet_count=#{workbook.worksheets.size.inspect}, " +
  "style_count=#{workbook.styles.size.inspect}>"
end

#use(mixin) ⇒ Object

use a mixin to define its markup handlers (templates, partials, and styles) in your workbook scope all blocks in mixins will be instance eval’d in the workbook scope and should be written as such



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/osheet/workbook.rb', line 57

def use(mixin)
  # templates and partials are just blocks themselves so they just need to
  # be added to the workbook element
  # they will be instance eval'd when they get used
  (mixin.templates || []).each { |mt| template(*mt.args, &mt.build) }
  (mixin.partials  || []).each { |mp| partial(*mp.args, &mp.build)  }

  # styles not only need to be added to the workbook element, but
  # any build passed to the style needs to be instance eval'd
  (mixin.styles || []).each do |ms|
    StyleBuild.new(self, *ms.args, &ms.build).add do |build|
      instance_eval(&build)
    end
  end
end

#workbook_elementObject Also known as: workbook



48
49
50
# File 'lib/osheet/workbook.rb', line 48

def workbook_element
  @_osheet_workbook_element
end

#writerObject



40
41
42
# File 'lib/osheet/workbook.rb', line 40

def writer
  @_osheet_writer
end