# class ActiveLdap::GetText::Parser

### Public Class Methods

new(configuration=nil) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 9
    def initialize(configuration=nil)
      configuration = ensure_configuration(configuration)
      configuration = default_configuration.merge(configuration)

      configuration = extract_options(configuration)
      ActiveLdap::Base.setup_connection(configuration)
    end

### Public Instance Methods

extract_all_in_schema(targets=[]) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 37
    def extract_all_in_schema(targets=[])
      extract(targets) do
        schema = ActiveLdap::Base.schema
        schema.object_classes.each do |object_class|
          register_object_class(object_class, "-")
        end
        schema.attributes.each do |attribute|
          register_attribute(attribute, "-")
        end
        schema.ldap_syntaxes.each do |syntax|
          register_syntax(syntax, "-")
        end
      end
    end

parse(file, targets=[]) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 17
    def parse(file, targets=[])
      targets = RubyParser.parse(file, targets) if RubyParser.target?(file)
      extract(targets) do
        load_constants(file).each do |name|
          klass = name.constantize
          next unless klass.is_a?(Class)
          next unless klass < ActiveLdap::Base
          register(klass.name.singularize.underscore.gsub(/_/, " "), file)
          next unless @extract_schema
          klass.classes.each do |object_class|
            register_object_class(object_class, file)
          end
        end
      end
    end

target?(file) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 33
    def target?(file)
      @classes_re.match(File.read(file))
    end

### Private Instance Methods

default_configuration() click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 61
    def default_configuration
      {
        :host => "127.0.0.1",
        :allow_anonymous => true,
        :extract_schema => false,
      }
    end

ensure_configuration(configuration) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 69
    def ensure_configuration(configuration)
      configuration ||= ENV["RAILS_ENV"] || {}
      if configuration.is_a?(String)
        if File.exists?(configuration)
          require 'erb'
          require 'yaml'
          erb = ERB.new(File.read(configuration))
          erb.filename = configuration
          configuration = YAML.load(erb.result)
        else
          ENV["RAILS_ENV"] = configuration
          require 'config/environment'
          configuration = ActiveLdap::Base.configurations[configuration]
        end
      end
      if defined?(Rails)
        rails_configuration = ActiveLdap::Base.configurations[Rails.env]
        configuration = rails_configuration.merge(configuration)
      end
      configuration = configuration.symbolize_keys
    end

extract(targets) { || ... } click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 103
    def extract(targets)
      @targets = {}
      targets.each do |id, *file_infos|
        @targets[id] = file_infos
      end
      yield
      @targets.collect do |id, file_infos|
        [id, *file_infos.uniq]
      end.sort_by do |id,|
        id
      end
    end

extract_options(configuration) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 53
    def extract_options(configuration)
      configuration = configuration.dup
      classes = configuration.delete(:classes) || ["ActiveLdap::Base"]
      @classes_re = /class.*#{Regexp.union(*classes)}/ #
      @extract_schema = configuration.delete(:extract_schema)
      configuration
    end

load_constants(file) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 91
    def load_constants(file)
      old_constants = Object.constants
      begin
        eval(File.read(file), TOPLEVEL_BINDING, file)
      rescue
        format = _("Ignored '%{file}'. Solve dependencies first.")
        $stderr.puts(format % {:file => file})
        $stderr.puts($!)
      end
      Object.constants - old_constants
    end

register(id, file) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 116
    def register(id, file)
      file_info = "#{file}:-"
      @targets[id] ||= []
      @targets[id] << file_info
    end

register_attribute(attribute, file) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 139
    def register_attribute(attribute, file)
      [attribute.name, *attribute.aliases].each do |name|
        msgid = ActiveLdap::Base.human_attribute_name_msgid(name)
        register(msgid, file) if msgid
      end
      if attribute.description
        msgid = ActiveLdap::Base.human_attribute_description_msgid(attribute)
        register(msgid, file)
      end
    end

register_object_class(object_class, file) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 122
    def register_object_class(object_class, file)
      [object_class.name, *object_class.aliases].each do |name|
        register(ActiveLdap::Base.human_object_class_name_msgid(name), file)
      end
      if object_class.description
        msgid =
          ActiveLdap::Base.human_object_class_description_msgid(object_class)
        register(msgid, file)
      end
      (object_class.must(false) + object_class.may(false)).each do |attribute|
        register_attribute(attribute, file)
      end
      object_class.super_classes.each do |super_class|
        register_object_class(super_class, file)
      end
    end

register_syntax(syntax, file) click to toggle source

    # File lib/active_ldap/get_text/parser.rb, line 150
    def register_syntax(syntax, file)
      msgid = ActiveLdap::Base.human_syntax_name_msgid(syntax)
      register(msgid, file)

      if syntax.description
        msgid = ActiveLdap::Base.human_syntax_description_msgid(syntax)
        register(msgid, file)
      end
    end