Class: FatCore::Column
- Inherits:
-
Object
- Object
- FatCore::Column
- 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
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Return a new Column appending the items of other to our items, checking for type compatibility.
-
#<<(itm) ⇒ Object
Append item to end of the column.
-
#[](k) ⇒ Object
Attributes.
- #all? ⇒ Boolean
- #any? ⇒ Boolean
- #avg ⇒ Object
- #empty? ⇒ Boolean
-
#first ⇒ Object
Aggregates.
-
#initialize(header:, items: []) ⇒ Column
constructor
A new instance of Column.
- #last ⇒ Object
- #last_i ⇒ Object
- #max ⇒ Object
- #min ⇒ Object
- #none? ⇒ Boolean
- #one? ⇒ Boolean
-
#rng_s ⇒ Object
Return a string that of the first and last values.
- #size ⇒ Object
- #sum ⇒ Object
- #to_a ⇒ Object
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
#header ⇒ Object (readonly)
Returns the value of attribute header.
7 8 9 |
# File 'lib/fat_core/column.rb', line 7 def header @header end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
7 8 9 |
# File 'lib/fat_core/column.rb', line 7 def items @items end |
#type ⇒ Object (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
90 91 92 93 |
# File 'lib/fat_core/column.rb', line 90 def all? only_with('all?', 'Boolean') items.compact.all? end |
#any? ⇒ Boolean
85 86 87 88 |
# File 'lib/fat_core/column.rb', line 85 def any? only_with('any?', 'Boolean') items.compact.any? end |
#avg ⇒ Object
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
35 36 37 |
# File 'lib/fat_core/column.rb', line 35 def empty? items.empty? end |
#first ⇒ Object
Aggregates
47 48 49 |
# File 'lib/fat_core/column.rb', line 47 def first items.compact.first end |
#last ⇒ Object
51 52 53 |
# File 'lib/fat_core/column.rb', line 51 def last items.compact.last end |
#last_i ⇒ Object
39 40 41 |
# File 'lib/fat_core/column.rb', line 39 def last_i size - 1 end |
#max ⇒ Object
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 |
#min ⇒ Object
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
95 96 97 98 |
# File 'lib/fat_core/column.rb', line 95 def none? only_with('any?', 'Boolean') items.compact.none? end |
#one? ⇒ Boolean
100 101 102 103 |
# File 'lib/fat_core/column.rb', line 100 def one? only_with('any?', 'Boolean') items.compact.one? end |
#rng_s ⇒ Object
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 |
#size ⇒ Object
31 32 33 |
# File 'lib/fat_core/column.rb', line 31 def size items.size end |
#sum ⇒ Object
60 61 62 63 |
# File 'lib/fat_core/column.rb', line 60 def sum only_with('sum', 'Numeric', 'String') items.compact.sum end |
#to_a ⇒ Object
27 28 29 |
# File 'lib/fat_core/column.rb', line 27 def to_a items end |