Class: TableFu::Datum
- Inherits:
-
Object
- Object
- TableFu::Datum
- Defined in:
- lib/table_fu.rb
Overview
A Datum is an individual cell in the TableFu::Row
Direct Known Subclasses
Instance Attribute Summary collapse
-
#column_name ⇒ Object
readonly
Returns the value of attribute column_name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(datum, col_name, row, spreadsheet) ⇒ Datum
constructor
Each piece of datum should know where it is by column and row number, along with the spreadsheet it’s apart of.
-
#macro? ⇒ Boolean
Returns whether there is a macro format for this datum.
-
#macro_value ⇒ Object
Returns the macro’d format.
-
#method_missing(method) ⇒ Object
This method missing looks for 4 matches.
-
#to_s ⇒ Object
Our standard formatter for the datum.
-
#value ⇒ Object
Returns the raw value of a datum.
Constructor Details
#initialize(datum, col_name, row, spreadsheet) ⇒ Datum
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?
259 260 261 262 263 264 |
# File 'lib/table_fu.rb', line 259 def initialize(datum, col_name, row, spreadsheet) @datum = datum @column_name = col_name @row = row @spreadsheet = spreadsheet end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
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 = => ‘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 = [‘Total’]
=> true
Fourth Option The boolean that’s false
>> @data.invisible?
And it’s not in the list col_opts = [‘State’]
=> false
364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/table_fu.rb', line 364 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
#column_name ⇒ Object (readonly)
Returns the value of attribute column_name.
254 255 256 |
# File 'lib/table_fu.rb', line 254 def column_name @column_name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
254 255 256 |
# File 'lib/table_fu.rb', line 254 def @options end |
Instance Method Details
#macro? ⇒ Boolean
Returns whether there is a macro format for this datum
Returns: true if there is a macro format, false or nil otherwise
294 295 296 |
# File 'lib/table_fu.rb', line 294 def macro? @spreadsheet.formatting && @spreadsheet.formatting[@column_name].is_a?(Hash) end |
#macro_value ⇒ Object
Returns the macro’d format
Returns: The macro value
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/table_fu.rb', line 302 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 method = @spreadsheet.formatting[@column_name]['method'] arguments = @spreadsheet.formatting[@column_name]['arguments'].inject([]) do |arr,arg| arr << @row.column_for(arg) arr end TableFu::Formatting.send(method, *arguments) end |
#to_s ⇒ Object
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.
278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/table_fu.rb', line 278 def to_s ret = if macro? macro_value.to_s elsif @spreadsheet.formatting && format_method = @spreadsheet.formatting[column_name] TableFu::Formatting.send(format_method, @datum) || '' else @datum.to_s || '' end ret.force_encoding("UTF-8") if RUBY_VERSION > "1.9" ret end |
#value ⇒ Object
Returns the raw value of a datum
Returns: raw value of the datum, could be nil
325 326 327 328 329 330 331 |
# File 'lib/table_fu.rb', line 325 def value if @datum =~ /[0-9]+/ @datum.to_i else @datum end end |