Class: ResourcePolicy::ProtectedResource

Inherits:
Object
  • Object
show all
Defined in:
lib/resource_policy/protected_resource.rb

Overview

Generates resource which has same attributes as policy target, but returns ‘nil` when attribute in not readable according to policy

Instance Method Summary collapse

Constructor Details

#initialize(policy) ⇒ ProtectedResource

Returns a new instance of ProtectedResource.



7
8
9
# File 'lib/resource_policy/protected_resource.rb', line 7

def initialize(policy)
  @policy = policy
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



11
12
13
14
15
16
# File 'lib/resource_policy/protected_resource.rb', line 11

def method_missing(method_name, *args)
  return super unless target_respond_to?(method_name, *args)
  return nil unless policy.attribute(method_name).readable?

  policy_target.public_send(method_name, *args)
end

Instance Method Details

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/resource_policy/protected_resource.rb', line 18

def respond_to_missing?(*args)
  target_respond_to?(*args) || super
end