lib/ezmlm/list/author.rb
author Mahlon E. Smith <mahlon@laika.com>
Fri, 12 May 2017 11:09:36 -0700
changeset 15 a38e6916504c
child 17 23c7f5c8ee39
permissions -rw-r--r--
Everything is workin! - Add a corpus of test messages to the spec data, with the script that generated them. - Add native objects for Authors, Messages, and Threads. - Tests!
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     1
#!/usr/bin/ruby
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     2
# vim: set nosta noet ts=4 sw=4:
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     3
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     4
# A collection of messages authored from a unique user.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     5
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     6
# Note that Ezmlm uses the "real name" part of an address
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     7
# to identify an author.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     8
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
     9
#    author = Ezmlm::List::Author.new( list, 'acgcbmbmeapgpfckcdol' )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    10
#    author.name            #=> "Help - navigate on interface?"
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    11
#    author.first.date.to_s #=> "2017-05-07T14:55:05-07:00"
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    12
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    13
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    14
# == Version
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    15
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    16
#  $Id$
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    17
#
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    18
#---
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    19
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    20
require 'pathname'
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    21
require 'ezmlm' unless defined?( Ezmlm )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    22
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    23
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    24
### A collection of messages for a specific author.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    25
###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    26
class Ezmlm::List::Author
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    27
	include Enumerable
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    28
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    29
	### Instantiate a new list of messages given
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    30
	### a +list+ and a +author_id+.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    31
	###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    32
	def initialize( list, author_id )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    33
		raise ArgumentError, "Unknown list object." unless list.respond_to?( :listdir )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    34
		raise ArgumentError, "Malformed Author ID." unless author_id =~ /^\w{20}$/
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    35
		raise "Thread indexing is not enabled." unless list.threaded?
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    36
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    37
		@list     = list
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    38
		@id       = author_id
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    39
		@messages = nil
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    40
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    41
		self.load_index
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    42
	end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    43
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    44
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    45
	# The list object this message is stored in.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    46
	attr_reader :list
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    47
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    48
	# The author's identifier.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    49
	attr_reader :id
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    50
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    51
	# The author's name.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    52
	attr_reader :name
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    53
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    54
	# An array of messages this author has sent.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    55
	attr_reader :messages
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    56
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    57
	# An array of threads this author has participated in.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    58
	attr_reader :threads
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    59
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    60
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    61
	### Enumerable API:  Lazy load each message ID as a
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    62
	### Ezmlm::List::Message, yielding it to the block.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    63
	###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    64
	def each
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    65
		self.load_index # refresh for any updates since object was created
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    66
		self.messages.each do |id|
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    67
			yield Ezmlm::List::Message.new( self.list, id )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    68
		end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    69
	end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    70
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    71
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    72
	### Lazy load each thread ID as a Ezmlm::List::Thread, yielding it to the block.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    73
	###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    74
	def each_thread
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    75
		self.load_index # refresh for any updates since object was created
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    76
		self.threads.each do |id|
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    77
			yield Ezmlm::List::Thread.new( self.list, id )
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    78
		end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    79
	end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    80
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    81
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    82
	#########
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    83
	protected
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    84
	#########
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    85
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    86
	### Parse the author index into an array of Messages.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    87
	###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    88
	def load_index
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    89
		@messages = []
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    90
		@threads  = []
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    91
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    92
		path = self.author_path
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    93
		raise "Unknown author: %p" % [ self.id ] unless path.exist?
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    94
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    95
		path.each_line.with_index do |line, i|
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    96
			if i.zero?
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    97
				@name = line.match( /^\w+ (.+)/ )[1]
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    98
			else
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
    99
				match = line.match( /^(\d+):\d+:(\w+) / ) or next
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   100
				self.messages << match[1].to_i
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   101
				self.threads  << match[2]
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   102
			end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   103
		end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   104
	end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   105
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   106
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   107
	### Return the path on disk for the author index.
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   108
	###
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   109
	def author_path
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   110
		prefix = self.id[ 0 ..  1 ]
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   111
		hash   = self.id[ 2 .. -1 ]
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   112
		return self.list.listdir + 'archive' + 'authors' + prefix + hash
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   113
	end
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   114
a38e6916504c Everything is workin!
Mahlon E. Smith <mahlon@laika.com>
parents:
diff changeset
   115
end # class Ezmlm::List::Author