Skip to content

Commit

Permalink
Refactored ElementalParent#add_item
Browse files Browse the repository at this point in the history
This was achieved by adding an extension to the Hash class in the form
of the #create_key_gap_at method.
  • Loading branch information
dicom committed Jan 10, 2014
1 parent 74b53ae commit be73a34
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
3 changes: 2 additions & 1 deletion lib/dicom.rb
Expand Up @@ -13,8 +13,9 @@
# in principle 'private' classes, which are mainly of interest to developers.

# Gem specific extensions:
require_relative 'dicom/extensions/string'
require_relative 'dicom/extensions/array'
require_relative 'dicom/extensions/hash'
require_relative 'dicom/extensions/string'

# General module features/settings:
require_relative 'dicom/general/version'
Expand Down
27 changes: 8 additions & 19 deletions lib/dicom/elemental_parent.rb
Expand Up @@ -24,33 +24,22 @@ module ElementalParent
def add_item(item=nil, options={})
if item
if item.is_a?(Item)
if options[:index]
if index = options[:index]
# This Item will take a specific index, and all existing Items with index higher or equal to this number will have their index increased by one.
# Check if index is valid (must be an existing index):
if options[:index] >= 0
if index >= 0
# If the index value is larger than the max index present, we dont need to modify the existing items.
if options[:index] < @tags.length
# Extract existing Hash entries to an array:
pairs = @tags.sort
@tags = Hash.new
# Change the key of those equal or larger than index and put these key,value pairs back in a new Hash:
pairs.each do |pair|
if pair[0] < options[:index]
@tags[pair[0]] = pair[1] # (Item keeps its old index)
else
@tags[pair[0]+1] = pair[1]
pair[1].index = pair[0]+1 # (Item gets updated with its new index)
end
end
if index < @tags.length
@tags = @tags.create_key_gap_at(index)
else
# Set the index value one higher than the already existing max value:
options[:index] = @tags.length
index = @tags.length
end
#,Add the new Item and set its index:
@tags[options[:index]] = item
item.index = options[:index]
@tags[index] = item
item.index = index
else
raise ArgumentError, "The specified index (#{options[:index]}) is out of range (must be a positive integer)."
raise ArgumentError, "The specified index (#{index}) is out of range (must be a positive integer)."
end
else
# Add the existing Item to this Sequence:
Expand Down
31 changes: 31 additions & 0 deletions lib/dicom/extensions/hash.rb
@@ -0,0 +1,31 @@
# Extensions to the Hash class used by the dicom gem.
#
class Hash

# Creates a gap in the integer keys at the specified index.
# This is achieved by incrementing by one all existing index keys that are equal or
# bigger than the given index.
#
# @note It is assumed that this hash features integers as keys and items as values!
# @param [Integer] index the index at which to clear
# @return [Hash] the modified self
#
def create_key_gap_at(index)
# Extract existing Hash entries to an array:
pairs = self.sort
h = Hash.new
# Change the key of those equal or larger than index and put these key,value pairs back in a new Hash:
pairs.each do |pair|
if pair[0] < index
# The key keeps its old index:
h[pair[0]] = pair[1]
else
# The key (and the value Item) gets its index incremented by one:
h[pair[0]+1] = pair[1]
pair[1].index = pair[0]+1
end
end
h
end

end

0 comments on commit be73a34

Please sign in to comment.