Class: Osheet::WorkbookApi::TemplatedElement

Inherits:
Object
  • Object
show all
Defined in:
lib/osheet/workbook_api.rb

Overview

element handling API

Constant Summary collapse

ELEMENT_CLASS =

this class is used to create elements that can be templated. Arguments are handled differently if building an element from a template vs. building from a build block. After the element is built, it is added to the current stack element and either the build or writer is called.

{
  :worksheet => Osheet::Worksheet,
  :column    => Osheet::Column,
  :row       => Osheet::Row,
  :cell      => Osheet::Cell
}

Instance Method Summary collapse

Constructor Details

#initialize(scope, name, *args, &build) ⇒ TemplatedElement

Returns a new instance of TemplatedElement.



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/osheet/workbook_api.rb', line 83

def initialize(scope, name, *args, &build)
  @scope = scope
  @workbook = @scope.workbook
  @name = name
  @args = args
  if (@template = @workbook.templates.get(@name, @args.first))
    @element = ELEMENT_CLASS[@name].new
    @build   = Proc.new { @scope.instance_exec(*@args[1..-1], &@template) }
  else
    @element = ELEMENT_CLASS[@name].new(*@args)
    @build   = build || Proc.new {}
  end
end

Instance Method Details

#addObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/osheet/workbook_api.rb', line 97

def add
  @scope.element_stack.current.send(@name, @element)
  @scope.element_stack.using(@element) do
    if @scope.writer
      @scope.writer.send(@name, @element, &@build)
    else
      @build.call
    end
  end
end