class PIM::Attribute

Attributes

model_defined[RW]
modification_status[RW]
name[RW]
parent[RW]

Public Class Methods

attribute(name) click to toggle source

Deprecated: Use “<DATA_MODULE>.attribute(name)” instead!

# File pim.rb, line 5076
def self.attribute name
  PIM.active_module.attribute(name)
end
new(name, parent, params = {}) click to toggle source
# File pim.rb, line 4867
def initialize name, parent, params = {}, states = {}

  @name = name
  @model_defined = false

  if parent.is_a? PIM::AttributeTemplate
    @parent = parent.base_class
    @params = PIM.deep_merge_if_absent(params, parent.params)
    @states = PIM.deep_merge_if_absent(states, parent.states)
  else
    @parent = parent
    @params = params
    @states = states
  end

  verify

end
param(param, default_value) click to toggle source
# File pim.rb, line 4858
def self.param param, default_value
  define_method param do |value = default_value|
    @params[param] = value
  end
  private param
end

Public Instance Methods

==(other) click to toggle source
# File pim.rb, line 4898
def ==(other)
  return other && (params && (params.eql? other.params)) && (parent == other.parent) && (name == other.name)
end
as_json() click to toggle source
# File pim.rb, line 5036
def as_json

  hash = {
    :name => @name.to_s,
    :baseClass => base_class
  }
  hash[:label] = param(:label) if has_param?(:label)
  hash[:description] = param(:description) if has_param?(:description)
  hash[:defaultValue] = default_value if has_param?(:default_value)
  hash[:typeName] = type_name if has_param?(:type_name)
  hash[:directPublications] = param(:direct_publications) if has_param?(:direct_publications)
  hash[:modification_status] = @modification_status if @modification_status
  hash[:model_defined] = @model_defined
  if base_class.respond_to?(:as_json)
    hash.merge!(base_class.as_json)
  end

  params_hash = Hash.new
  params.each_pair do |k,v|
    next if [:label, :default_value, :description, :type_name, :direct_publications].include?(k)
    next if v.is_a? Proc
    k = PIM::Utils.camelize(k.to_s).to_sym
    if k == :custom
      custom_values = {}
      v.each_pair do |custom_key, custom_value|
        custom_key = PIM::Utils.camelize(custom_key.to_s).to_sym
        custom_values[custom_key] = param_as_json(custom_value)
      end
      params_hash[k] = custom_values
    else
      params_hash[k] = param_as_json(v)
    end
  end

  hash[:params] = params_hash if !params_hash.empty?
  hash

end
base_class() click to toggle source
# File pim.rb, line 4906
def base_class
  return @parent if @parent.is_a? Class
  return super_attribute.base_class if super_attribute
  return nil
end
clear_params() click to toggle source
# File pim.rb, line 4981
def clear_params
  @params.clear
end
custom_param(name) click to toggle source
# File pim.rb, line 4985
def custom_param name
  custom_params = param(:custom) || {}
  custom_params[name]
end
default_value() click to toggle source
# File pim.rb, line 4922
def default_value
  param :default_value
end
description() click to toggle source
# File pim.rb, line 4917
def description
  return param(:description) if has_param?(:description)
  return nil
end
has_param?(name) click to toggle source
# File pim.rb, line 4954
def has_param? name
  return true if PIM.has_hash_key?(@params, name)
  return true if not super_attribute.nil? and super_attribute.has_param?(name)
  return true if PIM.has_hash_key?(parent_params, name)
  return false
end
has_state?(name) click to toggle source
# File pim.rb, line 4934
def has_state? name
  return true if state(name)
  return true if super_attribute and super_attribute.state(name)
  return false
end
has_sub_attributes?() click to toggle source
# File pim.rb, line 5023
def has_sub_attributes?
  return param(:value_attribute) != nil || (base_class && base_class.respond_to?(:sub_attributes))
end
is_meta_attribute?() click to toggle source
# File pim.rb, line 4930
def is_meta_attribute?
  return PIM.is_meta_attribute?(name)
