Class: Workbook::Cell

Inherits:
Object
  • Object
show all
Includes:
Modules::TypeParser
Included in:
Types::Date, Types::FalseClass, Types::NilClass, Types::Numeric, Types::String, Types::Time, Types::TrueClass
Defined in:
lib/workbook/cell.rb

Constant Summary

VALID_TYPES =

Note that these types are sorted by 'importance'

[Numeric,String,Time,Date,TrueClass,FalseClass,NilClass,Workbook::NilValue,Symbol]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Modules::TypeParser

#clean!, #parse, #parse!, #string_boolean_converter, #string_cleaner, #string_integer_converter, #string_nil_converter, #string_optimistic_date_converter, #string_parsers, #string_parsers=, #string_parsers_as_procs, #strip_win_chars

Constructor Details

#initialize(value = nil, options = {}) ⇒ Cell



26
27
28
29
30
# File 'lib/workbook/cell.rb', line 26

def initialize value=nil, options={}
  self.format = options[:format]
  self.value = value
  @to_sym = nil
end

Instance Attribute Details

#formulaObject

Returns the value of attribute formula



9
10
11
# File 'lib/workbook/cell.rb', line 9

def formula
  @formula
end

Instance Method Details

#<=>(other) ⇒ Object

Compare



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/workbook/cell.rb', line 155

def <=> other
  rv = nil
  begin
    rv = self.value <=> other.value
  rescue NoMethodError
    rv = compare_on_class other
  end
  if rv == nil
    rv = compare_on_class other
  end
  return rv

end

#==(other) ⇒ Boolean

Tests for equality based on its value (formatting is irrelevant)



75
76
77
78
79
80
81
# File 'lib/workbook/cell.rb', line 75

def ==(other)
  if other.is_a? Cell
    other.value == self.value
  else
    other == self.value
  end
end

#colspanObject



211
212
213
# File 'lib/workbook/cell.rb', line 211

def colspan
  @colspan.to_i if defined?(@colspan) and @colspan.to_i > 1
end

#colspan=(c) ⇒ Object



204
205
206
# File 'lib/workbook/cell.rb', line 204

def colspan= c
  @colspan = c
end

#compare_on_class(other) ⇒ Object

Compare on class level



172
173
174
175
176
177
178
# File 'lib/workbook/cell.rb', line 172

def compare_on_class other
  other_value = nil
  other_value = other.value if other
  self_value = importance_of_class self.value
  other_value = importance_of_class other_value
  self_value <=> other_value
end

#formatObject

Returns current format



67
68
69
# File 'lib/workbook/cell.rb', line 67

def format
  @format ||= Workbook::Format.new
end

#format=(f) ⇒ Object

Change the current format



54
55
56
57
58
59
60
61
62
# File 'lib/workbook/cell.rb', line 54

def format= f
  if f.is_a? Workbook::Format
    @format = f
  elsif f.is_a? Hash
    @format = Workbook::Format.new(f)
  elsif f.class == NilClass
    @format = Workbook::Format.new
  end
end

#importance_of_class(value) ⇒ Object

Returns the importance of a value's class



183
184
185
186
187
188
# File 'lib/workbook/cell.rb', line 183

def importance_of_class value
  VALID_TYPES.each_with_index do |c,i|
    return i if value.is_a? c
  end
  return nil
end

#inspectObject



190
191
192
# File 'lib/workbook/cell.rb', line 190

def inspect
  "<Workbook::Cell @value=#{value}>"
end

#nil?Boolean

returns true when the value of the cell is nil.



85
86
87
# File 'lib/workbook/cell.rb', line 85

def nil?
  return value.nil?
end

#rowspanObject



214
215
216
# File 'lib/workbook/cell.rb', line 214

def rowspan
  @rowspan.to_i if defined?(@rowspan) and @rowspan.to_i > 1
end

#rowspan=(r) ⇒ Object



207
208
209
# File 'lib/workbook/cell.rb', line 207

def rowspan= r
  @rowspan = r
end

#to_sString

convert value to string, and in case of a Date or Time value, apply formatting



196
197
198
199
200
201
202
# File 'lib/workbook/cell.rb', line 196

def to_s
  if (value.is_a? Date or value.is_a? Time) and format[:number_format]
    value.strftime(format[:number_format])
  else
    value.to_s
  end
end

#to_symSymbol

returns a symbol representation of the cell's value

Examples:


<Workbook::Cell value="yet another value">.to_sym # returns :yet_another_value


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/workbook/cell.rb', line 94

def to_sym
  return @to_sym if @to_sym
  v = nil
  if value
    ends_with_exclamationmark = (value[-1] == '!')
    ends_with_questionmark = (value[-1] == '?')
    v = value.to_s.downcase

    replacements = {
      [/[\(\)\.\?\,\!\=\$\:]/,] => '',
      [/\&/] => 'amp',
      [/\+/] => '_plus_',
      [/\s/,'/_','/',"\\"] => '_',
      ['–_','-_','+_','-'] => '',
      ['__']=>'_',
      ['>']=>'gt',
      ['<']=>'lt',
      ['á','à','â','ä','ã','å'] => 'a',
      ['Ã','Ä','Â','À','�?','Å'] => 'A',
      ['é','è','ê','ë'] => 'e',
      ['Ë','É','È','Ê'] => 'E',
      ['í','ì','î','ï'] => 'i',
      ['�?','Î','Ì','�?'] => 'I',
      ['ó','ò','ô','ö','õ'] => 'o',
      ['Õ','Ö','Ô','Ò','Ó'] => 'O',
      ['ú','ù','û','ü'] => 'u',
      ['Ú','Û','Ù','Ü'] => 'U',
      ['ç'] => 'c',
      ['Ç'] => 'C',
      ['š', 'ś'] => 's',
      ['Š', 'Ś'] => 'S',
      ['ž','ź'] => 'z',
      ['Ž','Ź'] => 'Z',
      ['ñ'] => 'n',
      ['Ñ'] => 'N',
      ['#'] => 'hash',
      ['*'] => 'asterisk'
    }
    replacements.each do |ac,rep|
      ac.each do |s|
        v = v.gsub(s, rep)
      end
    end
    if RUBY_VERSION < '1.9'
      v = v.gsub(/[^\x00-\x7F]/n,'')
    else
      # See String#encode
      encoding_options = {:invalid => :replace, :undef => :replace, :replace => ''}
      v = v.encode(Encoding.find('ASCII'), encoding_options)
    end
    v = "#{v}!" if ends_with_exclamationmark
    v = "#{v}?" if ends_with_questionmark
    v = v.downcase.to_sym
  end
  @to_sym = v
  return @to_sym
end

#valid_value?(value) ⇒ Boolean

Evaluates a value for class-validity



18
19
20
21
22
# File 'lib/workbook/cell.rb', line 18

def valid_value? value
  valid_type = false
  VALID_TYPES.each {|t| return true if value.is_a? t}
  valid_type
end

#valueNumeric, ...

Returns the current value



47
48
49
# File 'lib/workbook/cell.rb', line 47

def value
  @value
end

#value=(value) ⇒ Object

Change the current value



35
36
37
38
39
40
41
42
# File 'lib/workbook/cell.rb', line 35

def value= value
  if valid_value? value
    @value = value
    @to_sym = nil
  else
    raise ArgumentError, "value should be of a primitive type, e.g. a string, or an integer, not a #{value.class} (is_a? [TrueClass,FalseClass,Date,Time,Numeric,String, NilClass, Symbol])"
  end
end