Module: Roleful::ClassMethods

Defined in:
lib/roleful/inclusion.rb

Instance Method Summary collapse

Instance Method Details

#delegate_permission(*methods) ⇒ Object

Delegates method calls to the object's #role_proxy, accounting for cases when there are multiple roles for the given object.

Raises:

  • (ArgumentError)

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/roleful/inclusion.rb', line 51

def delegate_permission(*methods)
  options = methods.pop
  raise ArgumentError, "Delegation needs a target." unless options.is_a?(Hash) && to = options[:to]

  methods.each do |method|
    module_eval(<<-EOS, "(__DELEGATION__)", 1)
      def #{method}(*args, &block)
        target = Array(send(#{to.inspect}))
        target.any? do |to|
          to.__send__(#{method.inspect}, self, *args, &block)
        end
      end
    EOS
  end
end

#role(*args, &block) ⇒ Object


43
44
45
46
47
# File 'lib/roleful/inclusion.rb', line 43

def role(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  roles = [:all].eql?(args) ? all_roles : args
  roles.each { |name| define_role(name.to_sym, options, &block) }
end