Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Change eval statement

  • Loading branch information...
commit 54af434b1e9f3766a4e9aaf970c60b49be34a41f 1 parent 9c022f2
@binarylogic authored
Showing with 16 additions and 17 deletions.
  1. +16 −17 lib/boolean_datetime_attribute.rb
View
33 lib/boolean_datetime_attribute.rb
@@ -8,26 +8,25 @@ module ClassMethods
def boolean_datetime_attribute(*datetime_fields)
datetime_fields.each do |field|
boolean_field = field.to_s.gsub(/_at$/, "")
- src = "def #{boolean_field}=(value)
- if (value == true || value == 'true' || value == 1 || value == '1')
- self.#{field} = Time.now if !#{boolean_field}?
- else
- self.#{field} = nil if #{boolean_field}?
+ class_eval <<-"end_eval", __FILE__, __LINE__
+ def #{boolean_field}=(value)
+ if (value == true || value == 'true' || value == 1 || value == '1')
+ self.#{field} = Time.now if !#{boolean_field}?
+ else
+ self.#{field} = nil if #{boolean_field}?
+ end
+ #{field}
end
- #{field}
- end
- def #{boolean_field}?
- !#{field}.blank? && #{field} <= Time.current
- end
- alias_method :#{boolean_field}, :#{boolean_field}?
+ def #{boolean_field}?
+ !#{field}.blank? && #{field} <= Time.current
+ end
+ alias_method :#{boolean_field}, :#{boolean_field}?
- def #{boolean_field}!
- update_attribute :#{field}, Time.now
- end
- "
-
- class_eval src
+ def #{boolean_field}!
+ update_attribute :#{field}, Time.now
+ end
+ end_eval
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.