Class: CsvRecord::Field
- Inherits:
-
Object
- Object
- CsvRecord::Field
- Defined in:
- lib/csvrecord/base.rb
Overview
note on naming:
use naming convention from awk and tabular data package/json schema for now
use - records (use rows for "raw" untyped (string) data rows )
- fields (NOT columns or attributes) -- might add an alias later - why? why not?
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
ruby record class field.
-
#num ⇒ Object
readonly
zero-based position index (0,1,2,3,…) todo: use/rename to index/idx/pos - add an alias name - why?.
-
#type ⇒ Object
readonly
ruby record class field.
Instance Method Summary collapse
-
#initialize(name, num, type) ⇒ Field
constructor
A new instance of Field.
-
#typecast(value) ⇒ Object
cast (convert) from string value to type (e.g. float, integer, etc.).
Constructor Details
#initialize(name, num, type) ⇒ Field
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/csvrecord/base.rb', line 18 def initialize( name, num, type ) ## note: always symbol-ify (to_sym) name and type ## todo: add title or titles for header field/column title as is e.g. 'Team 1' etc. ## incl. numbers only or even an empty header title @name = name.to_sym @num = num if type.is_a?( Class ) @type = type ## assign class to its own property - why? why not? else @type = Type.registry[type.to_sym] if @type.nil? puts "!!!! warn unknown type >#{type}< - no class mapping found; add missing type to CsvRecord::Type.registry[]" ## todo/fix: raise exception!!!! end end end |
Instance Attribute Details
#name ⇒ Object (readonly)
ruby record class field
12 13 14 |
# File 'lib/csvrecord/base.rb', line 12 def name @name end |
#num ⇒ Object (readonly)
zero-based position index (0,1,2,3,…) todo: use/rename to index/idx/pos - add an alias name - why?
16 17 18 |
# File 'lib/csvrecord/base.rb', line 16 def num @num end |
#type ⇒ Object (readonly)
ruby record class field
12 13 14 |
# File 'lib/csvrecord/base.rb', line 12 def type @type end |
Instance Method Details
#typecast(value) ⇒ Object
cast (convert) from string value to type (e.g. float, integer, etc.)
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/csvrecord/base.rb', line 38 def typecast( value ) ## cast (convert) from string value to type (e.g. float, integer, etc.) ## todo: add typecast to class itself (monkey patch String/Float etc. - why? why not) ## use __typecast or something? ## or use a new "wrapper" class Type::String or StringType - why? why not? ## convert string value to (field) type if @type == String value ## pass through as is elsif @type == Float ## note: allow/check for nil values - why? why not? float = (value.nil? || value.empty?) ? nil : value.to_f puts "typecast >#{value}< to float number >#{float}<" float elsif @type == Integer number = (value.nil? || value.empty?) ? nil : value.to_i(10) ## always use base10 for now (e.g. 010 => 10 etc.) puts "typecast >#{value}< to integer number >#{number}<" number else ## todo/fix: raise exception about unknow type pp @type puts "!!!! unknown type >#{@type}< - don't know how to convert/typecast string value >#{value}<" value end end |