Sha256: 788a674e3c8ccebcf537d0a98875db0f78ef07cc5e930cf060d80f802bec0adb

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

# -*- coding: utf-8 -*-
#
# @file 
# @brief
# @author ongaeshi
# @date   2012/05/29

require 'milkode/common/util'

module Milkode
  class PackageTable
    include Enumerable

    def self.define_schema
      Groonga::Schema.define do |schema|
        schema.create_table("packages", :type => :hash) do |table|
          table.string("name")
          table.string("directory")
          table.time("addtime")
          table.time("updatetime")
          table.time("viewtime")
          table.time("favtime")
        end
      end
    end

    def initialize(table)
      @table = table
    end

    def size
      @table.size
    end

    def add(name, directory, options)
      @table.add(name,
                 :name      => name,
                 :directory => directory,
                 :addtime   => Time.now,
                 :favtime   => options[:fav] ? Time.now : Time.at(0))
    end

    def remove(name)
      @table[name].delete
    end

    def remove_all
      self.each do |r|
        remove(r.name)
      end
    end

    def [](name)
      @table[name]
    end

    def each
      @table.select.each do |r|
        yield r
      end
    end

    def sort(kind, order = "descending")
      Util.groonga_table_sort(@table, [{:key => kind, :order => order}])
    end

    def dump
      self.each do |r|
        p [r.name, r.addtime, r.updatetime, r.viewtime, r.favtime]
      end
    end

    def touch(name, kind, time = Time.now)
      @table[name][kind] = time
    end

    def touch_if(name, kind, time = Time.now)
      record = @table[name]

      if record
        record[kind] = time
      else
        nil
      end
    end

    def favs
      sorted = sort('favtime')
      zero_time = Time.at(0)
      index = sorted.find_index {|v| v.favtime == zero_time}
      unless index.nil?
        sorted[0...index]
      else
        sorted
      end
    end
  end
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
milkode-0.9.7 lib/milkode/database/package_table.rb