Module: TableTennis::Util::Identify

Includes:
MemoWise
Defined in:
lib/table_tennis/util/identify.rb

Overview

Helpers for measuring and truncating strings.

Class Method Summary collapse

Class Method Details

.float?(str) ⇒ Boolean

Returns:

  • (Boolean)


46
# File 'lib/table_tennis/util/identify.rb', line 46

def float?(str) = str.match?(/\A-?[\d,]+[.]\d*\Z/)

.identify(value) ⇒ Object

Try to identify a single cell value. Peer into strings.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/table_tennis/util/identify.rb', line 28

def identify(value)
  case value
  when nil, "" then return nil
  when String
    return :float if float?(value)
    return :int if int?(value)
    return nil if na?(value)
    return :string
  when Float then return :float
  when Numeric then return :int
  end
  return :time if time?(value)
  :unknown
end

.identify_column(values) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/table_tennis/util/identify.rb', line 14

def identify_column(values)
  # grab 100, what do we have?
  types = values.filter_map { identify(_1) }.uniq
  case types.length
  when 0 # all nils, too bad
  when 1
    types.first # one type, it wins
  when 2
    :float if types.sort == %i[float int]
  end
  # all mixed up, can't do much with it
end

.int?(str) ⇒ Boolean

Returns:

  • (Boolean)


47
# File 'lib/table_tennis/util/identify.rb', line 47

def int?(str) = str.match?(/\A-?[\d,]+\Z/)

.na?(str) ⇒ Boolean

tests

Returns:

  • (Boolean)


44
# File 'lib/table_tennis/util/identify.rb', line 44

def na?(str) = str.match?(/\A(n\/a|na|none|\s+)\Z/i)

.number?(str) ⇒ Boolean

Returns:

  • (Boolean)


45
# File 'lib/table_tennis/util/identify.rb', line 45

def number?(str) = str.match?(/\A-?[\d,]+(?:[.]?\d*)?\Z/)

.time?(value) ⇒ Boolean

Returns:

  • (Boolean)


48
# File 'lib/table_tennis/util/identify.rb', line 48

def time?(value) = value.respond_to?(:strftime)