Class: RoleAuthorization::Mapper
- Inherits:
-
Object
- Object
- RoleAuthorization::Mapper
- Defined in:
- lib/role_authorization/controller/mapper.rb
Instance Method Summary collapse
- #add_to_rules(rule_name, *options, &block) ⇒ Object
- #authorized?(controller_instance, controller, action, id = nil) ⇒ Boolean
-
#initialize ⇒ Mapper
constructor
A new instance of Mapper.
- #to_s ⇒ Object
- #with_options(options, &block) ⇒ Object
Constructor Details
#initialize ⇒ Mapper
Returns a new instance of Mapper.
3 4 5 6 7 8 |
# File 'lib/role_authorization/controller/mapper.rb', line 3 def initialize @rules = Hash.new do |h,k| h[k] = Array.new end self end |
Instance Method Details
#add_to_rules(rule_name, *options, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/role_authorization/controller/mapper.rb', line 30 def add_to_rules(rule_name, *, &block) , extracted_role = if .is_a?(Hash) [, nil] elsif .last.is_a?(Hash) [.pop, .first] else [{}, .first] end = if @with_options.nil? else @with_options.merge() end rule = RoleAuthorization::Rules::Rule.new(extracted_role, , &block) actions = ([rule.[:only] || [:all]]).flatten.map(&:to_sym) actions.map do |action| @rules[action] << rule end end |
#authorized?(controller_instance, controller, action, id = nil) ⇒ Boolean
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/role_authorization/controller/mapper.rb', line 54 def (controller_instance, controller, action, id = nil) rules = @rules[action] return false if rules.empty? rules.map do |rule| return true if rule.(controller_instance, controller, action, id) end return false end |
#to_s ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/role_authorization/controller/mapper.rb', line 10 def to_s output = [] @rules.each_pair do |action, rules| output << "Action :#{action}" rules.map {|rule| output << " #{rule.to_s}"} output << "" output << "" end output.join("\n") end |
#with_options(options, &block) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/role_authorization/controller/mapper.rb', line 22 def (, &block) unless block.nil? @with_options = instance_eval(&block) @with_options = nil end end |