Sha256: d99325c100d10d00d99e733908df190a874e57a36820e94d20e427a4c07e96a4

Contents?: true

Size: 870 Bytes

Versions: 1

Compression:

Stored size: 870 Bytes

Contents

module ActiveRecordLite
  module Base
    def fetch_lite(*select_sql)
      select_columns, columns = prepare(select_sql)
      _struct = Struct.new(self.name, *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

1 entries across 1 versions & 1 rubygems

Version Path
activerecord_lite-0.2.0 lib/activerecord_lite/base.rb