config.rb 1.09 KB
class DsAdmin::Storage::Config
  attr_accessor :model

  def initialize(conf_data)
    @config = conf_data
  end

  def con(storage)
    @config[storage.config_key][:con]
  end

  def query(storage)
    @config[storage.config_key][@model.config_key][:query]
  end

  def map(storage)
    @config[storage.config_key][@model.config_key][:map]
  end

  ##
  # replace special patterns within config strings with data
  # given by the data hash param. These patterns also allow to 
  # substitute part of the data giving the argument for a
  # String#sub call after a | to it.
  #
  # actually this is used within the handling of the ldap specific
  # :dnPat config parameter to generate the dn entry for a given model
  # Data (create or replace)
  #
  def eval_pattern(pattern, data = {})
    scan_exp = /([^#]*)(#\{(:[^}|]+)(\|([^}]*))?\})?/

    result = String.new
    pattern.scan(scan_exp) do |m|
      key = eval m[2] if m[2]
      msg = eval '[:sub, ' + m[4] + ']' if m[4]

      val = data[key] if data[key]
      val = val.send *msg if msg

      result += m[0] + (val || "")
    end
    result
  end
end