Class: StrongActions::Decision
- Inherits:
-
Object
- Object
- StrongActions::Decision
- Defined in:
- lib/strong_actions/decision.rb
Instance Method Summary collapse
- #call(role, controller_path, action_name = nil, params = {}) ⇒ Object
- #controller_names_for(controller_path) ⇒ Object
-
#initialize(target) ⇒ Decision
constructor
A new instance of Decision.
- #role_object_for(role) ⇒ Object
Constructor Details
#initialize(target) ⇒ Decision
Returns a new instance of Decision.
4 5 6 |
# File 'lib/strong_actions/decision.rb', line 4 def initialize(target) @target = target end |
Instance Method Details
#call(role, controller_path, action_name = nil, params = {}) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/strong_actions/decision.rb', line 8 def call(role, controller_path, action_name = nil, params = {}) action_name ||= 'index' role_definition = StrongActions.config.role_definition(role) return true unless role_definition controller_names_for(controller_path).each do |controller_name| controller_value = role_definition[controller_name] next if controller_value.nil? if controller_value.is_a?(Hash) action_value = controller_value[action_name] else action_value = controller_value end next if action_value.nil? action_values = Array(action_value) action_values.each do |definition| next if definition === true return false if definition === false role_object = role_object_for(role) return false unless role_object return false unless role_object.instance_eval(definition) end break end true end |
#controller_names_for(controller_path) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/strong_actions/decision.rb', line 49 def controller_names_for(controller_path) ret = [] path_elements = controller_path.split('/') if path_elements.size == 1 ret = path_elements else path_elements.each_with_index do |path_element, i| ret << ret.last.to_s + path_element + (i < path_elements.size - 1 ? '/' : '') end ret.reverse! end ret end |
#role_object_for(role) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/strong_actions/decision.rb', line 41 def role_object_for(role) begin return @target.instance_eval(role) rescue NameError raise "role #{role} is not defined in controller" end end |