end
is_multivalue?() click to toggle source
# File pim.rb, line 5019
def is_multivalue?
  [Array, Set, Hash, Struct].include?(base_class) or param(:is_multivalue)
end
label() click to toggle source
# File pim.rb, line 4912
def label
  return param(:label) if has_param?(:label)
  return @name.to_s
end
merge_params(params) click to toggle source
# File pim.rb, line 4977
def merge_params params
  @params.merge! params
end
modify(&block) click to toggle source
# File pim.rb, line 4990
def modify &block
  modifier = AttributeModifier.new(@name, @params, @states)
  DataModuleObject.set_data_module(modifier, data_module)
  modifier.instance_eval(&block)

  # Determine all attributes from which at least
  # on state of the current attribute is dependent
  all_state_dependent_attributes = Set.new
  @states.each_value do |state|
    all_state_dependent_attributes.merge(state[:dependent_attributes])
    state[:dependent_attributes].each do |dependent_attribute|
      state_dependent_attributes = data_module.module_state_dependent_attributes[dependent_attribute]
      state_dependent_attributes << @name unless state_dependent_attributes.include?(@name)
    end
  end

  # Loop over each entry of the map which maps an attribute (resp. its name)
  # to an array of attribute names which has a dependency to that attribute.
  data_module.module_state_dependent_attributes.each do |k, v|
    # Remove the current attribute name from the array unless dependency still
    # exists.
    v.delete(@name) unless all_state_dependent_attributes.include? k
  end

  # Remove all entries from the map where no other attribute (states) depends on
  data_module.module_state_dependent_attributes.delete_if { |k, v| v.empty? }

end
new(*args) click to toggle source
# File pim.rb, line 4902
def new *args
  base_class.new *args
end
param(name) click to toggle source
# File pim.rb, line 4961
def param name
  return PIM.get_hash_value(@params, name) if PIM.has_hash_key?(@params, name)
  return super_attribute.param(name) unless super_attribute.nil?
  return PIM.get_hash_value(parent_params, name) if PIM.has_hash_key?(parent_params, name)
  return nil
end
params() click to toggle source
# File pim.rb, line 4968
def params
  params = {}
  params.merge!(@params)
  params.merge!(super_attribute.params) { |key, v1, v2| v1 } if super_attribute
  params.merge!(parent_params) { |key, v1, v2| v1 } if parent_params
  params.delete_if { |key, value| !value }
  params
end
state(name) click to toggle source
# File pim.rb, line 4940
def state name
  return @states[name] if @states.has_key? name
  return super_attribute.state(name) if super_attribute
  return nil
end
states() click to toggle source
# File pim.rb, line 4946
def states
  states = {}
  states.merge!(@states)
  states.merge!(super_attribute.states) { |key, v1, v2| v1 } if super_attribute
  states.delete_if { |key, value| value.nil? }
  states
end
sub_attributes() click to toggle source
# File pim.rb, line 5027
def sub_attributes
  if param(:value_attribute)
    return [ param(:value_attribute) ]
  elsif base_class and base_class.respond_to?(:sub_attributes)
    return base_class.sub_attributes
  end
  return []
end
type_name() click to toggle source
# File pim.rb, line 4926
def type_name
  param :type_name
end

Protected Instance Methods

parent_params() click to toggle source
# File pim.rb, line 5093
def parent_params
  return @parent.params if @parent.is_a?(Class) and @parent.respond_to?(:params)
  return nil
end
set_data_module(data_module) click to toggle source
Calls superclass method PIM::DataModuleObject::set_data_module
# File pim.rb, line 5082
def set_data_module data_module
  super
  data_module.check_attribute!(@parent)
  data_module.check_attribute!(@params[:value_class]) if @params.has_key?(:value_class)
  if @parent.is_a?(Class) and @parent.respond_to?(:sub_attributes)
    @parent.sub_attributes.each do |sub_attribute|
      data_module.check_attribute!(sub_attribute, "Sub attribute '#{sub_attribute}' used in attribute '#{name}' is not defined")
    end
  end
end
super_attribute() click to toggle source
# File pim.rb, line 5098
def super_attribute
  data_module.attribute @parent if @parent.is_a? Symbol
end