Module: Polidef::Policies::ClassMethods

Defined in:
lib/polidef/policies.rb

Instance Method Summary collapse

Instance Method Details

#defpolicy(policy_name, dependencies, &block) ⇒ Object

extract to InlinePolicy class



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/polidef/policies.rb', line 33

def defpolicy(policy_name, dependencies, &block)
  klass = Class.new(Policy) do
    def initialize(deps)
      deps[:dependencies].each do |dep|
        instance_variable_set(:"@#{dep}", nil)
        self.class.send(:attr_accessor, dep)
      end
    end

    def policy
      yield
    end
  end

  klass.include(Policies)
  klass.add_inline_policy({name: policy_name, policy: klass.new(dependencies)})
end