class PIM::MultiDimensional

Public Class Methods

as_json() click to toggle source
# File pim.rb, line 3850
def self.as_json
  hash = {
    :baseClass => Array,
  }
  hash[:keyMembers] = []
  self.key_members.each do |m|
    hash[:keyMembers] << m
  end
  hash[:valueMembers] = []
  self.value_members.each do |m|
    hash[:valueMembers] << m
  end
  hash
end
from_json(json) click to toggle source
# File pim.rb, line 3845
def self.from_json json
  multi_dimensional = self.new json
  multi_dimensional
end
get_or_create_composite_class(data_module, arg) click to toggle source
# File pim.rb, line 3900
def self.get_or_create_composite_class data_module, arg

  if arg.is_a? Array
    attribute_class = Composite.new(*arg)
    DataModuleObject.set_data_module(attribute_class, data_module)
  elsif arg.is_a? Symbol
    attribute = data_module.attribute(arg)
    raise "Attribute '#{arg}' is not defined" if !attribute
    raise "Attribute '#{arg}' must be of class Composite" if !(attribute.base_class <= Composite)
    attribute_class = attribute.base_class
  else
    raise "Argument '#{arg}' must either be a list of attributes or a single attribute of type Composite"
  end

  return attribute_class
end
key_class() click to toggle source
# File pim.rb, line 3813
def self.key_class
  self::KEY_CLASS
end
key_members() click to toggle source
# File pim.rb, line 3809
def self.key_members
  self::KEY_MEMBERS
end
name=(name) click to toggle source
# File pim.rb, line 3921
def self.name= name
  @name = name
  self.key_class.name = "#{@name}::KEY_CLASS" if @name and self.key_class.name.nil?
  self.value_class.name = "#{@name}::VALUE_CLASS" if @name and self.value_class.name.nil?
end
new(data_module, keys, values, &block) click to toggle source
# File pim.rb, line 3782
def self.new data_module, keys, values, &block

  key_class = get_or_create_composite_class data_module, keys
  value_class = get_or_create_composite_class data_module, values

  # Check uniqueness of key and value members
  key_members = key_class.members
  value_members = value_class.members
  unless (key_members & value_members).empty?
    raise "Key and value members for multi dimensional class must be unique (#{key_members & value_members})"
  end

  klass = Class.new self do
    def self.new(*args, &block)
      subclass_new(*args, &block)
    end
    const_set :KEY_MEMBERS, key_members
    const_set :KEY_CLASS, key_class
    const_set :VALUE_MEMBERS, value_members
    const_set :VALUE_CLASS, value_class
  end

  klass.module_exec(&block) if block

  return klass
end
Also aliased as: subclass_new
new(*args) click to toggle source
# File pim.rb, line 3927
def initialize *args
  @hash = Hash.new { |h,k| h[k] = value_class.new }
  if args.size == 1 and PIM::Utils.is_array?(args[0])
    from_array args[0]
  elsif args.size == 1 and PIM::Utils.is_hash?(args[0])
    from_hash args[0]
  elsif args.size == 1 and args[0].is_a? String
    from_json args[0]
  elsif !args.empty?
    from_array args
  end
end
params() click to toggle source
# File pim.rb, line 3865
def self.params
  @params ||= {
    :type_name => "MultiDimensional",
    :is_multivalue => true
  }.freeze
  @params
end
sub_attributes() click to toggle source
# File pim.rb, line 3825
def self.sub_attributes
  [].concat(self.key_members).concat(self.value_members)
end
subclass_new(data_module, keys, values, &block)
Alias for: new
to_s() click to toggle source
Calls superclass method
# File pim.rb, line 3917
def self.to_s
  self.name || @name || super
