class LocalModel::CSV
Public Class Methods
all()
click to toggle source
# File lib/local_model/csv.rb, line 47 def self.all all_instances = [] each_record do |row| all_instances << new_from_record(row) end all_instances end
count()
click to toggle source
# File lib/local_model/csv.rb, line 55 def self.count total = 0 self.each_record{ total += 1 } total end
create(**args)
click to toggle source
# File lib/local_model/csv.rb, line 123 def self.create(**args) inst = new(**args) inst.save inst end
create!(**args)
click to toggle source
# File lib/local_model/csv.rb, line 129 def self.create!(**args) inst = new(**args) inst.save! inst end
destroy_all()
click to toggle source
# File lib/local_model/csv.rb, line 61 def self.destroy_all self.all.each{ |obj| obj.destroy } end
find(id)
click to toggle source
# File lib/local_model/csv.rb, line 110 def self.find(id) return self.find_by(id: id) end
find!(id)
click to toggle source
# File lib/local_model/csv.rb, line 114 def self.find!(id) found_record = find(id) if !found_record raise LocalModel::RecordNotFound.new else found_record end end
find_by(**args)
click to toggle source
# File lib/local_model/csv.rb, line 78 def self.find_by(**args) found_record = find_record do |row| matched = true args.each do |k,v| if row[k] != v matched = false break end end return new_from_record(row) if matched end nil end
first()
click to toggle source
# File lib/local_model/csv.rb, line 92 def self.first all.first end
last()
click to toggle source
# File lib/local_model/csv.rb, line 100 def self.last all.last end
new(**args)
click to toggle source
# File lib/local_model/csv.rb, line 135 def initialize(**args) args.each do |k,v| self.send("#{k}=", v) end self.id = nil end
new_from_record(row)
click to toggle source
# File lib/local_model/csv.rb, line 104 def self.new_from_record(row) obj = new(**row.to_h) obj.id = row[:id] obj end
schema() { |builder| ... }
click to toggle source
# File lib/local_model/csv.rb, line 6 def self.schema(&block) builder = SchemaBuilder.new(self) yield builder schema_dict = builder.schema.dup self.define_method :get_schema do schema_dict end self.define_singleton_method :get_schema do schema_dict end cols = builder.schema.keys.dup self.define_method :columns do cols end self.define_singleton_method :columns do cols end schema_data = cols.each_with_index.reduce({}) do |mem, (key,i)| mem[key] = {} mem[key][:adapter] = get_adapter(schema_dict[key]) mem[key][:type] = schema_dict[key] mem[key][:column_number] = i mem end self.define_method :schema_data do schema_data end if !File.file?(self.storage_path) CSV.open(self.storage_path, 'wb') do |csv| csv << cols.map(&:to_s) end end end
second()
click to toggle source
# File lib/local_model/csv.rb, line 96 def self.second all[1] end
where(**args)
click to toggle source
# File lib/local_model/csv.rb, line 65 def self.where(**args) all_records do |row| found = true args.each do |k,v| if row[k] != v found = false break end end found end.map{|r| new_from_record(r) } end
Public Instance Methods
destroy()
click to toggle source
# File lib/local_model/csv.rb, line 151 def destroy self.class.delete_row({id: self.id}) end
reload()
click to toggle source
# File lib/local_model/csv.rb, line 146 def reload raise LocalModel::RecordNotFound if !self.id self.class.find!(self.id) end
saved?()
click to toggle source
# File lib/local_model/csv.rb, line 142 def saved? !self.id.nil? end