aboutsummaryrefslogblamecommitdiffstats
path: root/lib/active_relation/relations/rename.rb
blob: 9ab07707a000376c1ba825f300795d785fd13057 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                     
                         
                                      
 
                                        
                                               
                                                                                


                 

                                       

                                       
       

                  
                                                     

       
           
                           
                                                                                               
       

     
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