Skip to content

Commit

Permalink
Removing old fashioned class_evals (more to go)
Browse files Browse the repository at this point in the history
  • Loading branch information
samlown committed Jun 24, 2011
1 parent 31b52ba commit a8a1372
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 37 deletions.
8 changes: 4 additions & 4 deletions lib/couchrest/model/associations.rb
Expand Up @@ -183,19 +183,19 @@ def initialize(array, property, parent)
casted_by[casted_by_property.to_s] << obj.id
end
end

def << obj
check_obj(obj)
casted_by[casted_by_property.to_s] << obj.id
super(obj)
end

def push(obj)
check_obj(obj)
casted_by[casted_by_property.to_s].push obj.id
super(obj)
end

def unshift(obj)
check_obj(obj)
casted_by[casted_by_property.to_s].unshift obj.id
Expand All @@ -212,7 +212,7 @@ def pop
casted_by[casted_by_property.to_s].pop
super
end

def shift
casted_by[casted_by_property.to_s].shift
super
Expand Down
52 changes: 20 additions & 32 deletions lib/couchrest/model/properties.rb
Expand Up @@ -149,15 +149,13 @@ def property(name, *options, &block)
# These properties are casted as Time objects, so they should always
# be set to UTC.
def timestamps!
class_eval <<-EOS, __FILE__, __LINE__
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(:updated_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|
write_attribute('updated_at', Time.now)
write_attribute('created_at', Time.now) if object.new?
end
EOS
set_callback :save, :before do |object|
write_attribute('updated_at', Time.now)
write_attribute('created_at', Time.now) if object.new?
end
end

protected
Expand Down Expand Up @@ -191,42 +189,32 @@ def define_property(name, options={}, &block)

# defines the getter for the property (and optional aliases)
def create_property_getter(property)
# meth = property.name
class_eval <<-EOS, __FILE__, __LINE__ + 1
def #{property.name}
read_attribute('#{property.name}')
end
EOS
define_method(property.name) do
read_attribute(property.name)
end

if ['boolean', TrueClass.to_s.downcase].include?(property.type.to_s.downcase)
class_eval <<-EOS, __FILE__, __LINE__
def #{property.name}?
value = read_attribute('#{property.name}')
!(value.nil? || value == false)
end
EOS
define_method("#{property.name}?") do
value = read_attribute(property.name)
!(value.nil? || value == false)
end
end

if property.alias
class_eval <<-EOS, __FILE__, __LINE__ + 1
alias #{property.alias.to_sym} #{property.name.to_sym}
EOS
alias_method(property.alias, property.name.to_sym)
end
end

# defines the setter for the property (and optional aliases)
def create_property_setter(property)
property_name = property.name
class_eval <<-EOS
def #{property_name}=(value)
write_attribute('#{property_name}', value)
end
EOS
name = property.name

define_method("#{name}=") do |value|
write_attribute(name, value)
end

if property.alias
class_eval <<-EOS
alias #{property.alias.to_sym}= #{property_name.to_sym}=
EOS
alias_method "#{property.alias}=", "#{name}="
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/models/sale_invoice.rb
@@ -1,7 +1,7 @@
require 'client'
require 'sale_entry'

class SaleInvoice < CouchRest::Model::Base
class SaleInvoice < CouchRest::Model::Base
use_database DB

belongs_to :client
Expand Down

0 comments on commit a8a1372

Please sign in to comment.