lib/fat_table/table.rb in fat_table-0.3.1 vs lib/fat_table/table.rb in fat_table-0.3.3
- old
+ new
@@ -156,11 +156,12 @@
def self.from_sql(query)
msg = 'FatTable.db must be set with FatTable.connect'
raise UserError, msg if FatTable.db.nil?
result = Table.new
- FatTable.db[query].each do |h|
+ rows = FatTable.db[query]
+ rows.each do |h|
result << h
end
result
end
@@ -295,10 +296,12 @@
###########################################################################
# :category: Attributes
# Return the table's Column with the given +key+ as its header.
+ # @param key [Symbol] symbol for header of column to return
+ # @return [FatTable::Column]
def column(key)
columns.detect { |c| c.header == key.as_sym }
end
# :category: Attributes
@@ -309,10 +312,19 @@
column(key).type
end
# :category: Attributes
+ # Set the column type for Column with the given +key+ as a String type,
+ # but only if empty. Otherwise, we would have to worry about converting
+ # existing items in the column to String. Perhaps that's a TODO.
+ def set_column_to_string_type(key)
+ column(key).force_to_string_type
+ end
+
+ # :category: Attributes
+
# Return the array of items of the column with the given header symbol
# +key+, or if +key+ is an Integer, return that row at that index. So a
# table's rows can be accessed by number, and its columns can be accessed by
# column header. Also, double indexing works in either row-major or
# column-major order: \tab\[:id\]\[8\] returns the 9th item in the column
@@ -978,10 +990,15 @@
# the tables have N and M rows respectively, the joined table will
# have N * M rows.
#
# Any groups present in either Table are eliminated in the output Table. See
# the README for examples.
+ # @param other [FatTable::Table] table to join with self
+ # @param exps [Array<String>, Array<Symbol>] table to join with self
+ # @param join_type [Array<String>, Array<Symbol>] type of join :inner, :left, :right, :full, :cross
+ # @return [FatTable::Table] result of joining self to other
+ #
def join(other, *exps, join_type: :inner)
unless other.is_a?(Table)
raise UserError, 'need other table as first argument to join'
end
unless JOIN_TYPES.include?(join_type)
@@ -1272,19 +1289,28 @@
public
# :category: Constructors
+ # Add a group boundary mark at the given row, or at the end of the table
+ # by default.
+ def add_boundary(at_row = nil)
+ row = at_row || (size - 1)
+ @boundaries << row
+ end
+
+ # :category: Constructors
+
# Add a +row+ represented by a Hash having the headers as keys. If +mark:+
# is set true, mark this row as a boundary. All tables should be built
# ultimately using this method as a primitive.
def add_row(row, mark: false)
row.each_pair do |k, v|
key = k.as_sym
columns << Column.new(header: k) unless column?(k)
column(key) << v
end
- @boundaries << (size - 1) if mark
+ add_boundary if mark
self
end
# :category: Constructors