Sha256: c930e3ea319dc18d79095adcb591b9d59c93d86d65c6fb97e1ab08de7dc2b229

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module StrongActions
  class Decision

    def initialize(target)
      @target = target
    end

    def call(role, controller_name, action_name = nil, params = {})
      role_definition = StrongActions.config.role_definition(role)
      return true unless role_definition

      begin
        role_object = @target.instance_eval(role)
      rescue NameError
        raise "role #{role} is not defined in controller"
      end

      controller_value = role_definition[controller_name]
      return true if controller_value.nil?

      if controller_value.is_a?(Hash)
        action_name ||= 'index'
        action_value = controller_value[action_name]
      else
        action_value = controller_value
      end
      return true if action_value.nil?

      action_value = [action_value] unless action_value.is_a?(Array)
      action_value.each do |definition|
        next if definition === true
        return false unless definition
        return false unless role_object.instance_eval(definition)
      end

      true
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
strong_actions-0.0.4 lib/strong_actions/decision.rb