Class: Record::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/record/field.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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, num, type) ⇒ Field

Returns a new instance of Field.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/record/field.rb', line 36

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?
      logger.warn "!!!! warn unknown type >#{type}< - no class mapping found; add missing type to Record::Type.registry[]"
      ## todo/fix:  raise exception!!!!
    end
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



30
31
32
# File 'lib/record/field.rb', line 30

def name
  @name
end

#numObject (readonly)

zero-based position index (0,1,2,3,…) todo: use/rename to index/idx/pos - add an alias name - why?



34
35
36
# File 'lib/record/field.rb', line 34

def num
  @num
end

#typeObject (readonly)

Returns the value of attribute type.



30
31
32
# File 'lib/record/field.rb', line 30

def type
  @type
end

Class Method Details

.build_loggerObject

add simple logger with debug flag/switch

use Parser.debug = true   # to turn on

todo/fix: use logutils instead of std logger - why? why not?


21
22
23
24
25
# File 'lib/record/field.rb', line 21

def self.build_logger()
  l = Logger.new( STDOUT )
  l.level = :info    ## set to :info on start; note: is 0 (debug) by default
  l
end

.loggerObject



26
# File 'lib/record/field.rb', line 26

def self.logger() @@logger ||= build_logger; end

Instance Method Details

#loggerObject



27
# File 'lib/record/field.rb', line 27

def logger()  self.class.logger; end

#typecast(value) ⇒ Object

cast (convert) from string value to type (e.g. float, integer, etc.)



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/record/field.rb', line 56

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
    logger.debug "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.)
    logger.debug "typecast >#{value}< to integer number >#{number}<"
    number
  else
    ## todo/fix: raise exception about unknow type
    logger.warn "!!!! unknown type >#{@type}< - don't know how to convert/typecast string value >#{value}<"
    logger.warn @type.inspect
    value
  end
end