aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/rename.rb
blob: 9ab07707a000376c1ba825f300795d785fd13057 (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
module ActiveRelation
  class Rename < Compound
    attr_reader :attribute, :pseudonym

    def initialize(relation, pseudonyms)
      @attribute, @pseudonym = pseudonyms.shift
      @relation = pseudonyms.empty?? relation : Rename.new(relation, pseudonyms)
    end

    def ==(other)
      self.class == other.class     and
      relation   == other.relation  and
      attribute  == other.attribute and
      pseudonym  == other.pseudonym
    end
    
    def attributes
      relation.attributes.collect(&method(:christen))
    end
    
    private
    def christen(attribute)
      (attribute =~ self.attribute ? attribute.as(pseudonym) : attribute).bind(self) rescue nil
    end
  end
end