-------------------------------------------------------------------------------- -- Title: LUPTimeline.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies require( "LUObject" ) require( "LUDate" ) require( "LUList" ) require( "LFSFile" ) require( "LUPNote" ) -- extend the super class local thisClass, superClass = LUObject.extend( LUObject ) -- factory class method, this is where all the instance methods are implemented as closures thisClass.new = function( anUser ) -- extend the super class instance local this = superClass.extend( superClass.new() ) -- instance variables local ivars = { user = anUser } -- instance method this.user = function() return ivars.user end -- private instance method local yearFilter = function( aDirectory, aName ) if ( string.len( aName ) == 4 ) and ( tonumber( aName ) ~= nil ) then return true end return false end -- instance method this.years = function() local aDirectory = this.user().directory() local someNames = aDirectory.list( yearFilter ) if ( someNames ~= nil ) then local aList = LUList.new() local count = someNames.size() for index = 1, count do local aName = someNames.get( index ) local aYear = tonumber( aName ) local aDate = LUDate.dateWithValues( { year = aYear, month = 1, day = 1 } ) aList.add( aDate ) end return aList end return nil end -- private instance method local monthFilter = function( aDirectory, aName ) if ( string.len( aName ) == 2 ) then local aNumber = tonumber( aName ) if ( aNumber ~= nil ) and ( aNumber >= 1 ) and ( aNumber <= 12 ) then return true end end return false end -- instance method this.monthsWithDate = function( aDate ) local aSeparator = LFSFile.separator() local aPath = this.user().directory().path() .. aSeparator .. aDate.toString( "%Y" ) local aDirectory = LFSFile.new( aPath ) local someNames = aDirectory.list( monthFilter ) if ( someNames ~= nil ) then local aList = LUList.new() local aYear = aDate.year() local count = someNames.size() for index = 1, count do local aName = someNames.get( index ) local aMonth = tonumber( aName ) local aDate = LUDate.dateWithValues( { year = aYear, month = aMonth, day = 1 } ) if ( aDate.year() == aYear ) then aList.add( aDate ) end end return aList end return nil end -- private instance method local dayFilter = function( aDirectory, aName ) if ( string.len( aName ) == 2 ) then local aNumber = tonumber( aName ) if ( aNumber ~= nil ) and ( aNumber >= 1 ) and ( aNumber <= 31 ) then return true end end return false end -- instance method this.daysWithDate = function( aDate ) local aSeparator = LFSFile.separator() local aPath = this.user().directory().path() .. aSeparator .. aDate.toString( "%Y" .. aSeparator .. "%m" ) local aDirectory = LFSFile.new( aPath ) local someNames = aDirectory.list( dayFilter ) if ( someNames ~= nil ) then local aList = LUList.new() local aYear = aDate.year() local aMonth = aDate.month() local count = someNames.size() for index = 1, count do local aName = someNames.get( index ) local aDay = tonumber( aName ) local aDate = LUDate.dateWithValues( { year = aYear, month = aMonth, day = aDay } ) if ( aDate.month() == aMonth ) then aList.add( aDate ) end end return aList end return nil end -- private instance method local noteFilter = function( aDirectory, aName ) if ( string.len( aName ) == 12 ) then local aSuffix = ".txt" if ( aSuffix == string.sub( aName, -string.len( aSuffix ) ) ) then return true end end return false end -- instance method this.notesWithDate = function( aDate ) local aSeparator = LFSFile.separator() local aPath = this.user().directory().path() .. aSeparator .. aDate.toString( "%Y" .. aSeparator .. "%m" .. aSeparator .. "%d" ) local aDirectory = LFSFile.new( aPath ) local someNames = aDirectory.list( noteFilter ) if ( someNames ~= nil ) then local aList = LUList.new() local aYear = aDate.year() local aMonth = aDate.month() local aDay = aDate.day() local count = someNames.size() for index = 1, count do local aName = someNames.get( index ) local aTime = string.sub( aName, 1, -string.len( ".txt" ) - 1 ) local _, _, anHour, aMinute, aSecond = string.find( aTime, "(%d+)%-(%d+)-(%d+)" ) if ( ( anHour ~= nil ) and ( aMinute ~= nil ) and ( aSecond ~= nil ) ) then anHour = tonumber( anHour ) aMinute = tonumber( aMinute ) aSecond = tonumber( aSecond ) if ( ( anHour ~= nil ) and ( anHour >= 0 ) and ( anHour <= 23 ) ) then if ( ( aMinute ~= nil ) and ( aMinute >= 0 ) and ( aMinute <= 59 ) ) then if ( ( aSecond ~= nil ) and ( aSecond >= 0 ) and ( aSecond <= 59 ) ) then local someValues = { year = aYear, month = aMonth, day = aDay, hour = anHour, min = aMinute, sec = aSecond } local aDate = LUDate.dateWithValues( someValues ) local aNote = LUPNote.noteWithDate( this.user(), aDate ) if ( aNote ~= nil ) then aList.add( aNote ) end end end end end end return aList end end -- return the public instance methods return superClass.inherit( this ) end -- define the class name LUPTimeline = thisClass return LUPTimeline