Class: FatCore::Column

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

Overview

Column objects are just a thin wrapper around an Array to allow columns to be summed and have other operations performed on them, but compacting out nils before proceeding. My original attempt to do this by monkey-patching Array turned out badly. This works much nicer.

Constant Summary collapse

TYPES =
%w(NilClass Boolean DateTime Numeric String)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header:, items: []) ⇒ Column

Returns a new instance of Column.



11
12
13
14
15
16
17
# File 'lib/fat_core/column.rb', line 11

def initialize(header:, items: [])
  @header = header.as_sym
  @type = 'NilClass'
  raise "Unknown column type '#{type}" unless TYPES.include?(@type.to_s)
  @items = []
  items.each { |i| self << i }
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



7
8
9
# File 'lib/fat_core/column.rb', line 7

def header
  @header
end

#itemsObject (readonly)

Returns the value of attribute items.



7
8
9
# File 'lib/fat_core/column.rb', line 7

def items
  @items
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/fat_core/column.rb', line 7

def type
  @type
end

Instance Method Details

#+(other) ⇒ Object

Return a new Column appending the items of other to our items, checking for type compatibility.



126
127
128
129
# File 'lib/fat_core/column.rb', line 126

def +(other)
  raise 'Cannot combine columns with different types' unless type == other.type
  Column.new(header: header, items: items + other.items)
end

#<<(itm) ⇒ Object

Append item to end of the column



120
121
122
# File 'lib/fat_core/column.rb', line 120

def <<(itm)
  items << convert_to_type(itm)
end

#[](k) ⇒ Object

Attributes



23
24
25
# File 'lib/fat_core/column.rb', line 23

def [](k)
  items[k]
end

#all?Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/fat_core/column.rb', line 90

def all?
  only_with('all?', 'Boolean')
  items.compact.all?
end

#any?Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/fat_core/column.rb', line 85

def any?
  only_with('any?', 'Boolean')
  items.compact.any?
end

#avgObject



75
76
77
78
79
80
81
82
83
# File 'lib/fat_core/column.rb', line 75

def avg
  only_with('avg', 'DateTime', 'Numeric')
  if type == 'DateTime'
    avg_jd = items.compact.map(&:jd).sum / items.compact.size.to_d
    DateTime.jd(avg_jd)
  else
    sum / items.compact.size.to_d
  end
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/fat_core/column.rb', line 35

def empty?
  items.empty?
end

#firstObject

Aggregates



47
48
49
# File 'lib/fat_core/column.rb', line 47

def first
  items.compact.first
end

#lastObject



51
52
53
# File 'lib/fat_core/column.rb', line 51

def last
  items.compact.last
end

#last_iObject



39
40
41
# File 'lib/fat_core/column.rb', line 39

def last_i
  size - 1
end

#maxObject



70
71
72
73
# File 'lib/fat_core/column.rb', line 70

def max
  only_with('max', 'NilClass', 'Numeric', 'String', 'DateTime')
  items.compact.max
end

#minObject



65
66
67
68
# File 'lib/fat_core/column.rb', line 65

def min
  only_with('min', 'NilClass', 'Numeric', 'String', 'DateTime')
  items.compact.min
end

#none?Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/fat_core/column.rb', line 95

def none?
  only_with('any?', 'Boolean')
  items.compact.none?
end

#one?Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/fat_core/column.rb', line 100

def one?
  only_with('any?', 'Boolean')
  items.compact.one?
end

#rng_sObject

Return a string that of the first and last values.



56
57
58
# File 'lib/fat_core/column.rb', line 56

def rng_s
  "#{first}..#{last}"
end

#sizeObject



31
32
33
# File 'lib/fat_core/column.rb', line 31

def size
  items.size
end

#sumObject



60
61
62
63
# File 'lib/fat_core/column.rb', line 60

def sum
  only_with('sum', 'Numeric', 'String')
  items.compact.sum
end

#to_aObject



27
28
29
# File 'lib/fat_core/column.rb', line 27

def to_a
  items
end