module PIM::Attributes

Constants

DEFAULT_ATTRIBUTE_PARAMETERS

Default definition per type when creating attribute from hash. Used to set default parameters per type, unless set in the definition. Note: Please also adapt the corresponding code in Java ‘com.lansa.lax.pim.datamodel.Attribute’!

DEFAULT_TO_XML
MANDATORY_ATTRIBUTE_DEFINITIONS

Mandatory definition per type when creating attribute from hash. Used to set a base_class different from String - which is the default - and other mandatory params. Note: Please also adapt the corresponding code in Java ‘com.lansa.lax.pim.datamodel.Attribute’!

Public Instance Methods

add_param(params, name, attribute_hash, value_key) click to toggle source
# File pim.rb, line 4351
def add_param params, name, attribute_hash, value_key
  value = PIM.get_value(attribute_hash, value_key)
  params[PIM.to_sym(name)] = value unless PIM.is_empty?(value)
end
additional_attributes(attribute_names_per_module) click to toggle source
# File pim.rb, line 4397
def additional_attributes attribute_names_per_module
  additional_attributes = {}
  attribute_names_per_module.each_pair do |additional_module_name, attribute_names|
    additional_module = PIM.get_module(additional_module_name)
    if !additional_module
      log_warn "Additional module '#{additional_module_name}' is not defined"
      next
    end
    next if is_empty?(attribute_names)
    attributes = additional_module.included_attributes(attribute_names)
    additional_attributes[additional_module.to_s] = attributes if !attributes.empty?
  end
  additional_attributes
end
additional_attributes_as_json(attribute_names_per_module) click to toggle source
# File pim.rb, line 4412
def additional_attributes_as_json attribute_names_per_module
  as_json(Hash[additional_attributes(attribute_names_per_module).sort])
end
all_attributes() click to toggle source
# File pim.rb, line 4356
def all_attributes
  attributes = {}
  attributes.merge!(@module_attributes) if @module_attributes
  add_parent_objects(attributes) { |p| p.module_attributes }
  attributes
end
all_state_dependent_attributes(attribute) click to toggle source
# File pim.rb, line 4363
def all_state_dependent_attributes attribute
  attribute_names = []
  attribute_names.concat(@module_state_dependent_attributes[attribute]) if @module_state_dependent_attributes
  add_parent_objects(attribute_names) { |p| p.module_state_dependent_attributes[attribute] }
  attribute_names
end
attribute(name, parent = nil, label = nil, default_value = nil, &block) click to toggle source
# File pim.rb, line 4238
def attribute name, parent = nil, label = nil, default_value = nil, &block

  name = to_sym(name)
  if parent

    if module_attributes.has_key? name
      if not data_module.has_option?(:ignore_defined_attributes)
        raise "Attribute with name '#{name}' is already defined in data model #{self}"
      else
        return module_attributes[name]
      end
    end

    attribute = PIM::AttributeBuilder.build_attribute(data_module, name, parent, label, default_value, &block)
    module_attributes[name] = attribute

    attribute.states.each_value do |state|
      state[:dependent_attributes].each do |dependent_attribute|
        state_dependent_attributes = module_state_dependent_attributes[dependent_attribute]
        state_dependent_attributes << name if !state_dependent_attributes.include?(name)
      end
    end

    attribute

  elsif name.is_a? PIM::Attribute
    name
  else
    all_attributes[name]
  end

end
attributes_as_json() click to toggle source
# File pim.rb, line 4389
def attributes_as_json
  attributes = {}
  all_attributes.sort.each do |key, attribute|
    attributes[key] = attribute if !attribute.param(:exclude_from_json)
  end
  as_json(attributes)
end
check_attribute!(attribute, message = nil, ignore = false) click to toggle source
# File pim.rb, line 4378
def check_attribute! attribute, message = nil, ignore = false
  return if !attribute or attribute.is_a? Class
  attribute = attribute.name if attribute.is_a? PIM::Attribute
  if !exists_attribute?(attribute)
    return false if ignore
    message = message || "Attribute '#{attribute.to_sym}' is not defined"
    raise message
  end
  return true
