packages feed

bamboo-2009.5.22: src/Bamboo/Model/Counter.hs

{-# LANGUAGE NoImplicitPrelude #-}

module Bamboo.Model.Counter where

-- env
import Bamboo.Helper.Env 
import qualified Bamboo.Config.Global as G
import qualified System.IO as IO

hit x = create_stat_if_none x >> x.inc_stat

count_name = (/ G.count_meta)
count_path = (G.stat_uri / ) > count_name

default_count = 1
create_stat x = mkdir_p (x.take_directory) >> default_count.show.write_file x

create_stat_if_none x = do
  let i = x.count_path
  has_stat <- i.file_exist
  if has_stat
    then return ()
    else i.create_stat

safe_read_line x = with_file x IO.ReadMode IO.hGetLine 

inc_stat x = do
  let i = x.count_path
  c <- i.safe_read_line
  c.read.(+1).show.write_file i

read_stat x = do
  create_stat_if_none x
  safe_read_line (x.count_path) ^ read