Class: Workbook::Cell

Inherits:
Object
  • Object
show all
Includes:
Modules::TypeParser
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]

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

- (Cell) initialize(value = nil, options = {})

Returns a new instance of Cell



22
23
24
25
26
27
28
29
# File 'lib/workbook/cell.rb', line 22

def initialize value=nil, options={}
  if valid_value? value
    format = options[:format] 
    @value = value
  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])"
  end
end

Instance Attribute Details

- (Object) format

Returns the value of attribute format



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

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

- (Object) formula

Returns the value of attribute formula



11
12
13
# File 'lib/workbook/cell.rb', line 11

def formula
  @formula
end

- (Object) value

Returns the value of attribute value



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

def value
  @value
end

Instance Method Details

- (Object) <=>(other)



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/workbook/cell.rb', line 101

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

end

- (Object) ==(other)



45
46
47
48
49
50
51
# File 'lib/workbook/cell.rb', line 45

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

- (Object) compare_on_class(other)



115
116
117
118
119
120
121
# File 'lib/workbook/cell.rb', line 115

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

- (Object) importance_of_class(value)



123
124
125
126
127
128
# File 'lib/workbook/cell.rb', line 123

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

- (Object) inspect



130
131
132
# File 'lib/workbook/cell.rb', line 130

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

- (Boolean) nil?

Returns:

  • (Boolean)


53
54
55
# File 'lib/workbook/cell.rb', line 53

def nil?
  return value.nil?
end

- (Object) to_s



134
135
136
137
138
139
140
# File 'lib/workbook/cell.rb', line 134

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

- (Object) to_sym



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/workbook/cell.rb', line 57

def to_sym
  #mb_chars.normalize(:kd).
  v = nil
  if value
    v = value.to_s.downcase
    v = v.gsub(' (j/?/leeg)','').gsub(/dd-mm-(.*)/,'').gsub(/\ja\/nee/,'').gsub(/\(\)/,'').gsub(/[\(\)]+/, '')
    v = v.strip.gsub(/(\.|\?|,|\=)/,'').
        gsub('$','').
        gsub(/\&/,'en').
        gsub(/\+/,'_plus_').
        gsub(/\s/, "_").
        gsub('–_','').
        gsub('-_','').
        gsub('+_','').
        gsub('/_','_').
        gsub('/','_').
        gsub('__','_').
        gsub('-','')
        
    accents = { 
      ['á','à','â','ä','ã'] => 'a',
      ['Ã','Ä','Â','À','�?'] => 'A',
      ['é','è','ê','ë'] => 'e',
      ['Ë','É','È','Ê'] => 'E',
      ['í','ì','î','ï'] => 'i',
      ['�?','Î','Ì','�?'] => 'I',
      ['ó','ò','ô','ö','õ'] => 'o',
      ['Õ','Ö','Ô','Ò','Ó'] => 'O',
      ['ú','ù','û','ü'] => 'u',
      ['Ú','Û','Ù','Ü'] => 'U',
      ['ç'] => 'c', ['Ç'] => 'C',
      ['ñ'] => 'n', ['Ñ'] => 'N'
    }
    accents.each do |ac,rep|
      ac.each do |s|
        v = v.gsub(s, rep)
      end
    end
    
    v = v.gsub(/[^\x00-\x7F]/n,'').downcase.to_sym
  end
  v
end

- (Boolean) valid_value?(value)

Returns:

  • (Boolean)


16
17
18
19
20
# File 'lib/workbook/cell.rb', line 16

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