end
validate_each(old_value, new_value, &validate_sub_attribute) click to toggle source
# File pim.rb, line 3873
def self.validate_each old_value, new_value, &validate_sub_attribute

  # Apply rules for all elements of new value
  new_value.each_pair do |new_key_hash, new_value_hash|

    path_index = new_key_hash.to_h

    # Determine old value
    old_key_hash = old_value.class <= self and !old_value.empty? and old_value.has_key?(new_key_hash) ? new_key_hash : nil
    old_value_hash = old_key_hash ? old_value[old_key_hash] : nil

    # Validate values for each key and value member
    key_members.each do |member|
      sub_old_value = old_key_hash ? ({ member => old_key_hash[member] }) : nil
      sub_new_value = { member => new_key_hash[member] }
      validate_sub_attribute.call member, sub_old_value, sub_new_value, path_index, member
    end
    value_members.each do |member|
      sub_old_value = old_value_hash ? ({ member => old_value_hash[member] }) : nil
      sub_new_value = { member => new_value_hash[member] }
      validate_sub_attribute.call member, sub_old_value, sub_new_value, path_index, member
    end

  end

end
value_class() click to toggle source
# File pim.rb, line 3821
def self.value_class
  self::VALUE_CLASS
end
value_members() click to toggle source
# File pim.rb, line 3817
def self.value_members
  self::VALUE_MEMBERS
end

Public Instance Methods

==(other) click to toggle source
# File pim.rb, line 3966
def == other
  return true if equal? other
  return to_h == other.to_h if other.is_a? MultiDimensional
  return to_h == other.to_hash if PIM::Utils.is_hash?(other)
  return to_a == other.to_ary if PIM::Utils.is_array?(other)
  return false
end
[](*keys) click to toggle source
# File pim.rb, line 3949
def [] *keys
  k = key_class.new keys
  @hash[k]
end
[]=(*args) click to toggle source
# File pim.rb, line 3954
def []= *args
  keys = args.first(args.size - 1)
  k = key_class.new *keys
  v = args[args.size - 1]
  if PIM::Utils.is_array?(v)
    v = value_class.new v
  elsif PIM::Utils.is_hash?(v)
    v = value_class.new v
  end
  @hash[k] = v
end
as_json() click to toggle source
# File pim.rb, line 4037
def as_json
  array = []
  each_pair do |k, v|
    element = {}
    k = k.as_json(true)
    element.merge!(k) if not k.nil?
    v = v.as_json if not v.nil?
    element.merge!(v) if not v.nil?
    array << element
  end
  array
end
data_module() click to toggle source
# File pim.rb, line 3940
def data_module
  self.class.data_module
end
delete(*keys) click to toggle source
# File pim.rb, line 3979
def delete *keys
  k = key_class.new keys
  @hash.delete k
end
dup() click to toggle source
# File pim.rb, line 3944
def dup
  duplicate = self.class.new(to_h)
  duplicate
end
each(&block) click to toggle source
# File pim.rb, line 3984
def each &block
  @hash.each &block
end
each_pair(&block) click to toggle source
# File pim.rb, line 3988
def each_pair &block
  @hash.each_pair &block
end
empty?() click to toggle source
# File pim.rb, line 3992
def empty?
  return true if @hash.empty?
  @hash.each_pair do |k, v|
    return false if !k.empty? or !v.empty?
  end
  return true
end
has_key?(*keys) click to toggle source
# File pim.rb, line 3974
def has_key? *keys
  k = key_class.new keys
  @hash.has_key?(k)
end
inspect() click to toggle source
# File pim.rb, line 4000
def inspect
  @hash.inspect
end
key_class() click to toggle source
# File pim.rb, line 3833
def key_class
  self.class.key_class
end
key_members() click to toggle source
# File pim.rb, line 3829
def key_members
  self.class.key_members
end
length() click to toggle source
# File pim.rb, line 4004
def length
  @hash.length
end
size() click to toggle source
# File pim.rb, line 4008
def size
  @hash.size
end
to_a() click to toggle source
# File pim.rb, line 4012
def to_a
  array = []
  each_pair do |k, v|
    element = {}
    element.merge!(k.to_h)
    element.merge!(v.to_h)
    array << element
  end
  array
end
to_h() click to toggle source
# File pim.rb, line 4023
def to_h
  hash = {}
  each_pair do |k, v|
    k = k.to_h
    v = v.to_h
    hash[k] = v
  end
  hash
end
to_s() click to toggle source
# File pim.rb, line 4033
def to_s
  @hash.to_s
end
value_class() click to toggle source
# File pim.rb, line 3841
def value_class
  self.class.value_class
end
value_members() click to toggle source
# File pim.rb, line 3837
def value_members
  self.class.value_members
end