Removing old fashioned class_evals (more to go)

This commit is contained in:
Sam Lown 2011-06-25 01:49:15 +02:00
parent 31b52ba012
commit a8a1372e57
3 changed files with 25 additions and 37 deletions

View file

@ -183,19 +183,19 @@ module CouchRest
casted_by[casted_by_property.to_s] << obj.id casted_by[casted_by_property.to_s] << obj.id
end end
end end
def << obj def << obj
check_obj(obj) check_obj(obj)
casted_by[casted_by_property.to_s] << obj.id casted_by[casted_by_property.to_s] << obj.id
super(obj) super(obj)
end end
def push(obj) def push(obj)
check_obj(obj) check_obj(obj)
casted_by[casted_by_property.to_s].push obj.id casted_by[casted_by_property.to_s].push obj.id
super(obj) super(obj)
end end
def unshift(obj) def unshift(obj)
check_obj(obj) check_obj(obj)
casted_by[casted_by_property.to_s].unshift obj.id casted_by[casted_by_property.to_s].unshift obj.id
@ -212,7 +212,7 @@ module CouchRest
casted_by[casted_by_property.to_s].pop casted_by[casted_by_property.to_s].pop
super super
end end
def shift def shift
casted_by[casted_by_property.to_s].shift casted_by[casted_by_property.to_s].shift
super super

View file

@ -149,15 +149,13 @@ module CouchRest
# These properties are casted as Time objects, so they should always # These properties are casted as Time objects, so they should always
# be set to UTC. # be set to UTC.
def timestamps! def timestamps!
class_eval <<-EOS, __FILE__, __LINE__ property(:updated_at, Time, :read_only => true, :protected => true, :auto_validation => false)
property(:updated_at, Time, :read_only => true, :protected => true, :auto_validation => false) property(:created_at, Time, :read_only => true, :protected => true, :auto_validation => false)
property(:created_at, Time, :read_only => true, :protected => true, :auto_validation => false)
set_callback :save, :before do |object| set_callback :save, :before do |object|
write_attribute('updated_at', Time.now) write_attribute('updated_at', Time.now)
write_attribute('created_at', Time.now) if object.new? write_attribute('created_at', Time.now) if object.new?
end end
EOS
end end
protected protected
@ -191,42 +189,32 @@ module CouchRest
# defines the getter for the property (and optional aliases) # defines the getter for the property (and optional aliases)
def create_property_getter(property) def create_property_getter(property)
# meth = property.name define_method(property.name) do
class_eval <<-EOS, __FILE__, __LINE__ + 1 read_attribute(property.name)
def #{property.name} end
read_attribute('#{property.name}')
end
EOS
if ['boolean', TrueClass.to_s.downcase].include?(property.type.to_s.downcase) if ['boolean', TrueClass.to_s.downcase].include?(property.type.to_s.downcase)
class_eval <<-EOS, __FILE__, __LINE__ define_method("#{property.name}?") do
def #{property.name}? value = read_attribute(property.name)
value = read_attribute('#{property.name}') !(value.nil? || value == false)
!(value.nil? || value == false) end
end
EOS
end end
if property.alias if property.alias
class_eval <<-EOS, __FILE__, __LINE__ + 1 alias_method(property.alias, property.name.to_sym)
alias #{property.alias.to_sym} #{property.name.to_sym}
EOS
end end
end end
# defines the setter for the property (and optional aliases) # defines the setter for the property (and optional aliases)
def create_property_setter(property) def create_property_setter(property)
property_name = property.name name = property.name
class_eval <<-EOS
def #{property_name}=(value) define_method("#{name}=") do |value|
write_attribute('#{property_name}', value) write_attribute(name, value)
end end
EOS
if property.alias if property.alias
class_eval <<-EOS alias_method "#{property.alias}=", "#{name}="
alias #{property.alias.to_sym}= #{property_name.to_sym}=
EOS
end end
end end

View file

@ -1,7 +1,7 @@
require 'client' require 'client'
require 'sale_entry' require 'sale_entry'
class SaleInvoice < CouchRest::Model::Base class SaleInvoice < CouchRest::Model::Base
use_database DB use_database DB
belongs_to :client belongs_to :client