Class: Handle::Permissions
- Inherits:
-
Object
- Object
- Handle::Permissions
show all
- Defined in:
- lib/handle/permissions.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Permissions.
5
6
7
8
9
10
11
12
|
# File 'lib/handle/permissions.rb', line 5
def initialize(*flags)
if flags.last.is_a?(Fixnum)
@bitmask = flags.pop
else
@bitmask = 0
end
@flags = Hash[flags.reverse.collect.with_index { |f,i| [f,2**i] }.reverse]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/handle/permissions.rb', line 18
def method_missing(sym, *args)
flag = sym.to_s.sub(/([\?\=])$/,'').to_sym
if @flags.include?(flag)
case $1
when '?' then read(flag)
when '=' then write(flag,args.first)
else super(sym, *args)
end
else
super(sym, *args)
end
end
|
Instance Attribute Details
#bitmask ⇒ Object
Returns the value of attribute bitmask.
3
4
5
|
# File 'lib/handle/permissions.rb', line 3
def bitmask
@bitmask
end
|
Instance Method Details
#inspect ⇒ Object
56
57
58
59
|
# File 'lib/handle/permissions.rb', line 56
def inspect
str = @flags.keys.select { |flag| self.read(flag) }.collect { |flag| flag.inspect }.join(', ')
"[#{str}]"
end
|
#max ⇒ Object
39
40
41
|
# File 'lib/handle/permissions.rb', line 39
def max
2**@flags.length-1
end
|
#read(flag) ⇒ Object
43
44
45
|
# File 'lib/handle/permissions.rb', line 43
def read(flag)
self.bitmask & @flags[flag] > 0
end
|
#to_bool ⇒ Object
31
32
33
|
# File 'lib/handle/permissions.rb', line 31
def to_bool
@flags.keys.collect { |flag| read(flag) }
end
|
#to_s ⇒ Object
35
36
37
|
# File 'lib/handle/permissions.rb', line 35
def to_s
"%#{@flags.length}.#{@flags.length}b" % bitmask
end
|
#write(flag, value) ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/handle/permissions.rb', line 47
def write(flag, value)
mask = @flags[flag]
if value
self.bitmask |= mask
else
self.bitmask &= (max - mask)
end
end
|