Class: Passpartu::Patcher

Inherits:
Object
  • Object
show all
Defined in:
lib/passpartu/patcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Patcher

Returns a new instance of Patcher.



7
8
9
10
11
# File 'lib/passpartu/patcher.rb', line 7

def initialize(klass)
  raise PolicyYmlNotFoundError if Passpartu.policy.nil?

  @klass = klass
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/passpartu/patcher.rb', line 5

def klass
  @klass
end

Class Method Details

.call(klass) ⇒ Object



13
14
15
# File 'lib/passpartu/patcher.rb', line 13

def self.call(klass)
  new(klass).call
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/passpartu/patcher.rb', line 17

def call
  phash = respond_to?(:policy_hash) ? {} : Passpartu.policy
  role_method = Passpartu.config.role_access_method

  klass.class_eval do
    define_method(:can?) do |*keys, only: nil, except: nil, skip: nil, &block|
      Passpartu::BlockVerify.call(
        send(role_method),
        keys,
        only: only,
        except: except,
        skip: skip,
        policy_hash: phash,
        &block
      )
    end

    phash.each_key do |policy_role|
      define_method("#{policy_role}_can?") do |*keys, only: nil, except: nil, skip: nil, &block|
        send(role_method).to_s == policy_role &&
          Passpartu::BlockVerify.call(
            send(role_method),
            keys,
            only: only,
            except: except,
            skip: skip,
            policy_hash: phash,
            &block
          )
      end
    end
  end
end