aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/extensions/class.rb
blob: f37898e7d7feace3ceab3291cc6c9e6c9a23a961 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
    end
    
    define_method :hash do
      @hash ||= delegatee.hash
    end
  end
end