Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

using hash.key instead of hash.index

  • Loading branch information...
commit d593879fed3e0062d27bdaf5c6e2350662efe18d 1 parent 3353958
@despo authored
Showing with 22 additions and 22 deletions.
  1. +22 −22 lib/surpass/style.rb
View
44 lib/surpass/style.rb
@@ -5,7 +5,7 @@ class StyleFormat
attr_accessor :borders
attr_accessor :pattern
attr_accessor :protection
-
+
def initialize(hash = {})
@number_format_string = hash[:number_format_string] || 'General'
@@ -15,7 +15,7 @@ def initialize(hash = {})
@pattern = Pattern.new(hash_select(hash, /^(fill|pattern)_/))
@protection = Protection.new
end
-
+
def hash_select(hash, pattern)
new_hash = {}
hash.keys.each do |k|
@@ -33,9 +33,9 @@ class StyleCollection
attr_accessor :styles
attr_accessor :default_style
attr_accessor :default_format
-
+
FIRST_USER_DEFINED_NUM_FORMAT_INDEX = 164
-
+
STANDARD_NUMBER_FORMATS = [
'General',
'0',
@@ -72,9 +72,9 @@ class StyleCollection
'[h]:mm:ss',
'mm:ss.0',
'##0.0E+0',
- '@'
+ '@'
]
-
+
def initialize
# Populate default font list.
@fonts = {}
@@ -82,21 +82,21 @@ def initialize
[0,1,2,3,5].each do |i|
@fonts[i] = Font.new
end
-
+
# Populate default number format list.
@number_formats = {}
STANDARD_NUMBER_FORMATS.each_with_index do |s, i|
index = (i <= 23) ? i : i + 14
@number_formats[index] = s
end
-
+
@styles = {}
@default_style = StyleFormat.new
-
+
# Store the 6 parameters of the default_style
@default_format = add_style(@default_style)[0]
end
-
+
### @export "autoformats"
def default_date_style
@default_date_style ||= StyleFormat.new(:number_format_string => 'dd-mmm-yyyy')
@@ -119,9 +119,9 @@ def add(style)
add_style(style)[1] # Return the index of the style just stored.
end
end
-
+
def number_format_index(number_format_string)
- index = @number_formats.index(number_format_string)
+ index = @number_formats.key(number_format_string)
if index.nil?
# TODO implement regex to check if valid string
index = FIRST_USER_DEFINED_NUM_FORMAT_INDEX + @number_formats.length - STANDARD_NUMBER_FORMATS.length
@@ -129,40 +129,40 @@ def number_format_index(number_format_string)
end
index
end
-
+
def font_index(font)
- index = @fonts.index(font)
+ index = @fonts.key(font)
if index.nil?
index = @fonts.length + 1
@fonts[index] = font
end
index
end
-
+
def format_index(format)
- index = @styles.index(format)
+ index = @styles.key(format)
if index.nil?
index = 0x10 + @styles.length
@styles[index] = format
end
index
end
-
+
private
# This is private, please use add(style) instead.
def add_style(style)
number_format_index = number_format_index(style.number_format_string)
font_index = font_index(style.font)
-
+
format = [font_index, number_format_index, style.alignment, style.borders, style.pattern, style.protection]
[format, format_index(format)]
end
-
+
public
def to_biff
fonts_biff + number_formats_biff + cell_styles_biff + StyleRecord.new.to_biff
end
-
+
# TODO use inject here?
def fonts_biff
result = ''
@@ -171,7 +171,7 @@ def fonts_biff
end
result
end
-
+
def number_formats_biff
result = ''
@number_formats.sort.each do |i, f|
@@ -180,7 +180,7 @@ def number_formats_biff
end
result
end
-
+
def cell_styles_biff
result = ''
0.upto(15) do |i|
Please sign in to comment.
Something went wrong with that request. Please try again.