class PIM::Item

Add system attributes

Constants

READ_ONLY_ATTRIBUTES

Public Class Methods

default_item(organization = nil, user = nil) click to toggle source
# File pim.rb, line 5813
def self.default_item organization = nil, user = nil
  result = self.new
  self.all_attributes.each do |a|
    attribute = data_module.attribute(a)
    result.instance_variable_set("@#{a}", attribute.default_value) if attribute.default_value
  end
  result
end
from_json(json, opts = {}) click to toggle source

Deprecated: Use “<DATA_MODULE>.item_from_json” instead!

# File pim.rb, line 5809
def self.from_json json, opts = {}
  PIM.active_module.item_from_json json, opts
end
new(opts = {}) click to toggle source
# File pim.rb, line 5822
def initialize opts = {}
  init_item(opts)
  if has_option?(:init_multivalue, !data_module.has_option?(:no_init_multivalue))
    self.class.all_attributes.each do |a|
      create_multi_value(a)
    end
  end
end

Protected Class Methods

create_attribute_accessor(attribute) click to toggle source
# File pim.rb, line 6347
def create_attribute_accessor attribute
  define_method(to_sym(attribute)) do
    get_attribute_value(attribute)
  end
  define_method("#{attribute}=".to_sym) do |value|
    set_attribute_value(attribute, value)
  end
end
inherited(subclass) click to toggle source
# File pim.rb, line 6318
def inherited subclass

  if @class_leaf == true
    PIM.log_error "Class '#{subclass}' tried to inherit from class '#{self}' which is marked as 'leaf'; '#{subclass}' will not be defined!"
    return
  end

  subclass.initialize_class self

end
initialize_class(parent = nil) click to toggle source
# File pim.rb, line 6329
def initialize_class parent = nil
  DataModuleClass.set_data_module self
  @class_attributes = []
  @class_attribute_status = {}
  @class_hierarchies = []
  @model_defined = true
  if parent
    @class_parent = parent if parent < PIM::Item
    if self.to_s.include?(data_module.to_s)
      data_module.module_categories[self.to_s] = self
    elsif PIM.is_empty?(self.name)
      log_trace "Anonymous category class '#{self.to_s}' created but not added to data module '#{data_module.to_s}'"
    else
      log_warn "Category class '#{self.to_s}' cannot be added to data module '#{data_module.to_s}'"
    end
  end
end

Public Instance Methods

[](attribute) click to toggle source
# File pim.rb, line 5831
def [] attribute
  get_attribute_value attribute
end
[]=(attribute, value) click to toggle source
# File pim.rb, line 5835
def []= attribute, value
  set_attribute_value attribute, value
end
add_readonly_attribute(attribute) click to toggle source
# File pim.rb, line 5957
def add_readonly_attribute attribute
  @readonlyAttributes__ ||= []
  attribute = attribute.to_s
  @readonlyAttributes__ << attribute if !@readonlyAttributes__.include?(attribute)
end
add_readonly_attributes(*attributes) click to toggle source
# File pim.rb, line 5963
def add_readonly_attributes *attributes
  attributes.each do |attribute|
    add_readonly_attribute attribute
  end
end
after_store(old_item = nil, opts = {}) click to toggle source

empty by default

# File pim.rb, line 5986
def after_store old_item = nil, opts = {}
  []
end
as_json(opts = {}) click to toggle source
# File pim.rb, line 5924
def as_json opts = {}
  hash = {
    "category__" => self.class.to_s
  }
  instance_variables.sort.each do |var|
    key = var.to_s[1..-1]
    next if hash.include?(key) or (key.start_with?('__') and key.end_with?('__'))
    value = instance_variable_get(var)
    if opts[:include_empty] || !value.nil?
      value = value_as_json(key, value)
      hash[key] = value if opts[:include_empty] || !value.nil?
    end
  end
  @unknownAttributeValues__.sort_by { |k, v| k }.each do |(k, v)|
    next if hash.include?(k) or (!opts[:include_empty] && v.nil?)
    v = PIM::Utils.as_json(v)
    hash[k.to_s] = v if opts[:include_empty] || !v.nil?
  end
  hash
end
before_store(old_item, called_from_import) click to toggle source

empty by default

# File pim.rb, line 5981
def before_store old_item, called_from_import
  []
end
calculate_primary_key() click to toggle source
# File pim.rb, line 5951
def calculate_primary_key
  hash = Hash[instance_variables.map { |name| [name[1..name.size], instance_variable_get(name)] } ]
  hash[:category__] = self.class.to_s
  PIM.calculate_primary_key(hash)
