Class: Csvdb::Table
- Inherits:
-
Object
- Object
- Csvdb::Table
- Defined in:
- lib/csvdb/table.rb
Instance Attribute Summary collapse
-
#cols ⇒ Object
Returns the value of attribute cols.
-
#table ⇒ Object
Returns the value of attribute table.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #add(attrs) ⇒ Object
- #all ⇒ Object
- #count ⇒ Object
- #create(attrs) ⇒ Object
- #find(idx = nil) ⇒ Object
-
#initialize(opts = {}) ⇒ Table
constructor
A new instance of Table.
- #join(table, opts = {}) ⇒ Object
- #pluck(header) ⇒ Object
- #pretty ⇒ Object
- #where(search_name = 'search') ⇒ Object
- #write(file = @file) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Table
Returns a new instance of Table.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/csvdb/table.rb', line 14 def initialize(opts = {}) if opts[:name] @table_name = opts[:name] else raise TableError, 'No name on that table' end if opts[:file] @file = opts[:file] table = CSV.read(@file) @cols = table[0].map.with_index {|head,idx| [head.to_sym,idx] }.to_h table.delete_at(0) elsif opts[:ary] table = opts[:ary] @cols = opts[:cols] else raise TableError, 'No table or file to parse.' end @cols.each do |col, val| add_attr(col, val) end @table = [] table.each_with_index do |row, idx| @table[idx] = Row.new(row, self, idx) end end |
Instance Attribute Details
#cols ⇒ Object
Returns the value of attribute cols.
12 13 14 |
# File 'lib/csvdb/table.rb', line 12 def cols @cols end |
#table ⇒ Object
Returns the value of attribute table.
12 13 14 |
# File 'lib/csvdb/table.rb', line 12 def table @table end |
#table_name ⇒ Object
Returns the value of attribute table_name.
12 13 14 |
# File 'lib/csvdb/table.rb', line 12 def table_name @table_name end |
Instance Method Details
#add(attrs) ⇒ Object
70 71 72 |
# File 'lib/csvdb/table.rb', line 70 def add(attrs) @table << Row.new(attrs, self, @table.length) end |
#all ⇒ Object
54 55 56 |
# File 'lib/csvdb/table.rb', line 54 def all @table end |
#count ⇒ Object
58 59 60 |
# File 'lib/csvdb/table.rb', line 58 def count @table.count end |
#create(attrs) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/csvdb/table.rb', line 62 def create(attrs) new_row = [] attrs.each do |att, new_val| new_row[@cols[att]] = new_val end @table << Row.new(new_row, self, @table.length) end |
#find(idx = nil) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/csvdb/table.rb', line 104 def find(idx = nil) if block_given? @table.select { |row| yield(row) }.first elsif idx @table[idx] else raise ArgumentError, 'Must pass either an index or a block.' end end |
#join(table, opts = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/csvdb/table.rb', line 82 def join(table, opts = {}) col1 = opts[:col1] ; idx1 = self.send(col1) col2 = opts[:col2] ; idx2 = self.send(col2) cols = ([col1] + (self.cols.keys - [col1]) .map { |k| "#{k}_#{self.table_name}".to_sym } + ((table.cols.keys - [col1]) .map { |k| "#{k}_#{table.table_name}".to_sym })) .map.with_index {|head,idx| [head.to_sym,idx] }.to_h joined = Table.new(ary: [], cols: cols, name: "#{self.table_name}.#{cols[:col1]} join #{table.table_name}.#{cols[:col2]}") ids = (self.pluck(col1) + table.pluck(col2)).uniq ids.each do |id| self.where { |row| row[idx1] == id }.table.each do |outer_row| table.where { |row| row[idx2] == id }.table.each do |inner_row| inner_row.delete_at(idx2) joined.add(outer_row + inner_row) end end end return joined end |
#pluck(header) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/csvdb/table.rb', line 114 def pluck(header) col = @cols[header.to_sym] ; vals = [] raise SearchError, "Column #{header} does not exist." unless col @table.each do |row| vals << row[col] end vals end |
#pretty ⇒ Object
123 124 125 126 |
# File 'lib/csvdb/table.rb', line 123 def pretty table = Terminal::Table.new headings: @cols.keys, rows: @table puts table end |
#where(search_name = 'search') ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/csvdb/table.rb', line 74 def where(search_name = 'search') Table.new( ary: @table.select { |row| yield(row) }, cols: self.cols, name: search_name ) end |
#write(file = @file) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/csvdb/table.rb', line 44 def write(file = @file) File.open(file, 'w') unless File.file?(file) CSV.open(file, 'wb') do |csv| csv << @cols.keys @table.each do |row| csv << row end end end |