lib/baza/row.rb in baza-0.0.20 vs lib/baza/row.rb in baza-0.0.21
- old
+ new
@@ -11,28 +11,28 @@
@args = {}
args.each do |key, value|
@args[key.to_sym] = value
end
- @args[:objects] = $objects if !@args[:objects] && $objects && $objects.is_a?(Baza::ModelHandler)
@args[:col_id] ||= :id
raise "No table given." unless @args[:table]
if @args[:data] && (@args[:data].is_a?(Integer) || @args[:data].is_a?(Fixnum) || @args[:data].is_a?(String))
@data = {@args[:col_id].to_sym => @args[:data].to_s}
reload
- elsif @args[:data] && @args[:data].is_a?(Hash)
+ elsif @args[:data] && @args.fetch(:data).is_a?(Hash)
@data = {}
- @args[:data].each do |key, value|
- @data[key.to_sym] = value
+ @args.fetch(:data).each do |key, value|
+ key = key.to_sym unless key.is_a?(Fixnum)
+ @data[key] = value
end
elsif @args[:id]
@data = {}
@data[@args[:col_id].to_sym] = @args[:id]
reload
else
- raise ArgumentError.new("Invalid data: #{@args[:data]} (#{@args[:data].class})")
+ raise ArgumentError, "Invalid data: #{@args[:data]} (#{@args[:data].class})"
end
end
def db
@args.fetch(:db)
@@ -41,11 +41,11 @@
def ob
return @args[:objects] if @args.key?(:objects)
false
end
- alias_method :objects, :ob
+ alias objects ob
def reload
last_id = id
data = db.single(@args[:table], @args[:col_id] => id)
unless data
@@ -57,18 +57,18 @@
@data[key.to_sym] = value
end
end
def update(newdata)
- db.update(@args[:table], newdata, @args[:col_id] => id)
+ db.update(@args.fetch(:table), newdata, @args.fetch(:col_id) => id)
reload
ob.call("object" => self, "signal" => "update") if ob
end
def delete
- db.delete(@args[:table], @args[:col_id] => id)
+ db.delete(@args.fetch(:table), @args.fetch(:col_id) => id)
destroy
end
def destroy
@args = nil
@@ -98,30 +98,30 @@
update(key.to_sym => value)
reload
end
def id
- @data[@args[:col_id]]
+ @data.fetch(@args.fetch(:col_id))
end
def to_param
id
end
def title
- return @data[@args[:col_title].to_sym] if @args[:col_title]
+ return @data[@args.fetch(:col_title).to_sym] if @args[:col_title]
if @data.key?(:title)
- return @data[:title]
+ return @data.fetch(:title)
elsif @data.key?(:name)
- return @data[:name]
+ return @data.fetch(:name)
end
raise "'col_title' has not been set for the class: '#{self.class}'."
end
- alias_method :name, :title
+ alias name title
def each(*args, &blk)
@data.each(*args, &blk)
end
@@ -136,13 +136,13 @@
def esc(str)
db.escape(str)
end
def method_missing(func_name, *args)
- if match = func_name.to_s.match(/^(\S+)\?$/) && @data.key?(match[1].to_sym)
- if @data[match[1].to_sym] == "1" || @data[match[1].to_sym] == "yes"
+ 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[match[1].to_sym] == "0" || @data[match[1].to_sym] == "no"
+ elsif @data.fetch(match[1].to_sym) == "0" || @data.fetch(match[1].to_sym) == "no"
return false
end
end
super