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 4726
def self.attribute name
  PIM.active_module.attribute(name)
end
new(name, parent, params = {}) click to toggle source
# File pim.rb, line 4557
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
end
param(param, default_value) click to toggle source
# File pim.rb, line 4548
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 4571
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 4686
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 4579
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 4654
def clear_params
  @params.clear
end
custom_param(name) click to toggle source
# File pim.rb, line 4658
def custom_param name
  custom_params = param(:custom) || {}
  custom_params[name]
end
default_value() click to toggle source
# File pim.rb, line 4595
def default_value
  param :default_value
end
description() click to toggle source
# File pim.rb, line 4590
def description
  return param(:description) if has_param?(:description)
  return nil
end
has_param?(name) click to toggle source
# File pim.rb, line 4627
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 4607
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 4673
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 4603
def is_meta_attribute?
  return PIM.is_meta_attribute?(name)
end
is_multivalue?() click to toggle source
# File pim.rb, line 4669
def is_multivalue?
  [Array, Set, Hash, Struct].include?(base_class) or param(:is_multivalue)
end
label() click to toggle source
# File pim.rb, line 4585
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 4650
def merge_params params
  @params.merge! params
end
modify(&block) click to toggle source
# File pim.rb, line 4663
def modify &block
  modifier = AttributeModifier.new(@name, @params, @states)
  DataModuleObject.set_data_module(modifier, data_module)
  modifier.instance_eval(&block)
end
new(*args) click to toggle source
# File pim.rb, line 4575
def new *args
  base_class.new *args
end
param(name) click to toggle source
# File pim.rb, line 4634
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 4641
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 4613
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 4619
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 4677
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 4599
def type_name
  param :type_name
end

Protected Instance Methods

parent_params() click to toggle source
# File pim.rb, line 4743
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 4732
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 4748
def super_attribute
  data_module.attribute @parent if @parent.is_a? Symbol
end