Sha256: 2fe043f4f57133cc82c7b3cd0e6c0d8707260d70a223587f856b0a10e5314421

Contents?: true

Size: 859 Bytes

Versions: 2

Compression:

Stored size: 859 Bytes

Contents

module ActiveRecordLite
  module Base
    def fetch_lite(*select_sql)
      select_columns, columns = prepare(select_sql)
      _struct = Struct.new(*columns)
      sql = select(select_columns).to_sql
      self.connection.select_all(sql).map { |result| _struct.new(*result.values) }
    end

    def pluck_first(attribute)
      order(attribute).pluck(attribute).first
    end

    def pluck_last(attribute)
      order(attribute).pluck(attribute).last
    end

    private
    def prepare(select_sql)
      first_key = select_sql[0]
      if first_key.is_a?(Hash)
        prepare_hash(first_key)
      else
        [select_sql, select_sql]
      end
    end

    def prepare_hash(select_sql)
      columns = select_sql.keys
      select_columns = select_sql.map { |key, value| "#{value} #{key}" }.join(",")
      [select_columns, columns]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord_lite-0.4.0 lib/activerecord_lite/base.rb
activerecord_lite-0.1.0 lib/activerecord_lite/base.rb