module PIM::Categories

Public Instance Methods

additional_categories(category_names_per_module) click to toggle source
# File pim.rb, line 5690
def additional_categories category_names_per_module
  additional_categories = {}
  category_names_per_module.each_pair do |additional_module_name, category_names|
    additional_module_name = additional_module_name.to_s
    additional_module = PIM.get_module(additional_module_name)
    if !additional_module
      log_warn "Additional module '#{additional_module_name}' is not defined"
      next
    end
    categories = []
    category_names.each do |category_name|
      category_name = category_name.to_s
      category = query_module_category(additional_module, category_name)
      if !category
        log_warn "Category '#{category_name}' is not defined in additional module '#{additional_module_name}'"
        next
      end
      PIM.execute_in_module(additional_module) do
        category = category.as_additional_category
      end
      category[:code] = category_name if !category[:code]
      category[:additional_module] = additional_module_name if !category[:additional_module]
      categories << category
    end
    additional_categories[additional_module.to_s] = categories if !categories.empty?
  end
  additional_categories
end
additional_categories_as_json(category_names_per_module) click to toggle source
# File pim.rb, line 5719
def additional_categories_as_json category_names_per_module
  as_json(Hash[additional_categories(category_names_per_module).sort])
end
all_categories(without_hidden = false) click to toggle source
# File pim.rb, line 5638
def all_categories without_hidden = false
  categories = {}
  categories.merge!(@module_categories) if @module_categories
  add_parent_objects(categories) { |p| p.module_categories }
  categories.reject! { |name, category| category.class_hidden } if without_hidden
  categories
end
calculate_category(values) click to toggle source
# File pim.rb, line 5654
def calculate_category values
  category_calculation = @category_calculation
  if not category_calculation
    parent_modules.each { |p| category_calculation = p.category_calculation if not category_calculation }
  end
  category = category_calculation.call(values) if category_calculation
  category.to_s
end
calculate_primary_key(values) click to toggle source
# File pim.rb, line 5663
def calculate_primary_key values
  category_value = values[:category__]
  category = category(category_value) if category_value

  primary_key = category.calculate_primary_key(values) if category
  primary_key.to_s
end
categories_as_json() click to toggle source
# File pim.rb, line 5650
def categories_as_json
  as_json(Hash[all_categories(false).sort])
end
category(name) click to toggle source
# File pim.rb, line 5579
def category name
  if name.is_a? PIM::Item
    name
  else
    all_categories[name.to_s]
  end
end
copy_item(original_item, organization) click to toggle source
# File pim.rb, line 5734
def copy_item original_item, organization
  if !original_item.is_a?(Item)
    begin
      original_item = item_from_json(original_item, :init_multivalue => false)
    rescue Exception => e
      PIM.log_warn("Could not resolve class of original item in 'copy_item' (#{original_item})", e)
      return nil
    end
  end
  original_item.copy_item(organization)
end
create_category(category_hash) click to toggle source
# File pim.rb, line 5587
def create_category category_hash

  raise "Must be executed in data module" unless data_module
  raise "Must not be executed in PIM module itself" if data_module == PIM

  PIM.log_trace "create_category: #{category_hash}"

  PIM.execute_in_module(data_module) do

    name = PIM.get_value(category_hash, :name)
    simple_name = PIM.get_value(category_hash, :simple_name)
    raise "Must either specify 'name' or 'simple_name'" if name.nil? and simple_name.nil?
    unless name.nil?
      name_prefix, separator, name_suffix = name.rpartition '::'
      raise "Name must start with name of data module ('#{name_prefix}' != '#{data_module.to_s}')" if name_prefix != data_module.to_s
      category_name = name_suffix
    else
      category_name = simple_name
    end

    parent_name = PIM.get_value(category_hash, :parent)
    if parent_name
      PIM.log_trace "create_category: parent_name=#{parent_name} (exists? #{all_categories.include?(parent_name)})"
      parent_category = all_categories[parent_name]
      raise "Parent category '#{parent_name}' does not exist" if parent_category.nil?
    else
      parent_category = PIM::Item
    end

    category = data_module.const_set(category_name, Class.new(parent_category))
    data_module.module_categories[category.to_s] = category

    set_category_value(category, :label, PIM.get_value(category_hash, :label))
    set_category_value(category, :code, PIM.get_value(category_hash, :code))
    set_category_value(category, :leaf, PIM.get_value(category_hash, :leaf))
    set_category_value(category, :attribute, PIM.get_value(category_hash, :attributes))

    category

  end

end
item_from_json(json, opts = {}) click to toggle source
# File pim.rb, line 5723
def item_from_json json, opts = {}
  hash = PIM::Utils.hash_from_json json, "for item"
  category_name = hash[:category__] || hash['category__']
  return nil if is_empty?(category_name)
  category = data_module.category(category_name)
  raise "Category '#{category_name}' is not defined" if !category
  item = category.new opts
  item.from_json hash, opts
  item
end
module_categories() click to toggle source
# File pim.rb, line 5646
def module_categories
  @module_categories ||= {}
end
query_category(value) click to toggle source
# File pim.rb, line 5677
def query_category value
  category = all_categories[value.to_s]
  return category if category
  category_query_blocks = []
  category_query_blocks << @category_query if @category_query
  parent_modules.each { |p| category_query_blocks << p.category_query if p.category_query }
  category_query_blocks.each do |query_block|
    category = query_block.call(value)
    break if category
  end
  category
end
query_module_category(module_name, category_name) click to toggle source
# File pim.rb, line 5671
def query_module_category module_name, category_name
  category = PIM.execute_in_module(module_name) do
    query_category(category_name)
  end
end
set_category_value(category, method_symbol, value) click to toggle source
# File pim.rb, line 5630
def set_category_value category, method_symbol, value
  if PIM.is_array?(value)
    value.each { |v| set_category_value(category, method_symbol, v) }
  else
    category.send(method_symbol, value) unless PIM.is_empty?(value)
  end
end

Protected Instance Methods

category_calculation(&block) click to toggle source
# File pim.rb, line 5748
      def category_calculation &block
        return @category_calculation if not block
        raise "Category calculation for data model '#{self.to_s}' is already defined" if @category_calculation
#        raise "Category calculation for data model '#{self.to_s}' must have 1 method argument defined" if block.arity != 1
        @category_calculation = block
      end
category_query(&block) click to toggle source
# File pim.rb, line 5755
def category_query &block
  return @category_query if not block
  raise "Category query for data model '#{self.to_s}' is already defined" if @category_query
  @category_query = block
end