Project

General

Profile

Download (4.38 KB) Statistics
| Branch: | Tag: | Revision:
#--
# LdapShadows, a Medium-level LDAP Access Library and Tool.
# Copyright (c) 2009 Marc Dequènes (Duck) <Duck@DuckCorp.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++


$KCODE = 'UTF8'
require 'jcode'
require 'active_ldap'
require 'ldap_shadows/activeldap_fixes'
require 'facets/metaid'
require 'lib/ldap_shadows/elements'


module LdapShadows
module Shadows
end

class Shadow
attr_reader :name

def initialize(name)
@name = name

@config = Config.instance

# cannot use anonymous modules/classes, as active_ldap needs named classes for relations
module_name = "Shadow" + name.camelize
@container = LdapShadows::Shadows.module_eval(<<-EOS)
module #{module_name}; self; end
EOS

@container_elements = {}
@aspects = {}
end

def set_config(shadow_def_raw)
@shadow_config = @config.parse_and_validate(@name, 'shadow', shadow_def_raw)
end

def get_config
@shadow_config
end

def add_aspect(aspect_name, aspect_def_raw)
e_type = 'aspect'

klass = add_container_element(e_type, aspect_name, aspect_def_raw)

# TODO: remove this crap
aspect_def = @config.parse_and_validate(aspect_name, e_type, aspect_def_raw)
@aspects[aspect_name.to_sym] = aspect_def
end

def get_aspect(aspect_name)
@aspects[aspect_name.to_sym]
end

def get_aspect_klass(aspect_name)
@container_elements['aspect'][aspect_name.to_s]
end

def add_object(object_name, object_def_raw)
e_type = 'object'

klass = add_container_element(e_type, object_name, object_def_raw)

# TODO: remove this crap
object_def = @config.parse_and_validate(object_name, e_type, object_def_raw)
klass.presentation = object_def[:presentation]
end

def get_object(object_name)
@container_elements['object'][object_name.to_s]
end

def cast
@container_elements.each_value do |e_models|
e_models.each_value do |e_klass|
e_klass.cast
end
end

@container_elements.each_value do |e_models|
e_models.each_value do |e_klass|
e_klass.cast_relations
end
end
end

def objects
@container_elements['object'].keys.sort
end

def aspects
@container_elements['aspect'].keys.sort
end

protected

def add_container_element(e_type, e_name, e_def_raw)
superklass_location = LdapShadows::Elements
superklass_name = 'Ldap' + e_type.camelize
unless superklass_location.const_defined?(superklass_name)
raise PreProcessingError, _("Element model '%s' does not exist") % e_type
end

klass_name = superklass_name + e_name.to_s.camelize
if @container.const_defined?(klass_name)
raise PreProcessingError, _("Element '%s' for model '%s' as already been defined") % [e_name, e_type]
end

e_def = @config.parse_and_validate(e_name, e_type, e_def_raw)

klass = @container.module_eval(<<-EOS)
class #{klass_name} < #{superklass_location}::#{superklass_name}; self; end
EOS
klass.instance_variable_set(:@handle, e_name)
klass.instance_variable_set(:@shadow, self)
klass.instance_variable_set(:@parameters, e_def)
klass.meta_eval do
attr_reader :handle, :shadow, :parameters
protected :parameters
end

begin
klass_content = @config.load_hook_content(@name, e_type, e_name)
klass.class_eval(klass_content) unless klass_content.nil?
rescue
raise PreProcessingError, _("Could not load '%s' plugin for '%s': %s") % [e_type, e_name, $!]
end

@container_elements[e_type] ||= {}
@container_elements[e_type][e_name] = klass

klass
rescue
raise PreProcessingError, _("Could not create element '%s' for model '%s': %s") % [e_type, e_name, $!]
end
end
end

(8-8/8)