class Cascadev

Simple Class for CSV-like file management. Works like a spreadsheet. DOES NOT HAVE RESCUES FOR EMPTY / INVALID FILES!

Public Class Methods

new(database = nil) click to toggle source

Create a new data sheet. If argument is given, the gem will attempt to parse the string.

# File lib/cascadev.rb, line 8
def initialize(database = nil)
    if database != nil
        raw = database.split("\n")
        @meta = raw.shift.split(',')
        @database = Array.new
        raw.length.times do
            if raw[0].class == String
                self.check_args raw[0].split(',')
            else
                self.check_args raw[0]
            end
            @database.push raw.shift.split(',')
        end
    else
        @meta = Array.new
        @database = Array.new
    end
end

Public Instance Methods

check_args(args) click to toggle source

Check validity of arguments for most methods.

# File lib/cascadev.rb, line 156
def check_args(args)
    if args.class != Array
        raise '[CaScadeV] Could not set row: Invalid data type! Not an array!'
    end
    if !@database.empty?
        if args.length != @meta.length
            raise '[CaScadeV] Could not set row: Input doesn\'t match meta OR database currently holds data!'
        end
    end
    return true
end
elements() click to toggle source

Count the amount of rows in the database

# File lib/cascadev.rb, line 94
def elements
    return @database.length
end
enq(*args) click to toggle source

This method “pushes” the new row.

# File lib/cascadev.rb, line 75
def enq(*args)
    if args.length == 1 && args[0].class == String
        args = args[0].gsub(', ', ',').split(',')
        puts args.inspect
    end
    self.check_args(args)
    if @meta.empty?
        raise '[CaScadeV] Cannot enqueue: Meta is empty!'
    end
    stringargs = Array.new
    args.each do |a|
        stringargs.push a.to_s
    end
    @database.push stringargs
    return true
end
export(filename) click to toggle source

Export the database as .csv file

# File lib/cascadev.rb, line 106
def export(filename)
    File.open(filename, 'w') do |export|
        # Export meta
        export.puts @meta.join(',')
        # Export entire database
        @database.each do |element|
            export.puts element.join(',')
        end
    end
    return true
end
field(x, y) click to toggle source

Output only the selected fields via (column, row)

# File lib/cascadev.rb, line 100
def field(x, y)
    return @database[y-1][x-1]
end
find_by_column(column_id, pattern) click to toggle source

Find by column and pattern in db

# File lib/cascadev.rb, line 170
def find_by_column(column_id, pattern)
    found = []
    # puts "Searching in #{column_id.to_s} for #{pattern.to_s}..."
    for i in (1..elements)
        # puts "Matching with #{field(column_id, i)}..."
        if field(column_id, i) == pattern
            # puts "Occurence?"
            found.push [@database[i-1], i, column_id]
        end
    end
    return found
end
find_by_row(row_id, pattern) click to toggle source

Find by row and pattern in DB

# File lib/cascadev.rb, line 185
def find_by_row(row_id, pattern)
    found = []
    # puts row(row_id - 1).inspect
    for i in (1..(@meta.length))
        if field(i, row_id) == pattern
            found.push [@database[row_id-1], row_id, i]
        end
    end
    return found
end
import(filename, import_meta = true, ignore_first_line = false) click to toggle source

Import from a file. Does not come with a fail-safe for corrupt files if they squeeze through the parameter check.

# File lib/cascadev.rb, line 122
def import(filename, import_meta = true, ignore_first_line = false)
    if !File.exist?(filename) 
        raise '[CaScadeV] Cannot find specificed file ' + filename
    end
    File.open(filename, 'r') do |import|
        if import_meta
            self.set_meta(import.gets.chomp)
        end

        if !import_meta && ignore_first_line
            void = import.gets
            void = nil
        end

        import.each_line do |line|
            self.enq(line.chomp)
        end
    end
    return true
end
meta() click to toggle source

Return the meta line (top line) as array

# File lib/cascadev.rb, line 29
def meta
    if @meta == nil
        return Array.new
    else
        return @meta
    end
end
row(num) click to toggle source

This method returns the selected row. Starts with Row=1

# File lib/cascadev.rb, line 54
def row(num)
    return @database[num - 1]
end
set_meta(*new_meta) click to toggle source

If available, this will update the meta.

# File lib/cascadev.rb, line 39
def set_meta(*new_meta)
    if @database.length < 0
        raise '[CaScadeV] Cannot update meta: Database is not empty!'
    end
    if new_meta.length == 1 && new_meta[0].class == String
        new_meta = new_meta[0].split(',')
    end
    self.check_args(new_meta)
    @meta = new_meta
    return true
end
setrow(row_num, args) click to toggle source

This method overrides selected row.

# File lib/cascadev.rb, line 60
def setrow(row_num, args)
    if args.class == String
        args = args.split(',')
    end
    self.check_args(args)
    stringargs = Array.new
    args.each do |a|
        stringargs.push a.to_s
    end
    @database[row_num - 1] = stringargs
    return true
end
to_s() click to toggle source

Export as a CSV-String

# File lib/cascadev.rb, line 145
def to_s 
    export = []
    export.push @meta.join(',')
    @database.each do |element|
        export.push element.join(',')
    end
    return export.join("\n").chomp
end