Sha256: 375d56cb197a654ed743404162daf8610bcfc61a7731d9bd01ef8f70fc579dee

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module StrongActions
  class Decision

    def initialize(target)
      @target = target
    end

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

      controller_value = role_definition[controller_path]
      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

        role_object = role_object_for(role)
        return false unless role_object.instance_eval(definition)
      end

      true
    end

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

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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