Module: GeneralHelper

Defined in:
lib/skel/leaves/scorekeeper/helpers/general.rb

Overview

Utility methods used by Scorekeeper.

Instance Method Summary collapse

Instance Method Details

#authorized?(giver, receiver) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/skel/leaves/scorekeeper/helpers/general.rb', line 51

def authorized?(giver, receiver)
  giver and receiver and giver.authorized? and giver.name != receiver.name
end

#change_points(stem, channel, giver, receiver, delta, note = nil) ⇒ Object



55
56
57
58
59
# File 'lib/skel/leaves/scorekeeper/helpers/general.rb', line 55

def change_points(stem, channel, giver, receiver, delta, note=nil)
  return if delta.zero?
  chan = Channel.find_or_create :server => server_identifier(stem), :name => channel
  chan.scores.create :giver => giver, :receiver => receiver, :change => delta, :note => note
end

#find_in_channel(stem, channel, victim) ⇒ Object



40
41
42
43
44
45
# File 'lib/skel/leaves/scorekeeper/helpers/general.rb', line 40

def find_in_channel(stem, channel, victim)
  stem.channel_members[channel].each do |name, privilege|
    return normalize(name, false) if normalize(name) == normalize(victim)
  end
  return victim
end

#find_person(stem, nick) ⇒ Object



33
34
35
36
37
38
# File 'lib/skel/leaves/scorekeeper/helpers/general.rb', line 33

def find_person(stem, nick)
  Person.all(:server => server_identifier(stem)).each do |person|
    return person if person.name.downcase == normalize(nick) or person.pseudonyms.collect { |pn| pn.name.downcase }.include? normalize(nick)
  end
  return nil
end

#find_range(date) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/skel/leaves/scorekeeper/helpers/general.rb', line 17

def find_range(date)
  start = nil
  stop = nil
  if date.kind_of? Range then
    start = date.first
    stop = date.last
  elsif date.kind_of? Time then
    start = date.to_date
    stop = date.to_date + 1
  else
    start = date
    stop = date + 1
  end
  return start, stop
end

#normalize(nick, dc = true) ⇒ Object



47
48
49
# File 'lib/skel/leaves/scorekeeper/helpers/general.rb', line 47

def normalize(nick, dc=true)
  dc ? nick.downcase.split(/\|/)[0] : nick.split(/\|/)[0]
end

#parse_date(str) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/skel/leaves/scorekeeper/helpers/general.rb', line 4

def parse_date(str)
  date = nil
  begin
    date = Chronic.parse(str, :context => :past, :guess => false)
  rescue NameError
    begin
      date = Date.parse(str)
    rescue ArgumentError
    end
  end
  return date
end

#server_identifier(stem) ⇒ Object



61
62
63
# File 'lib/skel/leaves/scorekeeper/helpers/general.rb', line 61

def server_identifier(stem)
  "#{stem.server}:#{stem.port}"
end