Sha256: 920a1c2ada2fd7cc1a89846f420449cb37b4db766fec1357035f5aa959ec2ab0

Contents?: true

Size: 928 Bytes

Versions: 36

Compression:

Stored size: 928 Bytes

Contents

module ActiveRecord
  ###
  # This class encapsulates a Result returned from calling +exec_query+ on any
  # database connection adapter. For example:
  #
  #   x = ActiveRecord::Base.connection.exec_query('SELECT * FROM foo')
  #   x # => #<ActiveRecord::Result:0xdeadbeef>
  class Result
    include Enumerable

    attr_reader :columns, :rows

    def initialize(columns, rows)
      @columns   = columns
      @rows      = rows
      @hash_rows = nil
    end

    def each
      hash_rows.each { |row| yield row }
    end

    def to_hash
      hash_rows
    end

    private
    def hash_rows
      @hash_rows ||=
        begin
          # We freeze the strings to prevent them getting duped when
          # used as keys in ActiveRecord::Model's @attributes hash
          columns = @columns.map { |c| c.dup.freeze }
          @rows.map { |row|
            Hash[columns.zip(row)]
          }
        end
    end
  end
end

Version data entries

36 entries across 31 versions & 6 rubygems

Version Path
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/activerecord-3.2.22.5/lib/active_record/result.rb
activerecord-3.2.22.5 lib/active_record/result.rb
activerecord-3.2.22.4 lib/active_record/result.rb
activerecord-3.2.22.3 lib/active_record/result.rb
activerecord-3.2.22.2 lib/active_record/result.rb
activerecord-3.2.22.1 lib/active_record/result.rb
activerecord-3.2.22 lib/active_record/result.rb
activerecord-3.2.21 lib/active_record/result.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/result.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/result.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/result.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/result.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/result.rb
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/result.rb
activerecord-3.2.20 lib/active_record/result.rb
activerecord-3.2.19 lib/active_record/result.rb
activerecord-3.2.18 lib/active_record/result.rb
activerecord-3.2.17 lib/active_record/result.rb
activerecord-3.2.16 lib/active_record/result.rb
activerecord-3.2.15 lib/active_record/result.rb