Class: Workbook::Format

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

Overview

Format is an object used for maintinaing a cell's formatting. It can belong to many cells. It maintains a relation to the raw template's equivalent, to preserve attributes Workbook cannot modify/access.

Instance Attribute Summary collapse

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

#initialize(options = {}, name = nil) ⇒ Format

Initialize

Parameters:

  • options (Workbook::Format, Hash) (defaults to: {})

    (e.g. :background, :color



14
15
16
17
# File 'lib/workbook/format.rb', line 14

def initialize options={}, name=nil
  options.each {|k,v| self[k]=v}
  self.name = name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name



10
11
12
# File 'lib/workbook/format.rb', line 10

def name
  @name
end

#parentObject

Returns the value of attribute parent



10
11
12
# File 'lib/workbook/format.rb', line 10

def parent
  @parent
end

Instance Method Details

#all_namesArray<String>

returns an array of all format-names this style is inheriting from (and this style)

Returns:

  • (Array<String>)

    an array of Workbook::Formats



69
70
71
# File 'lib/workbook/format.rb', line 69

def all_names
  formats.collect{|a| a.name}
end

#flattenedWorkbook::Format

Applies the formatting options of self with its parents until no parent can be found

Returns:

  • (Workbook::Format)

    new Workbook::Format that is the result of merging current style with all its parent's styles.



75
76
77
78
79
# File 'lib/workbook/format.rb', line 75

def flattened
  ff=Workbook::Format.new()
  formats.each{|a| ff.merge!(a) }
  return ff
end

#formatsArray<Workbook::Format>

returns an array of all formats this style is inheriting from (including itself)

Returns:



56
57
58
59
60
61
62
63
64
65
# File 'lib/workbook/format.rb', line 56

def formats
  formats=[]
  f = self
  formats << f
  while f.parent
    formats << f.parent
    f = f.parent
  end
  formats.reverse
end

#has_background_color?(color = :any) ⇒ Boolean

Does the current format feature a background color? (not black or white or transparant).

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/workbook/format.rb', line 20

def has_background_color? color=:any
  if flattened[:background_color]
    return (flattened[:background_color].downcase==color.to_s.downcase or (!(flattened[:background_color]==nil or (flattened[:background_color].is_a? String and (flattened[:background_color].downcase=='#ffffff' or flattened[:background_color]=='#000000'))) and color==:any))
  else
    return false
  end
end

#merge(other_format) ⇒ Workbook::Format

Combines the formatting options of one with another, removes as a consequence the reference to the raw object's equivalent.

Parameters:

Returns:



41
42
43
44
# File 'lib/workbook/format.rb', line 41

def merge(other_format)
  self.remove_all_raws!
  self.merge_hash(other_format)
end

#merge!(other_format) ⇒ Workbook::Format

Applies the formatting options of self with another, removes as a consequence the reference to the raw object's equivalent.

Parameters:

Returns:



49
50
51
52
# File 'lib/workbook/format.rb', line 49

def merge!(other_format)
  self.remove_all_raws!
  self.merge_hash!(other_format)
end

#merge_hashObject



8
# File 'lib/workbook/format.rb', line 8

alias_method :merge_hash, :merge

#merge_hash!Object



9
# File 'lib/workbook/format.rb', line 9

alias_method :merge_hash!, :merge!

#to_cssObject

Returns a string that can be used as inline cell styling (e.g. `<td style=“<%=cell.format.to_css%>”><%=cell%></td>`)

Returns:

  • String very basic CSS styling string



30
31
32
33
34
35
36
# File 'lib/workbook/format.rb', line 30

def to_css
  css_parts = []
  background = [flattened[:background_color].to_s,flattened[:background].to_s].join(" ").strip
  css_parts.push("background: #{background}") if background and background != ""
  css_parts.push("color: #{flattened[:color].to_s}") if flattened[:color]
  css_parts.join("; ")
end