module LOM::Mapper
Instance methods to be injected in the class being mapped.
Class methods to be injected in the class being mapped, and performs initialization thanks to extend_object
Public Class Methods
# File lib/lom/mapper.rb, line 68 def self.extend_object(o) super o.include Mapper::InstanceMethods o.extend Enumerable o.const_set(:Filtered, LOM::Filtered) o.__ldap_init end
Public Instance Methods
# File lib/lom/mapper.rb, line 76 def __ldap_init @__ldap_branch = nil @__ldap_prefix = nil @__ldap_scope = :one @__ldap_filter = nil @__ldap_attrs = nil @__ldap_from = nil @__ldap_to = nil @__ldap_list = [] @__ldap_lh = nil end
# File lib/lom/mapper.rb, line 201 def _ldap_to_obj(entry) raise EntryNotFound if entry.nil? entry.extend(EntryEnhanced) args = entry.instance_exec(entry, &_ldap_from) args = [ args ] unless args.kind_of?(Array) self.new(*args) end
Retrieve matching data as a list of object
@return [Array<Object>]
# File lib/lom/mapper.rb, line 288 def all(&rawfilter) each(:object, rawfilter: rawfilter).to_a end
# File lib/lom/mapper.rb, line 308 def delete!(name) dn = ldap_dn_from_id(name) lh.delete(:dn => dn) end
Iterate over matching data.
@note If using `rawfilter`, no optimization will be performed
aned the ldap attributes will be retrieved, even if desired type is :id
@param type [:object, :id] return object or id @param filter [String] extra ldap search filter @param rawfilter [Proc] filter on ldap entry @param paged [Array<Integer,Integer>] pagination information
@yieldparam obj_or_id [Object, String] ldap converted element according to type
@return [Enumerator] if no block given @return [self] if block given
# File lib/lom/mapper.rb, line 226 def each(type = :object, filter: nil, rawfilter: nil, paged: nil) # Create Enumerator if no block given unless block_given? return enum_for(:each, type, filter: filter, rawfilter: rawfilter, paged: paged) end # Merging filters filter = Net::LDAP.filter('&', *[ filter, _ldap_filter ].compact) # Define attributes/converter according to selected type attributes, converter = case type when :id then [ rawfilter ? _ldap_attrs : :dn, ->(e) { ldap_dn_to_id(e.dn) } ] when :object then [ _ldap_attrs, ->(e) { _ldap_to_obj(e) } ] else raise ArgumentError, 'type must be either :object or :id' end # Paginate # XXX: pagination is emulated, should be avoided skip, count = if paged page, page_size = paged [ (page - 1) * page_size, page_size ] end # Perform search lh.search(:base => _ldap_branch, :filter => filter, :attributes => attributes, :scope => _ldap_scope) {|entry| if rawfilter && !rawfilter.call(entry) next elsif paged.nil? yield(converter.(entry)) elsif skip > 0 skip -= 1 elsif count <= 0 break else count -= 1 yield(converter.(entry)) end } # Return self self end
# File lib/lom/mapper.rb, line 300 def get(name) dn = ldap_dn_from_id(name) attrs = _ldap_attrs entry = lh.get(:dn => dn, :attributes => attrs) _ldap_to_obj(entry) end
# File lib/lom/mapper.rb, line 148 def ldap_attrs(*v) @__ldap_attrs = v end
# File lib/lom/mapper.rb, line 130 def ldap_branch(v = nil) return _ldap_branch if v.nil? @__ldap_branch = v end
# File lib/lom/mapper.rb, line 197 def ldap_dn_from_id(id) Net::LDAP::DN.new(_ldap_prefix.to_s, id, _ldap_branch).to_s end
Convert a dn to it's corresponding id the current mapping.
@raise [Error] dn belongs to this mapping (it is in the mapping
branch), but is malformed (not a direct child, or wrong prefix)
@return [String] id @return [nil] dn is not from this mapping
# File lib/lom/mapper.rb, line 182 def ldap_dn_to_id(dn) prefix = _ldap_prefix.to_s branch = _ldap_branch if sub = Net::LDAP::DN.sub?(dn, branch) case prefix when String, Symbol k, v, _ = sub.to_a raise Error, "not a direct child" if _ raise Error, "wrong prefix" if k.casecmp(prefix) != 0 v end end end
# File lib/lom/mapper.rb, line 144 def ldap_filter(v) @__ldap_filter = v[0] == '(' ? v : "(#{v})" end
@note block will be executed in the Net::LDAP::Entry instance
# File lib/lom/mapper.rb, line 153 def ldap_from(p=nil, &b) if (! p.nil? ^ b.nil?) || (p && !p.kind_of?(Proc)) raise ArgumentError, 'one and only one of proc/lamba/block need to be defined' end @__ldap_from = p || b end
# File lib/lom/mapper.rb, line 113 def ldap_list(name, body=nil, &block) if body && block raise ArgumentError elsif body.nil? && block.nil? raise ArgumentError elsif block body = block end @__ldap_list << name define_singleton_method(name) do |*args| filter = body.call(*args) LOM::Filtered.new(filter, src: self) end end
Return the list of defined list (using ldap_list
).
@return [Array<Symbol>] list of defined ldap list
# File lib/lom/mapper.rb, line 109 def ldap_listing @__ldap_list end
# File lib/lom/mapper.rb, line 135 def ldap_prefix(v = nil) return _ldap_prefix if v.nil? @__ldap_prefix = v end
# File lib/lom/mapper.rb, line 140 def ldap_scope(v) @__ldap_scope = v end
@note block will be executed in the mapped object instance
# File lib/lom/mapper.rb, line 162 def ldap_to(p=nil, &b) return _ldap_to if p.nil? && b.nil? if (! p.nil? ^ b.nil?) || (p && !p.kind_of?(Proc)) raise ArgumentError, 'one and only one of proc/lamba/block need to be defined' end @__ldap_to = p || b end
Set the LDAP
handler to use
# File lib/lom/mapper.rb, line 101 def lh=(lh) @__ldap_lh = lh end
Retrieve matching data as a list of id
@return [Array<String>]
# File lib/lom/mapper.rb, line 296 def list(&rawfilter) each(:id, rawfilter: rawfilter).to_a end
# File lib/lom/mapper.rb, line 280 def paginate(page, page_size) LOM::Filtered.new(src: self, paged: [ page, page_size ]) end
Private Instance Methods
# File lib/lom/mapper.rb, line 333 def _ldap_attrs @__ldap_attrs end
# File lib/lom/mapper.rb, line 317 def _ldap_branch @__ldap_branch || (raise MappingError, 'ldap_branch not defined') end
# File lib/lom/mapper.rb, line 329 def _ldap_filter @__ldap_filter end
# File lib/lom/mapper.rb, line 337 def _ldap_from @__ldap_from || (raise MappingError, 'ldap_from not defined' ) end
# File lib/lom/mapper.rb, line 321 def _ldap_prefix @__ldap_prefix || (raise MappingError, 'ldap_prefix not defined') end
# File lib/lom/mapper.rb, line 325 def _ldap_scope @__ldap_scope end
# File lib/lom/mapper.rb, line 341 def _ldap_to @__ldap_to || (raise MappingError, 'ldap_to not defined' ) end