class Baza::Row
Attributes
args[R]
data[R]
Public Class Methods
new(args)
click to toggle source
# File lib/baza/row.rb, line 10 def initialize(args) @args = {} args.each do |key, value| @args[key.to_sym] = value end @args[:col_id] ||= :id raise "No table given." unless @args[:table] if @args[:data] && (@args[:data].is_a?(Integer) || @args[:data].class.name == "Fixnum" || @args[:data].is_a?(String)) @data = {@args[:col_id].to_sym => @args[:data].to_s} reload elsif @args[:data] && @args.fetch(:data).is_a?(Hash) @data = {} @args.fetch(:data).each do |key, value| key = key.to_sym unless key.class.name == "Fixnum" @data[key] = value end elsif @args[:id] @data = {} @data[@args[:col_id].to_sym] = @args[:id] reload else raise ArgumentError, "Invalid data: #{@args[:data]} (#{@args[:data].class})" end end
Public Instance Methods
[](key)
click to toggle source
# File lib/baza/row.rb, line 82 def [](key) raise "No valid key given." unless key raise "No data was loaded on the object? Maybe you are trying to call a deleted object?" unless @data if @data.key?(key) return @data[key] elsif @data.key?(key.to_sym) return @data[key.to_sym] elsif @data.key?(key.to_s) return @data[key.to_s] end raise "No such key: #{key}." end
[]=(key, value)
click to toggle source
# File lib/baza/row.rb, line 97 def []=(key, value) update(key.to_sym => value) reload end
db()
click to toggle source
# File lib/baza/row.rb, line 37 def db @args.fetch(:db) end
delete()
click to toggle source
# File lib/baza/row.rb, line 68 def delete db.delete(@args.fetch(:table), @args.fetch(:col_id) => id) destroy end
destroy()
click to toggle source
# File lib/baza/row.rb, line 73 def destroy @args = nil @data = nil end
each(*args, &blk)
click to toggle source
# File lib/baza/row.rb, line 124 def each(*args, &blk) @data.each(*args, &blk) end
each_value(*args, &blk)
click to toggle source
# File lib/baza/row.rb, line 128 def each_value(*args, &blk) @data.each_value(*args, &blk) end
esc(str)
click to toggle source
# File lib/baza/row.rb, line 136 def esc(str) db.escape(str) end
id()
click to toggle source
# File lib/baza/row.rb, line 102 def id @data.fetch(@args.fetch(:col_id)) end
key?(key)
click to toggle source
# File lib/baza/row.rb, line 78 def key?(key) @data.key?(key.to_sym) end
knj?()
click to toggle source
# File lib/baza/row.rb, line 6 def knj? true end
method_missing(func_name, *args)
click to toggle source
Calls superclass method
# File lib/baza/row.rb, line 140 def method_missing(func_name, *args) if (match = func_name.to_s.match(/^(\S+)\?$/)) && @data.key?(match[1].to_sym) if @data.fetch(match[1].to_sym) == "1" || @data.fetch(match[1].to_sym) == "yes" return true elsif @data.fetch(match[1].to_sym) == "0" || @data.fetch(match[1].to_sym) == "no" return false end end super end
ob()
click to toggle source
# File lib/baza/row.rb, line 41 def ob return @args[:objects] if @args.key?(:objects) false end
Also aliased as: objects
reload()
click to toggle source
# File lib/baza/row.rb, line 48 def reload last_id = id data = db.single(@args[:table], @args[:col_id] => id) unless data raise Errno::ENOENT, "Could not find any data for the object with ID: '#{last_id}' in the table '#{@args[:table]}'." end @data = {} data.each do |key, value| @data[key.to_sym] = value end end
title()
click to toggle source
# File lib/baza/row.rb, line 110 def title return @data[@args.fetch(:col_title).to_sym] if @args[:col_title] if @data.key?(:title) return @data.fetch(:title) elsif @data.key?(:name) return @data.fetch(:name) end raise "'col_title' has not been set for the class: '#{self.class}'." end
Also aliased as: name
to_hash()
click to toggle source
# File lib/baza/row.rb, line 132 def to_hash @data.clone end
to_param()
click to toggle source
# File lib/baza/row.rb, line 106 def to_param id end
update(newdata)
click to toggle source
# File lib/baza/row.rb, line 61 def update(newdata) db.update(@args.fetch(:table), newdata, @args.fetch(:col_id) => id) reload ob.call("object" => self, "signal" => "update") if ob end