Class: Workbook::Format
- Inherits:
-
Hash
- Object
- Hash
- Workbook::Format
- 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. The keys in the Hash are intended to closely mimick the CSS-style options:
{
background_color: '#ff000',
color: '#ffff00',
font_weight: :bold,
text_decoration: :underline,
}
Note that as we speak, not all exporters support all properties properly. Consider it WIP.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
-
#all_names ⇒ Array<String>
returns an array of all format-names this style is inheriting from (and this style).
-
#derived_type ⇒ Object
Formatting is sometimes the only way to detect the cells’ type.
-
#flattened ⇒ Workbook::Format
Applies the formatting options of self with its parents until no parent can be found.
-
#formats ⇒ Array<Workbook::Format>
returns an array of all formats this style is inheriting from (including itself).
-
#has_background_color?(color = :any) ⇒ Boolean
Does the current format feature a background color? (not black or white or transparant).
-
#initialize(options = {}, name = nil) ⇒ String
constructor
Initializes Workbook::Format with a hash.
-
#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.
-
#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.
- #merge_hash ⇒ Object
- #merge_hash! ⇒ Object
-
#to_css ⇒ Object
Returns a string that can be used as inline cell styling (e.g. ‘<td style=“<%=cell.format.to_css%>”><%=cell%></td>`).
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) ⇒ String
Initializes Workbook::Format with a hash. The keys in the Hash are intended to closely mimick the CSS-style options (see above)
29 30 31 32 33 34 35 36 |
# File 'lib/workbook/format.rb', line 29 def initialize ={}, name=nil if .is_a? String name = else .each {|k,v| self[k]=v} end self.name = name end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
23 24 25 |
# File 'lib/workbook/format.rb', line 23 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
23 24 25 |
# File 'lib/workbook/format.rb', line 23 def parent @parent end |
Instance Method Details
#all_names ⇒ Array<String>
returns an array of all format-names this style is inheriting from (and this style)
92 93 94 |
# File 'lib/workbook/format.rb', line 92 def all_names formats.collect{|a| a.name} end |
#derived_type ⇒ Object
Formatting is sometimes the only way to detect the cells’ type.
105 106 107 108 109 110 111 112 113 |
# File 'lib/workbook/format.rb', line 105 def derived_type if self[:numberformat] if self[:numberformat].to_s.match("h") :time elsif self[:numberformat].to_s.match(/y/i) :date end end end |
#flattened ⇒ Workbook::Format
Applies the formatting options of self with its parents until no parent can be found
98 99 100 101 102 |
# File 'lib/workbook/format.rb', line 98 def flattened ff=Workbook::Format.new() formats.each{|a| ff.merge!(a) } return ff end |
#formats ⇒ Array<Workbook::Format>
returns an array of all formats this style is inheriting from (including itself)
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/workbook/format.rb', line 79 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).
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/workbook/format.rb', line 39 def has_background_color? color=:any bg_color = flattened[:background_color] ? flattened[:background_color].to_s.downcase : nil if color != :any and bg_color return bg_color == color.to_s.downcase elsif bg_color return !(flattened[:background_color].downcase=='#ffffff' or flattened[:background_color]=='#000000') 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.
64 65 66 67 |
# File 'lib/workbook/format.rb', line 64 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.
72 73 74 75 |
# File 'lib/workbook/format.rb', line 72 def merge!(other_format) self.remove_all_raws! self.merge_hash!(other_format) end |
#merge_hash ⇒ Object
21 |
# File 'lib/workbook/format.rb', line 21 alias_method :merge_hash, :merge |
#merge_hash! ⇒ Object
22 |
# File 'lib/workbook/format.rb', line 22 alias_method :merge_hash!, :merge! |
#to_css ⇒ Object
Returns a string that can be used as inline cell styling (e.g. ‘<td style=“<%=cell.format.to_css%>”><%=cell%></td>`)
53 54 55 56 57 58 59 |
# File 'lib/workbook/format.rb', line 53 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 |