module ROM::Plugins::Relation::LDAP::AutoRestrictions

Generates methods for restricting relations by their indexed attributes

@example

rom = ROM.container(:ldap, {}) do |config|

  config.plugin(:ldap, relations: :auto_restrictions)

  config.relation(:users) do
    schema('(cn=*)', infer: true) do
      attribute :cn, ROM::LDAP::Types::Strings.meta(index: true)
    end
  end
end

rom.relations[:users].by_cn('Directory Administrator')

@api public

Public Class Methods

restriction_methods(schema) click to toggle source

@api private

# File lib/rom/plugins/relation/ldap/auto_restrictions.rb, line 40
def self.restriction_methods(schema)
  mod = Module.new
  methods = schema.attributes.each_with_object([]) do |attribute, generated|
    next unless attribute.indexed?

    meth_name = :"by_#{attribute.name}"
    next if generated.include?(meth_name)

    mod.module_eval do
      define_method(meth_name) do |value|
        where(attribute.name => value)
      end
    end

    generated << meth_name
  end

  [methods, mod]
end