class Libis::Ingester::FileGrouper

Protected Instance Methods

process(item) click to toggle source
# File lib/libis/ingester/tasks/file_grouper.rb, line 57
def process(item)
  grouping = parameter(:group_regex)
  if grouping && Kernel.eval(parameter(:group_source)) =~ Regexp.new(grouping)
    collections = Kernel.eval(parameter(:collection_label)).to_s.split('/') rescue []
    target_parent = item.parent
    collections.each do |collection|
      sub_parent = target_parent.get_items.select do |c|
        c.is_a?(Libis::Ingester::Collection) && c.name == collection
      end.first
      unless sub_parent
        sub_parent = Libis::Ingester::Collection.new
        sub_parent.name = collection
        sub_parent.navigate = parameter(:collection_navigate)
        sub_parent.publish = parameter(:collection_publish)
        target_parent.add_item(sub_parent)
        debug 'Created new Collection item: %s', sub_parent, collection
      end
      target_parent = sub_parent
    end
    group = nil
    if parameter(:group_label) || parameter(:group_name)
      group_name = Kernel.eval(parameter(:group_name)) if parameter(:group_name)
      group_label = Kernel.eval(parameter(:group_label)) if parameter(:group_label)
      # noinspection RubyScope
      group_name ||= group_label
      group_label ||= group_name
      group = target_parent.get_items.select { |g| g.name == group_name && g.is_a?(Libis::Ingester::Division) }.first
      unless group
        group = Libis::Ingester::Division.new
        group.name = group_name
        group.label = group_label
        target_parent.add_item(group)
        debug 'Created new Division item for group: %s', group, group_label
      end
    end
    if parameter(:file_label)
      item.label = Kernel.eval(parameter(:file_label))
      debug 'Assigning label %s', item, item.label
    end
    if parameter(:file_name)
      file_name = Kernel.eval(parameter(:file_name))
      debug 'Renaming to %s', item, file_name
      item.name = file_name
    end
    if group
      debug 'Adding to group %s', item, group.name
      item = group.move_item(item)
    elsif target_parent != item.parent
      debug 'Adding to collection %s', item, target_parent.name
      item = target_parent.move_item(item)
    end
    item.save!
  end
  self.processing_item = item
end