Class: OsCtld::Devices::Mode
- Inherits:
-
Object
- Object
- OsCtld::Devices::Mode
- Defined in:
- lib/osctld/devices/mode.rb
Instance Attribute Summary collapse
- #mode ⇒ Array<String> readonly
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #clone ⇒ Object
-
#compatible?(other) ⇒ Boolean
Return ‘true` if self is a superset of `other` or equal to `other`.
-
#complement(other) ⇒ Object
Expand access mode with modes from ‘other` that are missing in self.
-
#diff(target) ⇒ Hash<Symbol, String>
Generate diff to reach the ‘target` mode from the current mode.
-
#initialize(str) ⇒ Mode
constructor
A new instance of Mode.
- #to_s ⇒ Object
Constructor Details
#initialize(str) ⇒ Mode
Returns a new instance of Mode.
16 17 18 |
# File 'lib/osctld/devices/mode.rb', line 16 def initialize(str) @mode = self.class.normalize(str) end |
Instance Attribute Details
#mode ⇒ Array<String> (readonly)
13 14 15 |
# File 'lib/osctld/devices/mode.rb', line 13 def mode @mode end |
Class Method Details
.normalize(str) ⇒ Array<String>
5 6 7 8 9 10 |
# File 'lib/osctld/devices/mode.rb', line 5 def self.normalize(str) mode = str.chars mode.sort! mode.uniq! mode end |
Instance Method Details
#==(other) ⇒ Object
74 75 76 |
# File 'lib/osctld/devices/mode.rb', line 74 def ==(other) other.mode == mode end |
#clone ⇒ Object
70 71 72 |
# File 'lib/osctld/devices/mode.rb', line 70 def clone self.class.new(to_s) end |
#compatible?(other) ⇒ Boolean
Return ‘true` if self is a superset of `other` or equal to `other`
22 23 24 25 26 27 28 29 30 |
# File 'lib/osctld/devices/mode.rb', line 22 def compatible?(other) return true if other.mode == mode other.mode.each do |m| return false unless mode.include?(m) end true end |
#complement(other) ⇒ Object
Expand access mode with modes from ‘other` that are missing in self
34 35 36 37 |
# File 'lib/osctld/devices/mode.rb', line 34 def complement(other) @mode = (@mode + other.mode).sort! @mode.uniq! end |
#diff(target) ⇒ Hash<Symbol, String>
Generate diff to reach the ‘target` mode from the current mode
The return value is a hash describing actions that need to be taken to update cgroups. The hash has two keys: ‘:allow` and `:deny`, each pointing to a mode that has to be allowed/denied.
For example, for transition from ‘rm` to `wm`, the return value would be:
{allow: 'w', deny: 'r'}
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/osctld/devices/mode.rb', line 51 def diff(target) ret = { allow: [], deny: [] } %w[r w m].each do |m| if target.mode.include?(m) && !mode.include?(m) ret[:allow] << m elsif !target.mode.include?(m) && mode.include?(m) ret[:deny] << m end end ret.transform_values(&:join) end |
#to_s ⇒ Object
66 67 68 |
# File 'lib/osctld/devices/mode.rb', line 66 def to_s %w[r w m].select { |m| mode.include?(m) }.join end |