forked from lewispb/rubymap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lewispb#51 Add sort-orgs.rb script to sort organizations by name case…
… insensitively.
- Loading branch information
1 parent
da30594
commit 915cb0f
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#!/usr/bin/ruby | ||
|
||
# Replaces the organizations.yml file with its data sorted by name, case insensitively. | ||
# Before overwriting the file, tests that the unsorted and sorted data, | ||
# converted to sets, are equal. | ||
|
||
require 'set' | ||
require 'yaml' | ||
|
||
def test(unsorted, sorted) | ||
if Set.new(unsorted) != Set.new(sorted) | ||
raise "Sort corrupted the data." | ||
end | ||
end | ||
|
||
def sort_orgs_case_insensitively(orgs) | ||
orgs.sort { |org1, org2| org1['name'].casecmp(org2['name']) } | ||
end | ||
|
||
def filespec | ||
ARGV[0] || 'organizations.yml' | ||
end | ||
|
||
|
||
orgs = YAML.load_file(filespec) | ||
sorted_orgs = sort_orgs_case_insensitively(orgs) | ||
|
||
# Enable this line to verify that the test that tests for data corruption works: | ||
# sorted_orgs.pop | ||
|
||
test(orgs, sorted_orgs) | ||
File.write(filespec, sorted_orgs.to_yaml) |