Class: Kanrisuru::Mode

Inherits:
Object
  • Object
show all
Defined in:
lib/kanrisuru/mode.rb,
lib/kanrisuru/mode/permission.rb

Defined Under Namespace

Classes: Permission

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode) ⇒ Mode

Returns a new instance of Mode.



9
10
11
12
13
14
15
16
17
# File 'lib/kanrisuru/mode.rb', line 9

def initialize(mode)
  if mode.instance_of?(Integer)
    init_mode(mode.to_s(8))
  elsif mode.instance_of?(String)
    init_mode(mode)
  else
    raise 'Invalid mode type'
  end
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



7
8
9
# File 'lib/kanrisuru/mode.rb', line 7

def group
  @group
end

#otherObject (readonly)

Returns the value of attribute other.



7
8
9
# File 'lib/kanrisuru/mode.rb', line 7

def other
  @other
end

#ownerObject (readonly)

Returns the value of attribute owner.



7
8
9
# File 'lib/kanrisuru/mode.rb', line 7

def owner
  @owner
end

Instance Method Details

#directory?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/kanrisuru/mode.rb', line 19

def directory?
  @directory == 'd'
end

#inspectObject



83
84
85
86
# File 'lib/kanrisuru/mode.rb', line 83

def inspect
  format('#<Kanrisuru::Mode:0x%<object_id>s @numeric=%<numeric>s @symbolic=%<symbolic>s>',
         object_id: object_id, numeric: numeric, symbolic: symbolic)
end

#numericObject



64
65
66
# File 'lib/kanrisuru/mode.rb', line 64

def numeric
  @owner.numeric + @group.numeric + @other.numeric
end

#numeric=(numeric) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kanrisuru/mode.rb', line 68

def numeric=(numeric)
  string =
    if numeric.instance_of?(Integer)
      numeric.to_s(8)
    else
      numeric
    end

  tokens = string.chars

  @owner.numeric = tokens[0]
  @group.numeric = tokens[1]
  @other.numeric = tokens[2]
end

#symbolicObject



23
24
25
# File 'lib/kanrisuru/mode.rb', line 23

def symbolic
  @directory + @owner.symbolic + @group.symbolic + @other.symbolic
end

#symbolic=(string) ⇒ Object

Raises:

  • (ArgumentErorr)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kanrisuru/mode.rb', line 27

def symbolic=(string)
  raise ArgumentErorr, 'Invalid symbolic type' if string.class != String

  if string.length == 10 && !string.include?(',')
    modes = string[1..-1]

    @owner.symbolic = modes[0..2]
    @group.symbolic = modes[3..5]
    @other.symbolic = modes[6..8]
  else
    operations = string.split(',')
    operations.each do |operation|
      flags = operation.split(/[=+-]/)

      commands = flags[0] == '' ? 'a' : flags[0]
      fields = flags[1]

      commands = commands.chars

      commands.each do |command|
        case command
        when 'a'
          apply_operation(operation, fields, @owner)
          apply_operation(operation, fields, @group)
          apply_operation(operation, fields, @other)
        when 'u'
          apply_operation(operation, fields, @owner)
        when 'g'
          apply_operation(operation, fields, @group)
        when 'o'
          apply_operation(operation, fields, @other)
        end
      end
    end
  end
end

#to_iObject



92
93
94
# File 'lib/kanrisuru/mode.rb', line 92

def to_i
  numeric.to_i(8)
end

#to_sObject



88
89
90
# File 'lib/kanrisuru/mode.rb', line 88

def to_s
  symbolic
end