diff options
Diffstat (limited to 'lib/arel/algebra/extensions/class.rb')
-rw-r--r-- | lib/arel/algebra/extensions/class.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/arel/algebra/extensions/class.rb b/lib/arel/algebra/extensions/class.rb new file mode 100644 index 0000000000..520111b90f --- /dev/null +++ b/lib/arel/algebra/extensions/class.rb @@ -0,0 +1,32 @@ +module Arel + module ClassExtensions + 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 + + Class.send(:include, self) + end +end + |