Class: Authorize::Bitmask
- Inherits:
-
Set
- Object
- Set
- Authorize::Bitmask
- Includes:
- Comparable
- Defined in:
- lib/authorize/bitmask.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.name_values ⇒ Object
Returns the value of attribute name_values.
Class Method Summary collapse
-
.enum(mask) ⇒ Object
Enumerates all operations included in the given mask.
-
.max ⇒ Object
The maximum value this bitmask can hold (in which every named bit is set).
- .new(fixnum_or_enum = Set.new) ⇒ Object
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Comparability derives from integer representation.
- #add(el) ⇒ Object (also: #<<)
-
#complete ⇒ Object
Return an equivalent Bitmask using all possible names (fundamental and aggregate).
-
#fundamental ⇒ Object
Return an equivalent Bitmask using only fundamental names, never aggregate names.
-
#minimal ⇒ Object
Return an equivalent Bitmask using aggregated names to replace fundamental names where possible.
- #to_canonical_array ⇒ Object
-
#to_i ⇒ Object
(also: #to_int)
Calculate the integer value for the mask.
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Class Attribute Details
.name_values ⇒ Object
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
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 |
.max ⇒ Object
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: <<
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 |
#complete ⇒ Object
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 |
#fundamental ⇒ Object
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 |
#minimal ⇒ Object
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_array ⇒ Object
81 82 83 |
# File 'lib/authorize/bitmask.rb', line 81 def to_canonical_array sort_by{|name| self.class.name_values[name]} end |
#to_i ⇒ Object 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_s ⇒ Object
77 78 79 |
# File 'lib/authorize/bitmask.rb', line 77 def to_s to_canonical_array.join("|") end |
#valid? ⇒ Boolean
47 48 49 |
# File 'lib/authorize/bitmask.rb', line 47 def valid? inject(true) {|memo, n| memo && !!self.class.name_values[n]} end |