Class: ToSpreadsheet::Context

Inherits:
Object
  • Object
show all
Includes:
Pairing
Defined in:
lib/to_spreadsheet/context.rb,
lib/to_spreadsheet/context/pairing.rb

Overview

This is the DSL context

Defined Under Namespace

Modules: Pairing

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Pairing

#assoc!, #clear_assoc!, #to_xls_entity, #to_xml_node, #xml_node_and_xls_entity

Constructor Details

#initialize(wb_options = nil) ⇒ Context

Returns a new instance of Context.



38
39
40
41
# File 'lib/to_spreadsheet/context.rb', line 38

def initialize(wb_options = nil)
  @rules = []
  workbook wb_options if wb_options
end

Instance Attribute Details

#rulesObject

Returns the value of attribute rules.



14
15
16
# File 'lib/to_spreadsheet/context.rb', line 14

def rules
  @rules
end

Class Method Details

.currentObject



21
22
23
# File 'lib/to_spreadsheet/context.rb', line 21

def current
  Thread.current[:_to_spreadsheet_ctx]
end

.current=(ctx) ⇒ Object



25
26
27
# File 'lib/to_spreadsheet/context.rb', line 25

def current=(ctx)
  Thread.current[:_to_spreadsheet_ctx] = ctx
end

.globalObject



17
18
19
# File 'lib/to_spreadsheet/context.rb', line 17

def global
  @global ||= new
end

.with_context(ctx, &block) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/to_spreadsheet/context.rb', line 29

def with_context(ctx, &block)
  old = current
  self.current = ctx
  r = block.call(ctx)
  self.current = old
  r
end

Instance Method Details

#add_rule(rule_type, selector_type, selector_value, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/to_spreadsheet/context.rb', line 96

def add_rule(rule_type, selector_type, selector_value, options = {})
  rule = ToSpreadsheet::Rule.make(rule_type, selector_type, selector_value, options)
  if @rule_container
    @rule_container.children << rule
  else
    @rules << rule
  end
  rule
end

#default(selector, value) ⇒ Object

default ‘td.c’, 5



91
92
93
94
# File 'lib/to_spreadsheet/context.rb', line 91

def default(selector, value)
  selector = selector_query(selector)
  add_rule :default_value, *selector, value
end

#format(selector = nil, options, &block) ⇒ Object

format ‘td.b’, b: true # bold format column: 0, width: 50 format ‘A1:C30’, b: true Accepted properties: rubydoc.info/github/randym/axlsx/Axlsx/Cell column format also accepts Axlsx columnInfo settings



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/to_spreadsheet/context.rb', line 71

def format(selector = nil, options, &block)
  if !selector && options.is_a?(String)
    selector = options
    options = nil
  else
    options = options.dup
  end
  options ||= block
  selector = selector_query(selector, options)
  add_rule :format, *selector, options
end

#format_xls(selector = nil, theme = nil, &block) ⇒ Object

Examples:

format_xls 'table.zebra' do
  format 'td', lambda { |cell| {b: true} if cell.row.even? }
end
format_xls ToSpreadsheet.theme(:a_theme)
format_xls 'table.zebra', ToSpreadsheet.theme(:zebra)


49
50
51
52
53
54
# File 'lib/to_spreadsheet/context.rb', line 49

def format_xls(selector = nil, theme = nil, &block)
  selector, theme = nil, selector if selector.is_a?(Proc) && !theme
  process_dsl(selector, &theme) if theme
  process_dsl(selector, &block) if block
  self
end

#merge(other_context) ⇒ Object

A new context



107
108
109
110
111
# File 'lib/to_spreadsheet/context.rb', line 107

def merge(other_context)
  ctx       = Context.new()
  ctx.rules = rules + other_context.rules
  ctx
end

#process_dsl(selector, &block) ⇒ Object



56
57
58
59
60
# File 'lib/to_spreadsheet/context.rb', line 56

def process_dsl(selector, &block)
  @rule_container = add_rule :container, *selector_query(selector)
  instance_eval(&block)
  @rule_container = nil
end

#sheet(selector = nil, options) ⇒ Object

sheet ‘table.landscape’, page_setup: { orientation: landscape }



84
85
86
87
88
# File 'lib/to_spreadsheet/context.rb', line 84

def sheet(selector = nil, options)
  options  = options.dup
  selector = selector_query(selector, options)
  add_rule :sheet, *selector, options
end

#workbook(selector = nil, value) ⇒ Object



62
63
64
# File 'lib/to_spreadsheet/context.rb', line 62

def workbook(selector = nil, value)
  add_rule :workbook, *selector_query(selector), value
end