Class: Authorize::Bitmask

Inherits:
Set
  • Object
show all
Includes:
Comparable
Defined in:
lib/authorize/bitmask.rb

Direct Known Subclasses

Permission::Mask

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.name_valuesObject

Returns the value of attribute name_values.



7
8
9
# File 'lib/authorize/bitmask.rb', line 7

def name_values
  @name_values
end

Class Method Details

.enum(mask) ⇒ Object

Enumerates all operations included in the given mask

Raises:

  • (RangeError)


29
30
31
32
# File 'lib/authorize/bitmask.rb', line 29

def enum(mask)
  raise RangeError, "Unnamed bits in mask (#{mask.to_s(2)})" unless (mask | max) == max
  name_values.inject(Set[]){|s, (p, v)| s << p if (v == (mask & v)); s }
end

.maxObject

The maximum value this bitmask can hold (in which every named bit is set).



24
25
26
# File 'lib/authorize/bitmask.rb', line 24

def max
  name_values.values.inject{|memo, v| memo | v}
end

.new(fixnum_or_enum = Set.new) ⇒ Object



8
9
10
11
# File 'lib/authorize/bitmask.rb', line 8

def new(fixnum_or_enum = Set.new)
  enum = fixnum_or_enum.kind_of?(Fixnum) ? enum(fixnum_or_enum) : fixnum_or_enum
  super(enum)
end

Instance Method Details

#<=>(other) ⇒ Object

Comparability derives from integer representation



73
74
75
# File 'lib/authorize/bitmask.rb', line 73

def <=>(other)
  to_int <=> other.to_int
end

#add(el) ⇒ Object Also known as: <<

Raises:

  • (ArgumentError)


35
36
37
38
# File 'lib/authorize/bitmask.rb', line 35

def add(el)
  raise ArgumentError, "Unrecognized bit name (#{el})" unless self.class.name_values.keys.include?(el)
  super
end

#completeObject

Return an equivalent Bitmask using all possible names (fundamental and aggregate)



68
69
70
# File 'lib/authorize/bitmask.rb', line 68

def complete
  self.class.new(to_int)
end

#fundamentalObject

Return an equivalent Bitmask using only fundamental names, never aggregate names



52
53
54
55
56
57
# File 'lib/authorize/bitmask.rb', line 52

def fundamental
  complete.to_canonical_array.inject(self.class.new) do |memo, n|
    memo << n unless (memo.to_i & self.class.name_values[n]) == self.class.name_values[n]
    memo
  end
end

#minimalObject

Return an equivalent Bitmask using aggregated names to replace fundamental names where possible



60
61
62
63
64
65
# File 'lib/authorize/bitmask.rb', line 60

def minimal
  complete.to_canonical_array.reverse.inject(self.class.new) do |memo, n|
    memo << n unless (memo.to_i & self.class.name_values[n]) == self.class.name_values[n]
    memo
  end
end

#to_canonical_arrayObject



81
82
83
# File 'lib/authorize/bitmask.rb', line 81

def to_canonical_array
  sort_by{|name| self.class.name_values[name]}
end

#to_iObject Also known as: to_int

Calculate the integer value for the mask



42
43
44
# File 'lib/authorize/bitmask.rb', line 42

def to_i
  inject(0) {|memo, n| memo | self.class.name_values[n]}
end

#to_sObject



77
78
79
# File 'lib/authorize/bitmask.rb', line 77

def to_s
  to_canonical_array.join("|")
end

#valid?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/authorize/bitmask.rb', line 47

def valid?
  inject(true) {|memo, n| memo && !!self.class.name_values[n]}
end