Skip to content

Commit

Permalink
Merge pull request #53 from keithrbennett/sort-orgs-by-name
Browse files Browse the repository at this point in the history
Add sort-orgs.rb script to sort organizations
  • Loading branch information
lewispb authored May 6, 2019
2 parents 75aad3c + fd446c5 commit b9718af
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Sometimes folks are interested in finding a new gig, in a new location. Ruby Map
What if your company / meetup / event is missing on the map? No problem, just send us a PR and it will get added asap.
An example PR can be found [here](https://github.com/lewispb/rubymap/pull/1).

Please try to maintain the order-by-name of the yaml file, to prevent merge conflicts.

## Prerequisites

```bash
Expand Down
49 changes: 49 additions & 0 deletions bin/sort-orgs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/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 orgs_filespec
@orgs_filespec ||= begin
project_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
File.join(project_root, 'data', 'seeds', 'organizations.yml')
end
end


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 main
orgs = YAML.load_file(orgs_filespec)
sorted_orgs = sort_orgs_case_insensitively(orgs)

if orgs == sorted_orgs
puts "File was already sorted. No need to overwrite."
exit(0)
else

# Enable this line to verify that the test that tests for data corruption works:
# sorted_orgs.pop

test(orgs, sorted_orgs)
File.write(orgs_filespec, sorted_orgs.to_yaml)
puts "Organizations file sort successful."
end
end


main

0 comments on commit b9718af

Please sign in to comment.