end
call_after_store?(old_item = nil, opts = {}) click to toggle source

false by default

# File pim.rb, line 5991
def call_after_store? old_item = nil, opts = {}
  false
end
copy_item(organization) click to toggle source
# File pim.rb, line 5886
def copy_item organization

  copy = self.dup

  # Set primary key attributes to nil, if any
  class_primary_key = self.class.class_primary_key
  if class_primary_key and class_primary_key.attributes
    class_primary_key.attributes.each do |attribute|
      variable = "@#{attribute}"
      if copy.instance_variable_defined?(variable)
        copy.instance_variable_set(variable, nil)
      end
    end
  end

  # Set all optional primary key part attributes to nil
  data_module.all_attributes.each_pair do |key, attribute|
    next if not attribute.param :potential_primary_key_part
    variable = "@#{key}"
    if copy.instance_variable_defined?(variable)
      copy.instance_variable_set(variable, nil)
    end
  end

  copy

end
dup() click to toggle source
Calls superclass method
# File pim.rb, line 5914
def dup
  duplicate = super
  duplicate.instance_variables.each do |v|
    value = duplicate.instance_variable_get(v)
    value = PIM.dup_value(value)
    duplicate.instance_variable_set(v, value)
  end
  duplicate
end
from_json(hash, opts = {}) click to toggle source
# File pim.rb, line 5945
def from_json hash, opts = {}
  init_item(opts)
  @unknownAttributeValues__ = set_instance_values(data_module, self.class, hash)
  @additionalCategories__.sort!.uniq! if !is_empty?(@additionalCategories__)
end
get(attribute, opts = {})
Alias for: get_attribute_value
get_attribute_value(attribute, opts = {}) click to toggle source
# File pim.rb, line 5839
def get_attribute_value attribute, opts = {}
  # Returns true, if either this opts or the item's opts have the :create_multivalue option set to 'true'
  # Defaults to data_model 'no_create_multivalue' flag
  create_multivalue = PIM::Utils.is_opt?(opts,
                                         :create_multivalue,
                                         has_option?(:create_multivalue, !data_module.has_option?(:no_create_multivalue)))
  var = "@#{attribute}"
  if instance_variable_defined?(var)
    value = instance_variable_get(var)
    value = create_multi_value(attribute) if create_multivalue and value.nil?
  elsif has_attribute?(attribute)
    value = create_multi_value(attribute) if create_multivalue
  else
    value = get_unknown_attribute_value(attribute)
  end
  value
end
Also aliased as: get
has?(attribute)
has_attribute?(attribute) click to toggle source
# File pim.rb, line 5879
def has_attribute? attribute
  attribute = to_sym(attribute)
  return true if self.class.has_attribute?(attribute)
  return true if @additionalCategoryAttributes__.include?(attribute)
  return false
end
has_attribute_value?(attribute) click to toggle source
# File pim.rb, line 5873
def has_attribute_value? attribute
  var = "@#{attribute}"
  return instance_variable_defined?("@#{attribute}")
end
Also aliased as: has?
hierarchy_graph(hierarchy_name = nil, opts = {}) click to toggle source

empty by default

# File pim.rb, line 5996
def hierarchy_graph hierarchy_name = nil, opts = {}
  nil
end
set(attribute, value)
Alias for: set_attribute_value
set_attribute_value(attribute, value) click to toggle source
# File pim.rb, line 5858
def set_attribute_value attribute, value
  if READ_ONLY_ATTRIBUTES.include?(attribute.to_s)
    old_value = get_attribute_value(attribute)
  elsif has_attribute?(attribute)
    var = "@#{attribute}"
    old_value = instance_variable_defined?(var) ? instance_variable_get(var) : nil
    instance_variable_set("@#{attribute}", value)
    update_additional_category(attribute, old_value, value)
  else
    old_value = set_unknown_attribute_value(attribute, value)
  end
  old_value
end
Also aliased as: set
set_readonly_attribute(attribute) click to toggle source
# File pim.rb, line 5969
def set_readonly_attribute attribute
  set_readonly_attributes [attribute]
end
set_readonly_attributes(*attributes) click to toggle source
# File pim.rb, line 5973
def set_readonly_attributes *attributes
  @readonlyAttributes__ = []
  attributes.each do |attribute|
    add_readonly_attribute attribute
  end
end