Class: WEBrick::HTTPAuth::Htgroup

Inherits:
Object
  • Object
show all
Defined in:
lib/webrick/httpauth/htgroup.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Htgroup

Returns a new instance of Htgroup.



15
16
17
18
19
20
21
# File 'lib/webrick/httpauth/htgroup.rb', line 15

def initialize(path)
  @path = path
  @mtime = Time.at(0)
  @group = Hash.new
  open(@path,"a").close unless File::exist?(@path)
  reload
end

Instance Method Details

#add(group, members) ⇒ Object



56
57
58
# File 'lib/webrick/httpauth/htgroup.rb', line 56

def add(group, members)
  @group[group] = members(group) | members
end

#flush(output = nil) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/webrick/httpauth/htgroup.rb', line 37

def flush(output=nil)
  output ||= @path
  tmp = Tempfile.new("htgroup", File::dirname(output))
  begin
    @group.keys.sort.each{|group|
      tmp.puts(format("%s: %s", group, self.members(group).join(" ")))
    }
    tmp.close
    File::rename(tmp.path, output)
  rescue
    tmp.close(true)
  end
end

#members(group) ⇒ Object



51
52
53
54
# File 'lib/webrick/httpauth/htgroup.rb', line 51

def members(group)
  reload
  @group[group] || []
end

#reloadObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/webrick/httpauth/htgroup.rb', line 23

def reload
  if (mtime = File::mtime(@path)) > @mtime
    @group.clear
    open(@path){|io|
      while line = io.gets
        line.chomp!
        group, members = line.split(/:\s*/)
        @group[group] = members.split(/\s+/)
      end
    }
    @mtime = mtime
  end
end