Class: Fib::PermissionsCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/fib/permissions_collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePermissionsCollection

Returns a new instance of PermissionsCollection.



11
12
13
14
# File 'lib/fib/permissions_collection.rb', line 11

def initialize
  @permissions = {}
  @package = Fib::ElementPackage.new
end

Instance Attribute Details

#containerObject

Returns the value of attribute container.



6
7
8
# File 'lib/fib/permissions_collection.rb', line 6

def container
  @container
end

#packageObject (readonly)

Returns the value of attribute package.



5
6
7
# File 'lib/fib/permissions_collection.rb', line 5

def package
  @package
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



5
6
7
# File 'lib/fib/permissions_collection.rb', line 5

def permissions
  @permissions
end

Class Method Details

.build_by_permissions(permissions) ⇒ Object



129
130
131
132
# File 'lib/fib/permissions_collection.rb', line 129

def build_by_permissions permissions
  return unless permissions.is_a? Array
  new.tap { |p| p.append(*permissions) }
end

Instance Method Details

#&(permission_collection) ⇒ Object



72
73
74
75
76
77
# File 'lib/fib/permissions_collection.rb', line 72

def & permission_collection
  raise ParameterIsNotValid, "must be permission_collection" unless permission_collection.is_a?(Fib::PermissionsCollection)

  current_permission_values = permissions.values
  build_new { append *(current_permission_values & permission_collection.permissions.values).flatten }
end

#+(permission_collection) ⇒ Object Also known as: |



56
57
58
59
60
61
# File 'lib/fib/permissions_collection.rb', line 56

def + permission_collection
  raise ParameterIsNotValid, "must be permission_collection" unless permission_collection.is_a?(Fib::PermissionsCollection)

  current_permission_values = permissions.values
  build_new { append *(current_permission_values | permission_collection.permissions.values).flatten }
end

#-(permission_collection) ⇒ Object



65
66
67
68
69
70
# File 'lib/fib/permissions_collection.rb', line 65

def - permission_collection
  raise ParameterIsNotValid, "must be permission_collection" unless permission_collection.is_a?(Fib::PermissionsCollection)

  current_permission_values = permissions.values
  build_new { append *(current_permission_values - permission_collection.permissions.values).flatten }
end

#add(key, name = "", options = {}, &block) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fib/permissions_collection.rb', line 91

def add key, name="", options={}, &block
  return unless key.present?

  keys = [options[:key] || []].flatten
  urls = [options[:url] || []].flatten
  bind = [options[:bind] || []].flatten
  actions = options[:action] || []
  display = options[:display] if options.key? :display

  # 构建权限对象
  permission = Fib::Permission.new key, name: name

  # 默认创建一个与permission key相同的element key类型
  permission.append Fib::Element.create_key key
  permission.append keys.map{ |k| Fib::Element.create_key k }

  permission.append urls.map{ |u| Fib::Element.create_url u }
  permission.append actions.map{ |a|
    controller = a.shift
    a.map { |action| Fib::Element.create_action controller, action }
  }.flatten

  permission.bind_permission bind
  display ? permission.display_on : permission.display_off unless display.nil?

  # 执行自定义闭包
  permission.instance_exec &block if block_given?

  # 设置elements所属permission
  permission.inject_elements_permission
  permission.container = container

  # 将该权限放入集合
  set permission
end

#build_new(&block) ⇒ Object



87
88
89
# File 'lib/fib/permissions_collection.rb', line 87

def build_new &block
  self.class.new.tap { |n| n.instance_exec(&block) if block_given? }
end

#build_packageObject



52
53
54
# File 'lib/fib/permissions_collection.rb', line 52

def build_package
  @package = Fib::ElementPackage.merge *permission_packages
end

#empty?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/fib/permissions_collection.rb', line 44

def empty?
  permissions.keys.size == 0
end

#extract_by_keys(keys) ⇒ Object



79
80
81
# File 'lib/fib/permissions_collection.rb', line 79

def extract_by_keys keys
  Fib::PermissionsCollection.build_by_permissions select_permissions_by_keys(keys)
end

#keysObject



21
22
23
# File 'lib/fib/permissions_collection.rb', line 21

def keys
  permissions.keys
end

#mset(*permissions) ⇒ Object Also known as: append



34
35
36
37
38
39
40
# File 'lib/fib/permissions_collection.rb', line 34

def mset *permissions
  permissions.flatten.each do |p|
    next unless p.is_a?(Fib::Permission)
    @permissions[p.key] = p
  end
  build_package
end

#permission_packagesObject



48
49
50
# File 'lib/fib/permissions_collection.rb', line 48

def permission_packages
  (permissions.values.map(&:package) + permissions.values.map(&:bind_packages)).flatten.uniq
end

#permissions_infoObject



16
17
18
19
# File 'lib/fib/permissions_collection.rb', line 16

def permissions_info
  permissions.values.select { |v| v.display }
    .map { |v| [v.key, v.name] }
end

#select_permissions_by_keys(keys) ⇒ Object



83
84
85
# File 'lib/fib/permissions_collection.rb', line 83

def select_permissions_by_keys keys
  permissions.select { |k, v| keys.include? k }.values
end

#set(permission) ⇒ Object Also known as: <<



25
26
27
28
29
30
# File 'lib/fib/permissions_collection.rb', line 25

def set permission
  raise ParameterIsNotValid, "set method can't accept expect permission object" unless permission.is_a?(Fib::Permission)
  raise ParameterIsNotValid, "permission key #{permission.key} is exist" if permissions.has_key? permission.key

  mset permission
end