Class: Rush::Access

Inherits:
Object
  • Object
show all
Defined in:
lib/rush/access.rb

Overview

A class to hold permissions (read, write, execute) for files and dirs. See Rush::Entry#access= for information on the public-facing interface.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#group_can_executeObject

Returns the value of attribute group_can_execute.



5
6
7
# File 'lib/rush/access.rb', line 5

def group_can_execute
  @group_can_execute
end

#group_can_readObject

Returns the value of attribute group_can_read.



5
6
7
# File 'lib/rush/access.rb', line 5

def group_can_read
  @group_can_read
end

#group_can_writeObject

Returns the value of attribute group_can_write.



5
6
7
# File 'lib/rush/access.rb', line 5

def group_can_write
  @group_can_write
end

#other_can_executeObject

Returns the value of attribute other_can_execute.



6
7
8
# File 'lib/rush/access.rb', line 6

def other_can_execute
  @other_can_execute
end

#other_can_readObject

Returns the value of attribute other_can_read.



6
7
8
# File 'lib/rush/access.rb', line 6

def other_can_read
  @other_can_read
end

#other_can_writeObject

Returns the value of attribute other_can_write.



6
7
8
# File 'lib/rush/access.rb', line 6

def other_can_write
  @other_can_write
end

#user_can_executeObject

Returns the value of attribute user_can_execute.



4
5
6
# File 'lib/rush/access.rb', line 4

def user_can_execute
  @user_can_execute
end

#user_can_readObject

Returns the value of attribute user_can_read.



4
5
6
# File 'lib/rush/access.rb', line 4

def user_can_read
  @user_can_read
end

#user_can_writeObject

Returns the value of attribute user_can_write.



4
5
6
# File 'lib/rush/access.rb', line 4

def user_can_write
  @user_can_write
end

Class Method Details

.from_hash(hash) ⇒ Object



66
67
68
# File 'lib/rush/access.rb', line 66

def self.from_hash(hash)
	new.from_hash(hash)
end

.parse(options) ⇒ Object



27
28
29
# File 'lib/rush/access.rb', line 27

def self.parse(options)
	new.parse(options)
end

.permissionsObject



12
13
14
# File 'lib/rush/access.rb', line 12

def self.permissions
	%w(read write execute)
end

.rolesObject



8
9
10
# File 'lib/rush/access.rb', line 8

def self.roles
	%w(user group other)
end

Instance Method Details

#apply(full_path) ⇒ Object



31
32
33
34
35
# File 'lib/rush/access.rb', line 31

def apply(full_path)
	FileUtils.chmod(octal_permissions, full_path)
rescue Errno::ENOENT
	raise Rush::DoesNotExist, full_path
end

#display_hashObject



48
49
50
51
52
53
54
# File 'lib/rush/access.rb', line 48

def display_hash
	hash = {}
	to_hash.each do |key, value|
		hash[key] = true if value == 1
	end
	hash
end

#extract_list(type, value, choices) ⇒ Object



120
121
122
123
124
125
# File 'lib/rush/access.rb', line 120

def extract_list(type, value, choices)
	list = parts_from(value)
	list.each do |value|
		raise(Rush::BadAccessSpecifier, "Unrecognized #{type}: #{value}") unless choices.include? value
	end
end

#from_hash(hash) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/rush/access.rb', line 56

def from_hash(hash)
	self.class.roles.each do |role|
		self.class.permissions.each do |perm|
			key = "#{role}_can_#{perm}"
			send("#{key}=".to_sym, hash[key.to_sym].to_i == 1 ? true : false)
		end
	end
	self
end

#from_octal(mode) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rush/access.rb', line 87

def from_octal(mode)
	perms = octal_integer_array(mode)

	self.user_can_read = (perms[0] & 4) > 0 ? true : false
	self.user_can_write = (perms[0] & 2) > 0 ? true : false
	self.user_can_execute = (perms[0] & 1) > 0 ? true : false

	self.group_can_read = (perms[1] & 4) > 0 ? true : false
	self.group_can_write = (perms[1] & 2) > 0 ? true : false
	self.group_can_execute = (perms[1] & 1) > 0 ? true : false

	self.other_can_read = (perms[2] & 4) > 0 ? true : false
	self.other_can_write = (perms[2] & 2) > 0 ? true : false
	self.other_can_execute = (perms[2] & 1) > 0 ? true : false

	self
end

#octal_integer_array(mode) ⇒ Object



105
106
107
108
109
# File 'lib/rush/access.rb', line 105

def octal_integer_array(mode)
	mode %= 01000                      # filter out everything but the bottom three digits
	mode = sprintf("%o", mode)         # convert to string
	mode.split("").map { |p| p.to_i }  # and finally, array of integers
end

#octal_permissionsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rush/access.rb', line 70

def octal_permissions
	perms = [ 0, 0, 0 ]
	perms[0] += 4 if user_can_read
	perms[0] += 2 if user_can_write
	perms[0] += 1 if user_can_execute
	
	perms[1] += 4 if group_can_read
	perms[1] += 2 if group_can_write
	perms[1] += 1 if group_can_execute

	perms[2] += 4 if other_can_read
	perms[2] += 2 if other_can_write
	perms[2] += 1 if other_can_execute

	eval("0" + perms.join)
end

#parse(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/rush/access.rb', line 16

def parse(options)
	options.each do |key, value|
		next unless m = key.to_s.match(/(.*)_can$/)
		key = m[1].to_sym
		roles = extract_list('role', key, self.class.roles)
		perms = extract_list('permission', value, self.class.permissions)
		set_matrix(perms, roles)
	end
	self
end

#parts_from(value) ⇒ Object



127
128
129
# File 'lib/rush/access.rb', line 127

def parts_from(value)
	value.to_s.split('_').reject { |r| r == 'and' }
end

#set_matrix(perms, roles) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/rush/access.rb', line 111

def set_matrix(perms, roles)
	perms.each do |perm|
		roles.each do |role|
			meth = "#{role}_can_#{perm}=".to_sym
			send(meth, true)
		end
	end
end

#to_hashObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/rush/access.rb', line 37

def to_hash
	hash = {}
	self.class.roles.each do |role|
		self.class.permissions.each do |perm|
			key = "#{role}_can_#{perm}".to_sym
			hash[key] = send(key) ? 1 : 0
		end
	end
	hash
end