Class: Zabbirc::OpList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/zabbirc/op_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(ops = nil) ⇒ OpList



6
7
8
9
10
11
12
13
# File 'lib/zabbirc/op_list.rb', line 6

def initialize ops=nil
  @ops = {}
  if ops
    ops.each do |op|
      add op
    end
  end
end

Instance Method Details

#add(op) ⇒ Object



25
26
27
28
29
30
# File 'lib/zabbirc/op_list.rb', line 25

def add op
  if exists? op.
    return get(op.)
  end
  @ops[op.] = op
end

#authenticate(name) ⇒ Object Also known as: exists?



15
16
17
# File 'lib/zabbirc/op_list.rb', line 15

def authenticate name
  @ops.key? name
end

#dump_settingsObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zabbirc/op_list.rb', line 64

def dump_settings
  dump = {}
  each do |op|
    dump[op.] = op.setting.to_hash
  end

  file = File.open(STORED_SETTINGS_FILE, "w")
  file.puts dump.to_yaml
  file.close
  true
end

#each(&block) ⇒ Object



43
44
45
# File 'lib/zabbirc/op_list.rb', line 43

def each &block
  @ops.values.each &block
end

#get(name) ⇒ Object



21
22
23
# File 'lib/zabbirc/op_list.rb', line 21

def get name
  @ops[name]
end

#interested_in(event) ⇒ Object



47
48
49
# File 'lib/zabbirc/op_list.rb', line 47

def interested_in event
  self.class.new(find_all{ |op| op.interested_in? event })
end

#load_settingsObject



76
77
78
79
80
81
82
83
84
# File 'lib/zabbirc/op_list.rb', line 76

def load_settings
  return unless File.exists?(STORED_SETTINGS_FILE)
  stored_settings = YAML.load_file(STORED_SETTINGS_FILE)
  stored_settings.each do |, setting|
    op = get()
    next unless op
    op.setting.restore setting
  end
end

#notify(event) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/zabbirc/op_list.rb', line 51

def notify event
  group_by(&:primary_channel).each do |channel, ops|
    begin
      next if channel.nil?
      op_targets = ops.collect{|op| "#{op.nick}:" }.join(" ")
      channel.send "#{op_targets} #{event.label}"
      ops.each{ |op| op.event_notified event }
    rescue =>e
      binding.pry
    end
  end
end

#remove(op) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/zabbirc/op_list.rb', line 32

def remove op
  case op
  when String
    @ops[op] = nil
  when Op
    @ops[op.] = nil
  else
    raise ArgumentError, "Stirng or Op expected"
  end
end