Skip to content

Commit

Permalink
Simplified rank-related methods a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
LocoDelAssembly committed Oct 4, 2013
1 parent e64f545 commit 74ef757
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
7 changes: 2 additions & 5 deletions app/models/nomenclatural_rank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ class NomenclaturalRank
COMMON = true

def self.top_rank(rank)
all = rank.descendants
all.select!{|r| !r.parent_rank.nil?}
all.each do |r|
return r if not all.include?(r.parent_rank)
end
all = rank.descendants
all.detect { |r| !(r.parent_rank.nil? or all.include?(r.parent_rank)) }
end


Expand Down
21 changes: 8 additions & 13 deletions lib/ranks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ module Ranks
# TODO: check this now that Ranks moved to initializers.

# Returns a NomenclaturalRank Class, the highest assignable for the rank Class passed.
def self.top_rank(rank)
all = rank.descendants
all.select!{|r| !r.parent_rank.nil?}
all.each do |r|
return r if not all.include?(r.parent_rank)
end
end
def self.top_rank(rank)
all = rank.descendants
all.detect { |r| !(r.parent_rank.nil? or all.include?(r.parent_rank)) }
end

# Returns an ordered Array of NomenclaturalRanks for all direct descendants of the provided base Class
def self.ordered_ranks_for(rank)
Expand All @@ -25,13 +22,11 @@ def self.ordered_ranks_for(rank)
all.select!{|r| !r.parent_rank.nil?}
ordered.push(top)
return [] if all.size == 0

# This sort algorithim is terrible, it could be optimized.
while ordered.size != (all.size)
all.each do |r|
ordered.push(r) if (ordered.last == r.parent_rank)
end
end
ordered
ordered << all.detect { |r| ordered.last == r.parent_rank } while ordered.size != all.size

return ordered
end

# Returns true if rank.to_s is the name of a NomenclaturalRank.
Expand Down

2 comments on commit 74ef757

@mjy
Copy link
Member

@mjy mjy commented on 74ef757 Oct 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@LocoDelAssembly
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sorting algorithm remains O(n²) though, and more importantly still not immune to loops (so the spec I added to verify that parent_rank forms a cycle-free path never gets to show a failure because this method is executed too early). I'll revisit it after making some progress with DwC-A.

Please sign in to comment.