class PIM::DataModelManager::CategoryManager

Public Class Methods

new(data_model, category = nil, category_name = nil) click to toggle source
# File data-model-manager.rb, line 265
def initialize data_model, category = nil, category_name = nil
  super data_model
  @category = category
  @category_name = category_name
end

Public Instance Methods

[](category_name) click to toggle source
# File data-model-manager.rb, line 277
def [] category_name
  category = data_model.category("#{data_model.name}::#{category_name}")
  if not category
    PIM.log_warn("Could not find category '#{category_name}' in module '#{data_model.name}'")
  end
  return CategoryManager.new(data_model, category, category_name)
end
attributes() click to toggle source
# File data-model-manager.rb, line 285
def attributes
  return CategoryAttributes.new(data_model, @category)
end
create(category_name, parent_category = nil, params = {}) click to toggle source
# File data-model-manager.rb, line 289
def create category_name, parent_category = nil, params = {}
  begin

    category_full_name = "#{data_model.name}::#{category_name}"
    category = data_model.category(category_full_name)

    if category and category.modification_status != :DELETED
      PIM.log_warn("Could not create category '#{category_name}' because it already exists in module '#{data_model.name}'")
    else

      old_category = category
      parent_category = PIM::Utils.constantize(parent_category.to_s) if not parent_category.is_a?(Class)
      PIM::Utils.remove_const_on_parent category_full_name
      data_model.module_eval %Q(class #{category_name} < #{parent_category}; end)
      category = data_model.category(category_full_name)
      category.instance_eval do
        label params[:label]
      end

      category.modification_status = :CREATED
      category.model_defined = (old_category and old_category.model_defined)

    end
    category

  rescue Exception => e
    PIM.log_trace "Error creating category '#{category_name}'", e
    return
  end
end
delete() click to toggle source
# File data-model-manager.rb, line 363
def delete
  if (category)
    category.modification_status = :DELETED
  else
    PIM.log_warn("Could not delete category #{category_name} because it is not defined in module '#{data_model.name}'")
  end
  return category
end
update(parent_category = nil, params = {}) click to toggle source
# File data-model-manager.rb, line 320
def update parent_category = nil, params = {}
  begin

    category = @category
    category_name = @category_name
    category_full_name = "#{data_model.name}::#{category_name}"

    if not category
      PIM.log_warn("Could not update category because it is not defined in module '#{data_model.name}'")
    else

      parent_category = PIM::Utils.constantize(parent_category.to_s) if not parent_category.is_a?(Class)
      if category.class_parent != parent_category
        old_category = category
        PIM::Utils.remove_const_on_parent category_full_name
        data_model.module_eval %Q(class #{category_name} < #{parent_category}; end)
        category = data_model.category(category_full_name)
        if old_category
          old_variables = {}
          old_category.instance_variables.each do |var|
            old_variables[var] = old_category.instance_variable_get("#{var}")
          end
          old_variables.each do |key, value|
            category.instance_variable_set(key, value) if key != :@class_parent
          end
        end
      end

      category.modification_status = :UPDATED
      category.instance_eval do
        label params[:label]
        primary_key *params[:primary_key] if params[:primary_key]
      end

    end
    category

  rescue Exception => e
    PIM.log_trace "Error updating category '#{@category_name}'", e
    return
  end
end