Class: Fib::PermissionsCollection
- Inherits:
-
Object
- Object
- Fib::PermissionsCollection
- Extended by:
- Forwardable
- Defined in:
- lib/fib/permissions_collection.rb
Instance Attribute Summary collapse
-
#container ⇒ Object
Returns the value of attribute container.
-
#package ⇒ Object
readonly
Returns the value of attribute package.
-
#permissions ⇒ Object
readonly
Returns the value of attribute permissions.
Class Method Summary collapse
Instance Method Summary collapse
- #&(permission_collection) ⇒ Object
- #+(permission_collection) ⇒ Object (also: #|)
- #-(permission_collection) ⇒ Object
- #add(key, name = "", options = {}, &block) ⇒ Object
- #build_new(&block) ⇒ Object
- #build_package ⇒ Object
- #empty? ⇒ Boolean
- #extract_by_keys(keys) ⇒ Object
-
#initialize ⇒ PermissionsCollection
constructor
A new instance of PermissionsCollection.
- #keys ⇒ Object
- #mset(*permissions) ⇒ Object (also: #append)
- #permission_packages ⇒ Object
- #permissions_info ⇒ Object
- #select_permissions_by_keys(keys) ⇒ Object
- #set(permission) ⇒ Object (also: #<<)
Constructor Details
#initialize ⇒ PermissionsCollection
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
#container ⇒ Object
Returns the value of attribute container.
6 7 8 |
# File 'lib/fib/permissions_collection.rb', line 6 def container @container end |
#package ⇒ Object (readonly)
Returns the value of attribute package.
5 6 7 |
# File 'lib/fib/permissions_collection.rb', line 5 def package @package end |
#permissions ⇒ Object (readonly)
Returns the value of attribute permissions.
5 6 7 |
# File 'lib/fib/permissions_collection.rb', line 5 def @permissions end |
Class Method Details
.build_by_permissions(permissions) ⇒ Object
129 130 131 132 |
# File 'lib/fib/permissions_collection.rb', line 129 def return unless .is_a? Array new.tap { |p| p.append(*) } end |
Instance Method Details
#&(permission_collection) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/fib/permissions_collection.rb', line 72 def & raise ParameterIsNotValid, "must be permission_collection" unless .is_a?(Fib::PermissionsCollection) = .values build_new { append *( & ..values).flatten } end |
#+(permission_collection) ⇒ Object Also known as: |
56 57 58 59 60 61 |
# File 'lib/fib/permissions_collection.rb', line 56 def + raise ParameterIsNotValid, "must be permission_collection" unless .is_a?(Fib::PermissionsCollection) = .values build_new { append *( | ..values).flatten } end |
#-(permission_collection) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/fib/permissions_collection.rb', line 65 def - raise ParameterIsNotValid, "must be permission_collection" unless .is_a?(Fib::PermissionsCollection) = .values build_new { append *( - ..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="", ={}, &block return unless key.present? keys = [[:key] || []].flatten urls = [[:url] || []].flatten bind = [[:bind] || []].flatten actions = [:action] || [] display = [:display] if .key? :display # 构建权限对象 = Fib::Permission.new key, name: name # 默认创建一个与permission key相同的element key类型 .append Fib::Element.create_key key .append keys.map{ |k| Fib::Element.create_key k } .append urls.map{ |u| Fib::Element.create_url u } .append actions.map{ |a| controller = a.shift a.map { |action| Fib::Element.create_action controller, action } }.flatten . bind display ? .display_on : .display_off unless display.nil? # 执行自定义闭包 .instance_exec &block if block_given? # 设置elements所属permission . .container = container # 将该权限放入集合 set 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_package ⇒ Object
52 53 54 |
# File 'lib/fib/permissions_collection.rb', line 52 def build_package @package = Fib::ElementPackage.merge * end |
#empty? ⇒ Boolean
44 45 46 |
# File 'lib/fib/permissions_collection.rb', line 44 def empty? .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. (keys) end |
#keys ⇒ Object
21 22 23 |
# File 'lib/fib/permissions_collection.rb', line 21 def keys .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 * .flatten.each do |p| next unless p.is_a?(Fib::Permission) @permissions[p.key] = p end build_package end |
#permission_packages ⇒ Object
48 49 50 |
# File 'lib/fib/permissions_collection.rb', line 48 def (.values.map(&:package) + .values.map(&:bind_packages)).flatten.uniq end |
#permissions_info ⇒ Object
16 17 18 19 |
# File 'lib/fib/permissions_collection.rb', line 16 def .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 keys .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 raise ParameterIsNotValid, "set method can't accept expect permission object" unless .is_a?(Fib::Permission) raise ParameterIsNotValid, "permission key #{.key} is exist" if .has_key? .key mset end |