class GodObject::FilePermissions::Mode

Represents one component of the normal file mode in POSIX environments.

The Mode is basically an immutable bit set consisting of the digits :read, :write and :execute.

Constants

BIT_SET_CONFIGURATION

Configuration for the GodObject:::BitSet object which is used to handle the state internally.

PATTERN

Regular expression for parsing a Mode from a String representation.

Public Class Methods

new(*mode_components) click to toggle source

Initializes a new Mode

@return [void]

@overload initialize(numeric)

@param [Integer] numeric a numeric representation

@overload initialize(enabled_digits)

@param [Array<:read, :write, :execute>] enabled_digits a list of
  enabled digits
# File lib/god_object/file_permissions/mode.rb, line 93
def initialize(*mode_components)
  @bit_set = BIT_SET_CONFIGURATION.new(*mode_components)
end
parse(string) click to toggle source

Creates a new Mode object by parsing a String representation.

@param [String] string a String containing a mode @return [GodObject::FilePermissions::Mode] a new Mode object

# File lib/god_object/file_permissions/mode.rb, line 56
def parse(string)
  result = string.match(PATTERN)
  
  case 
  when !result
    raise ParserError, 'Invalid format'
  when result[:octal_mode] 
    new(result[:octal_mode].to_i)
  else
    mode_components = []

    result[:digit_mode].scan(/r|w|x/).each do |digit|
      mode_components << :read    if digit == 'r'
      mode_components << :write   if digit == 'w'
      mode_components << :execute if digit == 'x'
    end
    
    if mode_components.uniq!
      raise ParserError,
        "Duplicate digit in: #{string.inspect}"
    end

    new(mode_components)
  end
end