Class: Indulgence::Permission
- Inherits:
-
Object
- Object
- Indulgence::Permission
- Defined in:
- lib/indulgence/permission.rb
Constant Summary collapse
- @@role_method =
:role
- @@role_name_method =
:name
Instance Attribute Summary collapse
-
#ability ⇒ Object
readonly
Returns the value of attribute ability.
-
#entity ⇒ Object
readonly
Returns the value of attribute entity.
Class Method Summary collapse
- .all ⇒ Object
- .define_ability(args) ⇒ Object
- .none ⇒ Object
- .role_method ⇒ Object
- .role_method=(name) ⇒ Object
- .role_name_method ⇒ Object
- .role_name_method=(name) ⇒ Object
Instance Method Summary collapse
- #abilities ⇒ Object
- #abilities_for_role ⇒ Object
- #compare_single(thing) ⇒ Object
- #default ⇒ Object
- #define_ability(args) ⇒ Object
- #filter_many(things) ⇒ Object
-
#initialize(entity, ability, role_method = nil) ⇒ Permission
constructor
A new instance of Permission.
- #null_entity ⇒ Object
- #role_name ⇒ Object
Constructor Details
#initialize(entity, ability, role_method = nil) ⇒ Permission
Returns a new instance of Permission.
5 6 7 8 9 10 11 |
# File 'lib/indulgence/permission.rb', line 5 def initialize(entity, ability, role_method = nil) self @entity = entity @role_name = role_method.to_sym if role_method @ability = abilities_for_role[ability] raise AbilityNotFound, "Unable to find an ability called #{ability}" unless @ability end |
Instance Attribute Details
#ability ⇒ Object (readonly)
Returns the value of attribute ability.
3 4 5 |
# File 'lib/indulgence/permission.rb', line 3 def ability @ability end |
#entity ⇒ Object (readonly)
Returns the value of attribute entity.
3 4 5 |
# File 'lib/indulgence/permission.rb', line 3 def entity @entity end |
Class Method Details
.all ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/indulgence/permission.rb', line 66 def self.all Permission.define_ability( :name => :all, :compare_single => lambda {|thing, entity| true}, :filter_many => lambda {|things, entity| things.where(nil)} ) end |
.define_ability(args) ⇒ Object
74 75 76 77 |
# File 'lib/indulgence/permission.rb', line 74 def self.define_ability(args) args = generate_ability_args(args) ability_cache[args[:name].to_sym] ||= Ability.new args end |
.none ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/indulgence/permission.rb', line 58 def self.none Permission.define_ability( :name => :none, :compare_single => lambda {|thing, entity| false}, :filter_many => lambda {|things, entity| raise NotFoundError} ) end |
.role_method ⇒ Object
44 45 46 |
# File 'lib/indulgence/permission.rb', line 44 def self.role_method @@role_method end |
.role_method=(name) ⇒ Object
40 41 42 |
# File 'lib/indulgence/permission.rb', line 40 def self.role_method=(name) @@role_method = name.to_sym end |
.role_name_method ⇒ Object
54 55 56 |
# File 'lib/indulgence/permission.rb', line 54 def self.role_name_method @@role_name_method end |
.role_name_method=(name) ⇒ Object
50 51 52 |
# File 'lib/indulgence/permission.rb', line 50 def self.role_name_method=(name) @@role_name_method = name.to_sym end |
Instance Method Details
#abilities ⇒ Object
13 14 15 |
# File 'lib/indulgence/permission.rb', line 13 def abilities {} end |
#abilities_for_role ⇒ Object
87 88 89 90 |
# File 'lib/indulgence/permission.rb', line 87 def abilities_for_role return null_entity unless entity abilities[role_name] || default end |
#compare_single(thing) ⇒ Object
34 35 36 |
# File 'lib/indulgence/permission.rb', line 34 def compare_single(thing) ability.compare_single thing, entity end |
#default ⇒ Object
26 27 28 |
# File 'lib/indulgence/permission.rb', line 26 def default null_entity end |
#define_ability(args) ⇒ Object
79 80 81 |
# File 'lib/indulgence/permission.rb', line 79 def define_ability(args) self.class.define_ability(args) end |
#filter_many(things) ⇒ Object
30 31 32 |
# File 'lib/indulgence/permission.rb', line 30 def filter_many(things) ability.filter_many things, entity end |
#null_entity ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/indulgence/permission.rb', line 17 def null_entity { create: none, read: none, update: none, delete: none } end |
#role_name ⇒ Object
83 84 85 |
# File 'lib/indulgence/permission.rb', line 83 def role_name @role_name ||= entity_role_name end |