Class: Roleback::Definitions::Role
- Defined in:
- lib/roleback/definitions/role.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parents ⇒ Object
readonly
Returns the value of attribute parents.
Attributes inherited from RuleBased
Instance Method Summary collapse
- #add_rule(rule) ⇒ Object
- #inherit ⇒ Object
-
#initialize(name, parents: nil) ⇒ Role
constructor
A new instance of Role.
- #keys ⇒ Object
- #resource(name, options = {}, &block) ⇒ Object
- #rules ⇒ Object
- #scope(name, &block) ⇒ Object
- #to_s ⇒ Object
Methods inherited from RuleBased
Constructor Details
#initialize(name, parents: nil) ⇒ Role
Returns a new instance of Role.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/roleback/definitions/role.rb', line 7 def initialize(name, parents: nil) @name = name @rule_book = ::Roleback::RuleBook.new(self) @scopes = {} @resources = {} if parents if parents.is_a?(Symbol) @parents = [parents] elsif parents.is_a?(Array) # check for duplicates raise ::Roleback::BadConfiguration, "Duplicate parents found for role #{name}" if parents.uniq.length != parents.length @parents = parents else raise ::Roleback::BadConfiguration, "Parent must be a symbol or an array of symbols" end else @parents = nil end super(role: self, resource: ::Roleback::ANY, scope: ::Roleback::ANY) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/roleback/definitions/role.rb', line 4 def name @name end |
#parents ⇒ Object (readonly)
Returns the value of attribute parents.
5 6 7 |
# File 'lib/roleback/definitions/role.rb', line 5 def parents @parents end |
Instance Method Details
#add_rule(rule) ⇒ Object
39 40 41 |
# File 'lib/roleback/definitions/role.rb', line 39 def add_rule(rule) @rule_book.add(rule) end |
#inherit ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/roleback/definitions/role.rb', line 61 def inherit return if @parents.nil? new_rules = do_inherit if new_rules.empty? # no rules to inherit return end # add the new rules to the rule book self.rules.clear_rules new_rules.each do |rule| self.rules.add(rule) end end |
#keys ⇒ Object
35 36 37 |
# File 'lib/roleback/definitions/role.rb', line 35 def keys @rule_book.rules.keys end |
#resource(name, options = {}, &block) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/roleback/definitions/role.rb', line 43 def resource(name, = {}, &block) raise ::Roleback::BadConfiguration, "Resource #{name} already defined" if @resources[name] resource = ::Roleback::Definitions::Resource.new(name, role: self, options: , &block) @resources[name] = resource end |
#rules ⇒ Object
31 32 33 |
# File 'lib/roleback/definitions/role.rb', line 31 def rules @rule_book end |
#scope(name, &block) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/roleback/definitions/role.rb', line 50 def scope(name, &block) raise ::Roleback::BadConfiguration, "Scope #{name} already defined" if @scopes[name] scope = ::Roleback::Definitions::Scope.new(name, role: self, &block) @scopes[name] = scope end |
#to_s ⇒ Object
57 58 59 |
# File 'lib/roleback/definitions/role.rb', line 57 def to_s self.name.to_s end |