name_sort 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 658f83e40f4822ec25c1595dea3044bf1b575836
4
+ data.tar.gz: eae24d254ae9c7143b52bf060e1fd397e1b6c3a5
5
+ SHA512:
6
+ metadata.gz: c863ee908ea8dc2e43ae21545641174d43c636b966cfb89071bf5d44764bbcce6ac1f94deb482a178c19dda9ee173f4bc1c545a82e393094bc0d34a1fb1aaf1e
7
+ data.tar.gz: 1774e9d1920404abd568c88630eb3482e117ddde33a5c9f5241597d0624389b5f28be8a3b9ebad8c079fde425bbe97f9c8a3738b4763b45e3634669f35d0565f
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in name_sort.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # NameSort
2
+
3
+ Example name sorting
4
+
5
+ ## Installation
6
+
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'name_sort'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install name_sort
21
+
22
+
23
+ ## Usage
24
+
25
+ Run executable demo:
26
+
27
+ $ demo_name_sort
28
+
29
+
30
+ ## Development
31
+
32
+ checkout the repo
33
+
34
+ $ git clone git@github.com:mjording/name_sort.git
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/[my-github-username]/name_sort/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
48
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :test => :spec
6
+
7
+ task :default => [:test]
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "name_sort"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "name_sort"
5
+
6
+ sorter = NameSort::Sorter.new
7
+ {
8
+ "|" => "./spec/fixtures/pipe.txt",
9
+ " " => "./spec/fixtures/space.txt",
10
+ "," => "./spec/fixtures/comma.txt"
11
+ }.each_pair do |k,v|
12
+ sorter.input(k,v)
13
+ end
14
+
15
+ puts "Output 1"
16
+ sorter.print_names("gender")
17
+ puts ""
18
+ puts "Output 2"
19
+ sorter.print_names("date_of_birth")
20
+ puts ""
21
+ puts "Output 3"
22
+ sorter.print_names("date_of_birth").reverse
23
+ puts ""
@@ -0,0 +1,3 @@
1
+ module NameSort
2
+ VERSION = "0.1.0"
3
+ end
data/lib/name_sort.rb ADDED
@@ -0,0 +1,85 @@
1
+ require "name_sort/version"
2
+ require 'csv'
3
+
4
+ module NameSort
5
+ class InvalidDeliminatorError < StandardError; end
6
+ class Sorter
7
+ HDR = %w(last_name first_name gender date_of_birth favorite_color)
8
+ attr_reader :names, :parsed
9
+ def initialize
10
+ @names = []
11
+ end
12
+
13
+ def input(delimiter,file)
14
+ header_fields = header(delimiter)
15
+ parsed = CSV.read(file, col_sep: delimiter, headers: header_fields)
16
+ parsed.delete('middle_initial')
17
+ data = []
18
+ parsed.each do |line|
19
+ data << HDR.map{|field| line[field].nil? ? nil : format_for(field,line[field]) }.compact
20
+ end
21
+ @names += data
22
+ end
23
+
24
+ def format_for(field,value)
25
+ case field.strip()
26
+ when 'gender'
27
+ format_gender(value.strip())
28
+ when 'date_of_birth'
29
+ format_date(value.strip())
30
+ else
31
+ value.strip()
32
+ end
33
+ end
34
+
35
+ def format_gender(value)
36
+ if value.match(/^(m|male)$/i)
37
+ "Male"
38
+ elsif value.match(/^(f|female)$/i)
39
+ "Female"
40
+ end
41
+ end
42
+
43
+ def format_date(value)
44
+ if value.count('-') == 2
45
+ Date.strptime(value, "%m-%d-%Y")
46
+ elsif value.count('/') == 2
47
+ Date.strptime(value, "%m/%d/%Y")
48
+ end
49
+ end
50
+
51
+ def header(delimiter)
52
+ unless [' ','|',','].include?(delimiter)
53
+ raise InvalidDeliminatorError
54
+ end
55
+ hdr = %w(last_name first_name)
56
+ case delimiter
57
+ when '|'
58
+ hdr += %w(middle_initial gender favorite_color date_of_birth)
59
+ when ' '
60
+ hdr += %w(middle_initial gender date_of_birth favorite_color)
61
+ when ','
62
+ hdr += %w(gender favorite_color date_of_birth)
63
+ else
64
+ hdr
65
+ end
66
+ end
67
+
68
+ def sort_for(field)
69
+ field_index = HDR.index(field)
70
+ @names.sort_by{|n|n[field_index]}
71
+ end
72
+
73
+ def format_fields(record)
74
+ record.map.with_index do |field,idx|
75
+ HDR.index("date_of_birth") == idx ? field.strftime('%-m/%d/%Y') : field
76
+ end
77
+ end
78
+
79
+ def print_names(sort_by)
80
+ sort_for(sort_by).each do |record|
81
+ puts format_fields(record).join(" ")
82
+ end
83
+ end
84
+ end
85
+ end
data/name_sort.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'name_sort/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "name_sort"
8
+ spec.version = NameSort::VERSION
9
+ spec.authors = ["Matthew Jording"]
10
+ spec.email = ["mjording@gmail.com"]
11
+
12
+ spec.summary = %q{Quick demo for gem creation and delimited file sorting}
13
+ spec.description = %q{Quick demo for gem creation and delimited file sorting}
14
+ spec.homepage = "https://github.com/mjording/name_sort"
15
+
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.9"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency 'rspec', '~> 3.2.0'
25
+ spec.add_development_dependency 'pry', '~> 0.10.1'
26
+ spec.add_development_dependency 'awesome_print', '~> 1.6.1'
27
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: name_sort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Jording
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.2.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: awesome_print
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 1.6.1
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 1.6.1
83
+ description: Quick demo for gem creation and delimited file sorting
84
+ email:
85
+ - mjording@gmail.com
86
+ executables:
87
+ - demo_name_sort
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - exe/demo_name_sort
101
+ - lib/name_sort.rb
102
+ - lib/name_sort/version.rb
103
+ - name_sort.gemspec
104
+ homepage: https://github.com/mjording/name_sort
105
+ licenses: []
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.4.6
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Quick demo for gem creation and delimited file sorting
127
+ test_files: []