Class: Border

Inherits:
Object
  • Object
show all
Defined in:
lib/spreadsheet_builder/border.rb

Constant Summary collapse

SPREADSHEET_VALUES =
[
  :none,
  :thin, 
  :medium, 
  :thick, 
  :double, 
  :hair, 
  :dashed, 
  :dotted, 
  :thin_dash_dotted,
  :thin_dash_dot_dotted,
  :medium_dashed, 
  :medium_dash_dotted, 
  :medium_dash_dot_dotted, 
  :slanted_medium_dash_dotted 
]

Instance Method Summary collapse

Constructor Details

#initialize(key, val, dir = nil) ⇒ Border

Returns a new instance of Border.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/spreadsheet_builder/border.rb', line 3

def initialize(key, val, dir = nil)
  @dir     = dir && dir.to_sym
  @vals    = val.scan(/[^\s]+/)
  @keys    = key.scan(/[^-]+/)
  @border  = self
  dir_hash = { top: nil, bottom: nil, left: nil, right: nil }
  @css     = { 
    color: dir_hash.dup,
    style: dir_hash.dup,
    width: dir_hash.dup
  }

  case @keys[1]
  when Proc.new { |type| @css.keys.include?(type.to_sym) if type }
    assign_vals_to(@keys[1])
  when nil # "border" 
    width, style, color = @vals
    w, s, c = @css[:width], @css[:style], @css[:color]

    w[:top] = w[:bottom] = w[:left] = w[:right] = width
    s[:top] = s[:bottom] = s[:left] = s[:right] = style
    c[:top] = c[:bottom] = c[:left] = c[:right] = color
  else     # "border-left"
    dir     = @keys.delete_at(1)
    @border = Border.new(@keys.join('-'), @vals.join(' '), dir)
  end
end

Instance Method Details

#assign_vals_to(type) ⇒ Object

border: 1px solid black; border-width: 1px 1px 1px 1px; border-width: 1px; // all border-width: 1px 2px; // top and bottom 1px, left and right 2px border-width: 1px 2px 3px; // top 1px, left and right 2px, bottom 3px border-#direction-width: 1px;



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spreadsheet_builder/border.rb', line 38

def assign_vals_to(type)
  t = @css[type.to_sym]
  case @vals.length
  when 1
    t[:top] = t[:bottom] = t[:left] = t[:right] = @vals[0]
  when 2
    t[:top]  = t[:bottom] = @vals[0]
    t[:left] = t[:right]  = @vals[1]
  when 3
    t[:top]              = @vals[0]
    t[:left] = t[:right] = @vals[1]
    t[:bottom]           = @vals[2]
  when 4
    t[:top], t[:bottom], t[:left], t[:right] = @vals
  end
end

#formatObject



55
56
57
# File 'lib/spreadsheet_builder/border.rb', line 55

def format
  @border._format
end