end
create_attribute(attribute_hash) click to toggle source
# File pim.rb, line 4290
def create_attribute attribute_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.execute_in_module(data_module) do

    name = PIM.get_value(attribute_hash, :name)
    type_name = PIM.get_value(attribute_hash, :type_name)

    mandatory_attribute_definition = MANDATORY_ATTRIBUTE_DEFINITIONS[type_name]
    mandatory_base_class = PIM.get_value(mandatory_attribute_definition, :base_class)
    mandatory_params = PIM.get_value(mandatory_attribute_definition, :params)

    default_params = DEFAULT_ATTRIBUTE_PARAMETERS[type_name]

    if type_name == 'Composite'
      members = PIM.symbolized_array(PIM.get_value(attribute_hash, :members) || [])
      base_class = PIM::Composite.new(*members)
      DataModuleObject.set_data_module(base_class, data_module)
    elsif type_name == 'MultiDimensional'
      key_members = PIM.symbolized_array(PIM.get_value(attribute_hash, :key_members) || [])
      value_members = PIM.symbolized_array(PIM.get_value(attribute_hash, :value_members) || [])
      base_class = PIM::MultiDimensional.new(data_module, key_members, value_members)
      DataModuleObject.set_data_module(base_class, data_module)
    else
      base_class = mandatory_base_class || PIM.get_value(attribute_hash, :base_class) || String
      base_class = Object.const_get(base_class) if base_class.is_a?(String)
    end

    attribute = attribute(name, base_class)

    # Set default parameters
    params = {}
    params.merge!(default_params) if default_params

    # Set defined parameters
    defined_params = PIM.get_value(attribute_hash, :params)
    if defined_params
      params.merge!(PIM.symbolized_snakeized_hash(defined_params))
      value_class = params[:value_class]
      if value_class and value_class.is_a?(String)
        value_class = Object.const_get(value_class)
        params[:value_class] = value_class
      end
    end

    # Set mandatory parameters
    params.merge!(mandatory_params) if mandatory_params

    add_param(params, :label, attribute_hash, :label)
    add_param(params, :description, attribute_hash, :description)
    add_param(params, :type_name, attribute_hash, :type_name)

    attribute.merge_params(params) unless PIM.is_empty?(params)
    attribute

  end

end
exists_attribute?(attribute) click to toggle source
# File pim.rb, line 4286
def exists_attribute? attribute
  attribute and all_attributes.has_key?(to_sym(attribute))
end
has_attribute_states?(attribute_name) click to toggle source
# File pim.rb, line 4428
def has_attribute_states? attribute_name
  attribute = attribute(attribute_name)
  return false if !attribute
  return !attribute.states.empty?
end
included_attributes(attribute_names) click to toggle source
# File pim.rb, line 4416
def included_attributes attribute_names
  attributes = {}
  attribute_names.each do |attribute_name|
    add_included_attributes(attributes, attribute_name.to_s)
  end
  Hash[attributes.sort].values
end
included_attributes_as_json(attribute_names) click to toggle source
# File pim.rb, line 4424
def included_attributes_as_json attribute_names
  as_json(included_attributes(attribute_names))
end
modify_attribute(name, &block) click to toggle source
# File pim.rb, line 4271
def modify_attribute name, &block

  name = to_sym(name)

  attribute = module_attributes[name]
  if attribute.nil?
    if data_module.has_option?(:ignore_defined_attributes)
      raise "Attribute with name '#{name}' is not defined in data model #{self}"
    end
    return nil
  end
  attribute.modify &block

end
module_attributes() click to toggle source
# File pim.rb, line 4370
def module_attributes
  @module_attributes ||= {}
end
module_state_dependent_attributes() click to toggle source
# File pim.rb, line 4374
def module_state_dependent_attributes
  @module_state_dependent_attributes ||= Hash.new { |k,v| k[v] = [] }
end