class PIM::DataModelManager

Public Class Methods

add_attribute(out, id, type, params) click to toggle source
# File data-model-manager.rb, line 118
def self.add_attribute(out, id, type, params)

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

  # Set mandatory base_class according to type
  type = mandatory_base_class || type || String
  type = Object.const_get(type) if type.is_a?(String)

  # Set mandatory params according to type
  params ||= {}
  params.merge!(mandatory_params) if mandatory_params

  add_line out, %Q(attributes.create :#{id}, #{type}, #{params.inspect})

end
add_category(out, id, parent, label) click to toggle source
# File data-model-manager.rb, line 144
def self.add_category(out, id, parent, label)
  add_line out, %Q(categories.create "#{id}", "#{parent}", :label => "#{label}")
end
add_line(output, line) click to toggle source
# File data-model-manager.rb, line 114
def self.add_line(output, line)
  output.write %Q(    #{line}\n)
end
add_section(out, id, sec_id, label) click to toggle source
# File data-model-manager.rb, line 160
def self.add_section(out, id, sec_id, label)
  add_line out, %Q(layouts["#{id}"].sections.create #{sec_id.inspect}, "#{label}")
end
create_attributes_code(out, attributes) click to toggle source
# File data-model-manager.rb, line 27
def self.create_attributes_code out, attributes
  for attribute in attributes

    id = attribute["id"]
    type = attribute["type"]
    action = attribute["action"]

    case action
      when "ADD" then add_attribute(out, id, type, makeRubyParams(attribute))
      when "UPDATE" then update_attribute(out, id, makeRubyParams(attribute))
      when "REMOVE" then delete_attribute(out, id)
    end

  end
end
create_categories_code(out, categories) click to toggle source
# File data-model-manager.rb, line 43
def self.create_categories_code out, categories
  for category in categories

    id = category["id"]
    label = category["label"]
    parent = category["parent"]
    action = category["action"]

    case action
      when "ADD" then add_category(out, id, parent, label)
      when "UPDATE" then update_category(out, id, parent, label)
      when "REMOVE" then delete_category(out, id)
    end

    cat_attributes = category["attributes"] || []
    for cat_attribute in cat_attributes
      cat_action = (cat_attribute["action"] || "").downcase
      if %w(add remove).include?(cat_action)
        attr_id = cat_attribute["id"]
        modify_category_attributes(out, id, cat_action, attr_id)
      end
    end

  end
end
create_customization_ruby_code(yaml) click to toggle source
# File data-model-manager.rb, line 10
def self.create_customization_ruby_code yaml

  datamodel_customization = YAML.load(yaml)

  attributes = datamodel_customization["attributes"] || []
  categories = datamodel_customization["categories"] || []
  layouts = datamodel_customization["layouts"] || []

  output = StringIO.new

  create_attributes_code(output, attributes)
  create_categories_code(output, categories)
  create_layouts_code(output, layouts)

  return output.string
end
create_layouts_code(out, layouts) click to toggle source
# File data-model-manager.rb, line 69
def self.create_layouts_code out, layouts
  for layout in layouts

    id = layout["id"]
    label = layout["label"]
    action = layout["action"]
    sections = layout["sections"]
    order = layout["order"]

    case action
      when "UPDATE" then create_sections_code(out, id, sections)
      when "SORT" then sort_layout(out, id, order) unless order.nil?
    end

  end
end
create_sections_code(out, id, sections) click to toggle source
# File data-model-manager.rb, line 86
def self.create_sections_code out, id, sections
  for section in sections

    sec_action = section["action"]
    sec_id = section["id"]
    sec_label = section["label"]
    sec_label = sec_label.nil? ? "" : sec_label
    sec_order = section["order"]

    case sec_action
      when "ADD" then add_section(out, id, sec_id, sec_label)
      when "UPDATE" then update_section(out, id, sec_id, sec_label)
      when "REMOVE" then remove_section(out, id, sec_id)
      when "SORT" then sort_section(out, id, sec_id, sec_order) unless sec_order.nil?
    end

    sec_attributes = section["attributes"] || []
    for sec_attribute in sec_attributes
      sec_action = (sec_attribute["action"] || "").downcase
      if %w(add remove).include?(sec_action)
        sec_attr_id = sec_attribute["id"]
        modify_section_atributes(out, id, sec_id, sec_action, sec_attr_id)
      end
    end

  end
end
delete_attribute(out, id) click to toggle source
# File data-model-manager.rb, line 140
def self.delete_attribute(out, id)
  add_line out, %Q(attributes[:#{id}].delete)
end
delete_category(out, id) click to toggle source
# File data-model-manager.rb, line 152
def self.delete_category(out, id)
  add_line out, %Q(categories["#{id}"].delete)
end
makeRubyParams(attribute) click to toggle source
# File data-model-manager.rb, line 184
def self.makeRubyParams(attribute)
  label = attribute["label"] || ""
  params = PIM::Utils.symbolized_hash(attribute["params"] || {})
  ruby_params = { :label => label }.merge(params)
  return ruby_params
end
modify_category_attributes(out, id, operation, attr_id) click to toggle source
# File data-model-manager.rb, line 156
def self.modify_category_attributes(out, id, operation, attr_id)
  add_line out, %Q(categories["#{id}"].attributes.#{operation} :#{attr_id})
end
modify_section_atributes(out, id, sec_id, operation, sec_attr_id) click to toggle source
# File data-model-manager.rb, line 176
def self.modify_section_atributes(out, id, sec_id, operation, sec_attr_id)
  add_line out, %Q(layouts["#{id}"].sections[#{sec_id.inspect}].attributes.#{operation} :#{sec_attr_id})
end
new(data_model) click to toggle source
# File data-model-manager.rb, line 589
def initialize data_model
  @attribute_manager = AttributeManager.new data_model
  @category_manager = CategoryManager.new data_model
  @layout_manager = LayoutManager.new data_model
end
remove_section(out, id, sec_id) click to toggle source
# File data-model-manager.rb, line 168
def self.remove_section(out, id, sec_id)
  add_line out, %Q(layouts["#{id}"].sections[#{sec_id.inspect}].delete)
end
sort_layout(out, id, order) click to toggle source
# File data-model-manager.rb, line 180
def self.sort_layout(out, id, order)
  add_line out, %Q(layouts["#{id}"].sort_order #{order})
end
sort_section(out, id, sec_id, sec_order) click to toggle source
# File data-model-manager.rb, line 172
def self.sort_section(out, id, sec_id, sec_order)
  add_line out, %Q(layouts["#{id}"].sections[#{sec_id.inspect}].sort_order #{sec_order})
end
update_attribute(out, id, params) click to toggle source
# File data-model-manager.rb, line 136
def self.update_attribute(out, id, params)
  add_line out, %Q(attributes[:#{id}].update nil, #{params.inspect})
end
update_category(out, id, parent, label) click to toggle source
# File data-model-manager.rb, line 148
def self.update_category(out, id, parent, label)
  add_line out, %Q(categories["#{id}"].update "#{parent}", :label => "#{label}")
end
update_section(out, id, sec_id, label) click to toggle source
# File data-model-manager.rb, line 164
def self.update_section(out, id, sec_id, label)
  add_line out, %Q(layouts["#{id}"].sections[#{sec_id.inspect}].update "#{label}")
end

Public Instance Methods

attributes() click to toggle source
# File data-model-manager.rb, line 595
def attributes
  @attribute_manager
end
categories() click to toggle source
# File data-model-manager.rb, line 603
def categories
  @category_manager
end
layouts() click to toggle source
# File data-model-manager.rb, line 599
def layouts
  @layout_manager
end