Class: GodObject::FilePermissions::Mode

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, GodObject::FilePermissions::ModeMixin::ClassMethods
Includes:
BitSet, Comparable, ModeMixin
Defined in:
lib/god_object/file_permissions/mode.rb

Overview

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.

Constant Summary collapse

PATTERN =

Regular expression for parsing a Mode from a String representation.

/^#{blank}(?:#{digit_mode}|#{octal_mode})#{blank}$/
BIT_SET_CONFIGURATION =

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

Configuration.new(
  read:    'r',
  write:   'w',
  execute: 'x'
)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(numeric) ⇒ void #initialize(enabled_digits) ⇒ void

Initializes a new Mode

Overloads:

  • #initialize(numeric) ⇒ void

    Parameters:

    • numeric (Integer)

      a numeric representation

  • #initialize(enabled_digits) ⇒ void

    Parameters:

    • enabled_digits (Array<:read, :write, :execute>)

      a list of enabled digits



93
94
95
# File 'lib/god_object/file_permissions/mode.rb', line 93

def initialize(*mode_components)
  @bit_set = BIT_SET_CONFIGURATION.new(*mode_components)
end

Instance Attribute Details

#disabled_digitsSet<:read, :write, :execute> (readonly)

Returns a list of all digits which are disabled.

Returns:

  • (Set<:read, :write, :execute>)

    a list of all digits which are disabled



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

#enabled_digitsSet<:read, :write, :execute> (readonly)

Returns a list of all digits which are enabled.

Returns:

  • (Set<:read, :write, :execute>)

    a list of all digits which are enabled



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

#execute?true, false (readonly) Also known as: execute

Returns the current state of the execute digit.

Returns:

  • (true, false)

    the current state of the execute digit



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

#read?true, false (readonly) Also known as: read

Returns the current state of the read digit.

Returns:

  • (true, false)

    the current state of the read digit



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

#state{(:read, :write, :execute) => true, false} (readonly) Also known as: attributes

Returns a table of all digits and their states.

Returns:

  • ({(:read, :write, :execute) => true, false})

    a table of all digits and their states



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

#write?true, false (readonly) Also known as: write

Returns the current state of the write digit.

Returns:

  • (true, false)

    the current state of the write digit



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

Class Method Details

#build(mode) ⇒ GodObject::FilePermissions::ModeMixin #build(string) ⇒ GodObject::FilePermissions::ModeMixin #build(numeric) ⇒ GodObject::FilePermissions::ModeMixin #build(enabled_digits) ⇒ GodObject::FilePermissions::ModeMixin Originally defined in module GodObject::FilePermissions::ModeMixin::ClassMethods

Either passes through or generates a new Mode object

Overloads:

Returns:

.parse(string) ⇒ GodObject::FilePermissions::Mode

Creates a new Mode object by parsing a String representation.

Parameters:

  • string (String)

    a String containing a mode

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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

Instance Method Details

#+(other) ⇒ GodObject::FilePermissions::ModeMixin Originally defined in module ModeMixin

Returns a new Mode with the enabled digits of the current and other.

Parameters:

Returns:

#<=>(other) ⇒ -1, ... Originally defined in module ModeMixin

Note:

Only other Modes or Integer-likes are considered comparable.

Compares the Mode to another to determine its relative position.

Relative position is defined by comparing the Integer representation.

Parameters:

  • other (Object)

    other Object to be compared object

Returns:

  • (-1, 0, 1, nil)

    -1 if other is greater, 0 if other is equal and 1 if other is lesser than self, nil if comparison is impossible

#[](digit) ⇒ true, false

Returns current state of the given digit.

Parameters:

  • digit (:read, :write, :execute, Integer)

    name or index of a digit

Returns:

  • (true, false)

    current state of the given digit



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

#difference(other) ⇒ GodObject::FilePermissions::ModeMixin Also known as: - Originally defined in module ModeMixin

Returns a new Mode with the enabled digits of the current without the enabled digits of other.

Parameters:

Returns:

#eql?(other) ⇒ true, false Originally defined in module ModeMixin

Answers if another object is equal and of the same type family.

Parameters:

  • other (Object)

    an object to be checked for equality

Returns:

  • (true, false)

    true if the object is considered equal and of the same type family, false otherwise

See Also:

#hashsee Object#hash

Returns identity hash for hash table usage.

Returns:

  • (see Object#hash)

    identity hash for hash table usage



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

#inspectString Originally defined in module ModeMixin

Represents a Mode as String for debugging.

Returns:

  • (String)

    a String representation for debugging

#intersection(other) ⇒ GodObject::FilePermissions::ModeMixin Also known as: & Originally defined in module ModeMixin

Returns a new Mode with only those digits enabled which are enabled in both the current and other.

Parameters:

Returns:

#invertGodObject::FilePermissions::ModeMixin Originally defined in module ModeMixin

Returns a new Mode with all digit states inverted.

Returns:

#symmetric_difference(other) ⇒ GodObject::FilePermissions::ModeMixin Also known as: ^ Originally defined in module ModeMixin

Returns a new Mode with the enabled digits which are enabled in only one of current and other.

Parameters:

Returns:

#to_iInteger

Represents a Mode as a binary Integer.

Returns:

  • (Integer)

    an Integer representation



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

#to_s(format) ⇒ String

Represents a Mode as String.

Parameters:

  • format (:long, :short)

    the String format

Returns:

  • (String)

    a String representation



148
149
150
151
# File 'lib/god_object/file_permissions/mode.rb', line 148

def_delegators :@bit_set,
:attributes, :state, :[], :enabled_digits, :disabled_digits,
:read?, :read, :write?, :write, :execute?, :execute,
:to_i, :to_s, :hash

#union(other) ⇒ GodObject::FilePermissions::ModeMixin Also known as: | Originally defined in module ModeMixin

Returns a new Mode with the enabled digits of the current and other.

Parameters:

Returns: