Class: TableFu::Datum

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

Overview

A Datum is an individual cell in the TableFu::Row

Direct Known Subclasses

Header

Instance Attribute Summary

Instance Method Summary

Constructor Details

- (Datum) initialize(datum, col_name, row_num, spreadsheet)

Each piece of datum should know where it is by column and row number, along with the spreadsheet it’s apart of. There’s probably a better way to go about doing this. Subclass?



244
245
246
247
248
249
# File 'lib/table_fu.rb', line 244

def initialize(datum, col_name, row_num, spreadsheet)
  @datum = datum
  @column_name = col_name
  @row_num = row_num
  @spreadsheet = spreadsheet
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method)

This method missing looks for 4 matches

First Option We have a column option by that method name and it applies to this column Example -

  >> @data.column_name
  => 'Total'
  >> @datum.style

Finds col_opt[:style] = {‘Total’ => ‘text-align:left;’}

  => 'text-align:left;'

Second Option We have a column option by that method name, but no attribute

  >> @data.column_name
  => 'Total'
  >> @datum.style
  Finds col_opt[:style] = {'State' => 'text-align:left;'}
  => ''

Third Option The boolean

  >> @data.invisible?

And we’ve set it col_opts[:invisible] = [‘Total’]

  => true

Fourth Option The boolean that’s false

  >> @data.invisible?

And it’s not in the list col_opts[:invisible] = [‘State’]

  => false


337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/table_fu.rb', line 337

def method_missing(method)
  opts = indifferent_access @spreadsheet.col_opts
  if val = opts[method] && opts[method][column_name]
    val
  elsif val = opts[method] && !opts[method][column_name]
    ''
  elsif method.to_s =~ /\?$/ && col_opts = opts[method.to_s.chop.to_sym]
    col_opts.index(column_name) || false
  elsif method.to_s =~ /\?$/ && !opts[method.to_s.chop.to_sym]
    nil
  else
    super
  end
end

Instance Attribute Details

- (Object) column_name (readonly)

Returns the value of attribute column_name



239
240
241
# File 'lib/table_fu.rb', line 239

def column_name
  @column_name
end

- (Object) options (readonly)

Returns the value of attribute options



239
240
241
# File 'lib/table_fu.rb', line 239

def options
  @options
end

Instance Method Details

- (Object) macro_value

Returns the macro’d format if there is one

Returns: The macro value if it exists, otherwise nil



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/table_fu.rb', line 277

def macro_value
  # Grab the macro method first
  # Then get a array of the values in the columns listed as arguments
  # Splat the arguments to the macro method.
  # Example:
  #   @spreadsheet.col_opts[:formatting] = 
  #    {'Total Appropriation' => :currency,
  #     'AppendedColumn' => {'method' => 'append', 'arguments' => ['Projects','State']}}
  # 
  # in the above case we handle the AppendedColumn in this method
  if @row_num && @spreadsheet.formatting && @spreadsheet.formatting[@column_name].is_a?(Hash)
    method = @spreadsheet.formatting[@column_name]['method']
    arguments =  @spreadsheet.formatting[@column_name]['arguments'].inject([]){|arr,arg| arr << @spreadsheet.rows[@row_num].column_for(arg); arr}
    TableFu::Formatting.send(method, *arguments)
  end
end

- (Object) to_s

Our standard formatter for the datum

Returns: the formatted value, macro value, or a empty string

First we test to see if this Datum has a macro attached to it. If so we let the macro method do it’s magic

Then we test for a simple formatter method.

And finally we return a empty string object or the value.



263
264
265
266
267
268
269
270
271
# File 'lib/table_fu.rb', line 263

def to_s
  if macro_value
    macro_value
  elsif @spreadsheet.formatting && format_method = @spreadsheet.formatting[column_name]
    TableFu::Formatting.send(format_method, @datum) || ''
  else
    @datum || ''
  end
end

- (Object) value

Returns the raw value of a datum

Returns: raw value of the datum, could be nil



298
299
300
301
302
303
304
# File 'lib/table_fu.rb', line 298

def value
  if @datum =~ /[0-9]+/
    @datum.to_i
  else
    @datum
  end
end