From 9e5ee49ec55b9cb1c2b4444dee58f3dfaefc7c7e Mon Sep 17 00:00:00 2001 From: Nick Kallen Date: Mon, 19 May 2008 19:27:48 -0700 Subject: some drying up of boiler plate initialization and equality code --- lib/arel/extensions/class.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'lib/arel/extensions') diff --git a/lib/arel/extensions/class.rb b/lib/arel/extensions/class.rb index 09e6d86ed4..f37898e7d7 100644 --- a/lib/arel/extensions/class.rb +++ b/lib/arel/extensions/class.rb @@ -1,4 +1,30 @@ class Class + def attributes(*attrs) + @attributes = attrs + attr_reader *attrs + end + + def deriving(*methods) + methods.each { |m| derive m } + end + + def derive(method_name) + methods = { + :initialize => " + def #{method_name}(#{@attributes.join(',')}) + #{@attributes.collect { |a| "@#{a} = #{a}" }.join("\n")} + end + ", + :== => " + def ==(other) + #{name} === other && + #{@attributes.collect { |a| "@#{a} == other.#{a}" }.join(" &&\n")} + end + " + } + class_eval methods[method_name], __FILE__, __LINE__ + end + def hash_on(delegatee) define_method :eql? do |other| self == other -- cgit v1.2.3