namie 0.0.1

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: 440dea9bb74463fcf2fec7ceb3902107284cc2e7
4
+ data.tar.gz: b40cce711ac182df79785adc580a1f2e1b21d7b1
5
+ SHA512:
6
+ metadata.gz: 1b344dbd9b7f32e62fd4e1ec82f0943334672a8fadd057587dd0033d4ad588c32cdce685c8489fbcb52e89b4a89d5fa84f30198a45518b840c6cee2f60cecbe5
7
+ data.tar.gz: 3232b7e12198c6dfb0e29067897e4e9f2d37b5ef34a5b646d97e987c749ec3eaf212de6cf27f98dd016e1a86af55262f1c364cab74f1d7c57643f36a57a4aff2
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ coverage
3
+ rdoc
4
+ pkg
5
+ .bundle
6
+ Gemfile.lock
7
+ *.gem
8
+ *.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in phonie-codes.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,26 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec feature)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ guard :minitest do
11
+ # with Minitest::Unit
12
+ # watch(%r{^test/(.*)\/?test_(.*)\.rb$})
13
+ # watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
14
+ # watch(%r{^test/test_helper\.rb$}) { 'test' }
15
+
16
+ # with Minitest::Spec
17
+ watch(/^spec\/(.*)_spec\.rb$/)
18
+ watch(/^lib\/(.+)\.rb$/) { 'spec' }
19
+ # watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
20
+ watch(/^spec\/spec_helper\.rb$/) { 'spec' }
21
+ end
22
+
23
+ guard :rubocop do
24
+ watch(/.+\.rb$/)
25
+ watch(/(?:.+\/)?\.rubocop\.yml$/) { |m| File.dirname(m[0]) }
26
+ end
data/README.md ADDED
@@ -0,0 +1,57 @@
1
+
2
+ .-.
3
+ ; : .-.
4
+ .;: : .-. . ,';.,';. `-' .-.
5
+ .;' \ : ; : ;; ;; ;; ;' .;.-'
6
+ .:'.; \: `:::'-' '; ;; '; _.;:._. `:::'
7
+ (__.' `. _; `-'
8
+
9
+
10
+
11
+ Names as first class citizens
12
+
13
+
14
+ ## Install
15
+
16
+
17
+ gem 'namie'
18
+
19
+
20
+ ## Use
21
+
22
+
23
+ ```
24
+ name = Name.new('Jules Verne')
25
+ name.first # 'Jules'
26
+ name.last # 'Verne'
27
+ name.to_s # 'Jules Verne'
28
+ ```
29
+
30
+
31
+ Accepts prefix, suffix and middlenames:
32
+
33
+ ```
34
+ name = Name.new('Sir Arthur C. Clark')
35
+ name.title # 'Sir'
36
+ name.first # 'Arthur'
37
+ name.last # 'Clark'
38
+ name.to_s("%l %m, %f") # Clark C., Arthur
39
+ ```
40
+
41
+ Accepts and detects company names and suffixes:
42
+
43
+ ```
44
+ name = Name.new('ACME Corp LLC')
45
+ name.first # 'ACME'
46
+ name.last # 'Corp'
47
+ name.suffix # 'LLC'
48
+ ```
49
+
50
+
51
+ ## ActiveModel
52
+
53
+ Working on it:
54
+
55
+ * to params hash
56
+ * cached/customizable string
57
+ * sanitized string
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'spec'
7
+ t.test_files = FileList['spec/**/*_spec.rb']
8
+ t.verbose = true
9
+ end
10
+
11
+ task default: [:test]
@@ -0,0 +1,41 @@
1
+ module Namie
2
+ # Format name
3
+ class Formatter
4
+ attr_accessor :out, :name, :format
5
+ FORMATS = {
6
+ default: '%t %f %m %l %s',
7
+ airport: '%t %l, %f %m %s',
8
+ strip: '%f %l'
9
+ }
10
+
11
+ ABBR = {
12
+ t: :title,
13
+ f: :first,
14
+ m: :middle,
15
+ l: :last,
16
+ s: :suffix
17
+ }
18
+
19
+ def initialize(name, format)
20
+ @name = name
21
+ format ||= :default
22
+ @format = format.is_a?(Symbol) ? FORMATS[format] : format
23
+ @out = @format.dup
24
+ end
25
+
26
+ #
27
+ # %t Title
28
+ # %f First Name
29
+ # %m Middle Name(s)
30
+ # %l Last Name
31
+ # %s suffix
32
+ #
33
+ def to_s
34
+ ABBR.each do |k, v|
35
+ val = name.send(v)
36
+ out.gsub!("%#{k}", val || '')
37
+ end
38
+ out.gsub(/\s{2,}/, ' ').strip.chomp
39
+ end
40
+ end
41
+ end
data/lib/namie/name.rb ADDED
@@ -0,0 +1,38 @@
1
+ module Namie
2
+ # The Name!
3
+ class Name
4
+ attr_accessor :title, :first, :middle, :last, :suffix
5
+
6
+ def initialize(*args)
7
+ @title, @first, *@middle, @last, @suffix = Namie::Parser.new(args).args
8
+ end
9
+ alias_method :firstname, :first
10
+ alias_method :middlenames, :middle
11
+ alias_method :lastname, :last
12
+
13
+ def middle
14
+ return nil if @middle.reject(&:nil?).empty?
15
+ @middle.join(' ')
16
+ end
17
+
18
+ def valid?
19
+ !first.empty?
20
+ end
21
+
22
+ def ==(other)
23
+ fullname == other.fullname
24
+ end
25
+
26
+ def fullname
27
+ to_s
28
+ end
29
+
30
+ def shortname
31
+ to_s(:short)
32
+ end
33
+
34
+ def to_s(fmt = nil)
35
+ Namie::Formatter.new(self, fmt).to_s
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ module Namie
2
+ # Name parser
3
+ class Parser
4
+ TITLES = /^(Mister|Mr\.?|Sr\.?|Sir|Senhor|Dr\.?|Doutor|Sra\.?|Senhora)\b/i
5
+ JURIDIC = /\b(Ltd\.?|LTDA|Gmbh|LLC|S\/A)$/i
6
+ SUFFIXES = /\b(#{JURIDIC}|Junior|Jr\.?|Neto|II|III)$/i
7
+ attr_accessor :title, :first, :middle, :last, :suffix, :txt
8
+
9
+ def initialize(params)
10
+ hsh, txt = params.partition { |pm| pm.respond_to?(:key) }
11
+ hsh.each { |k, v| send("#{k}=", v) }
12
+ @txt = txt.map { |t| t.split(' ') }.flatten
13
+ remove_non_names
14
+ parse_name
15
+ normalize
16
+ end
17
+
18
+ def parse_name
19
+ @txt.push(@txt.shift.tr(',', '')) if txt.first =~ /,/
20
+ @first, *@middle, @last = txt.size > 2 ? txt : txt.insert(1, nil)
21
+ end
22
+
23
+ def remove_non_names
24
+ @title, @txt = txt.partition { |a| a =~ TITLES }
25
+ @suffix, @txt = txt.partition { |a| a =~ SUFFIXES }
26
+ end
27
+
28
+ def normalize
29
+ [:title, :middle, :suffix].each do |v|
30
+ val = send(v).reject(&:nil?).any? ? send(v).join(' ') : nil
31
+ instance_variable_set("@#{v}", val)
32
+ end
33
+ end
34
+
35
+ def args
36
+ [title, first, middle, last, suffix]
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,4 @@
1
+ # :nodoc:
2
+ module Namie
3
+ VERSION = '0.0.1'
4
+ end
data/lib/namie.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'namie/name'
2
+ require 'namie/parser'
3
+ require 'namie/formatter'
4
+
5
+ #
6
+ # Names as first class citizens
7
+ #
8
+ module Namie
9
+ end
data/namie.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+ require 'namie/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'namie'
7
+ s.version = Namie::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Marcos Piccinini']
10
+ s.email = ['nofxx@github.com']
11
+ s.homepage = 'http://github.com/nofxx/namie'
12
+ s.summary = 'Area codes for phonie'
13
+ s.description = 'Area codes for phonie'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.require_paths = ['lib']
18
+
19
+ s.add_development_dependency 'minitest', '~> 5.0'
20
+ s.add_development_dependency 'rubocop'
21
+ s.add_development_dependency 'rake'
22
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Namie::Parser do
4
+ describe 'Simple name' do
5
+ let(:parse) { Namie::Name.new('Jules Verne') } # Jules Gabriel Verne
6
+
7
+ it 'should have a nice #to_s' do
8
+ parse.to_s.must_equal('Jules Verne')
9
+ end
10
+
11
+ it 'should have a nice #to_s(:symbol)' do
12
+ parse.to_s(:airport).must_equal('Verne, Jules')
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,158 @@
1
+ require 'spec_helper'
2
+
3
+ describe Namie::Name do
4
+ it 'should instantiate' do
5
+ Namie::Name.new('Isaac Asimov').must_be_instance_of(Namie::Name)
6
+ end
7
+
8
+ it 'should accept middle names' do
9
+ Namie::Name.new('John Edgard Smith').valid?.must_equal(true)
10
+ end
11
+
12
+ it 'should abbr middle names' do
13
+ Namie::Name.new('George S. Patton').must_be_instance_of(Namie::Name)
14
+ end
15
+
16
+ it 'should respond to alias method firstname' do
17
+ Namie::Name.new('Isaac Asimov').first.must_equal('Isaac')
18
+ end
19
+
20
+ it 'should respond to alias method lastname' do
21
+ Namie::Name.new('George S. Patton').lastname.must_equal('Patton')
22
+ end
23
+
24
+ describe 'Simple name' do
25
+ let(:nam) { Namie::Name.new('Jules Verne') } # Jules Gabriel Verne
26
+
27
+ it { nam.title.must_be_nil }
28
+ it { nam.first.must_equal('Jules') }
29
+ it { nam.middle.must_be_nil }
30
+ it { nam.last.must_equal('Verne') }
31
+ it { nam.fullname.must_equal('Jules Verne') }
32
+ end
33
+
34
+ describe 'Simple name' do
35
+ let(:nam) { Namie::Name.new('Douglas Adams') } # Jules Gabriel Verne
36
+
37
+ it { nam.first.must_equal('Douglas') }
38
+ it { nam.middle.must_be_nil }
39
+ it { nam.last.must_equal('Adams') }
40
+ end
41
+
42
+ describe 'Name with middle name' do
43
+ let(:nam) { Namie::Name.new('Alberto Santos Dumont') }
44
+
45
+ it { nam.title.must_be_nil }
46
+ it { nam.first.must_equal('Alberto') }
47
+ it { nam.middle.must_equal('Santos') }
48
+ it { nam.last.must_equal('Dumont') }
49
+ it { nam.suffix.must_be_nil }
50
+ it { nam.fullname.must_equal('Alberto Santos Dumont') }
51
+ end
52
+
53
+ describe 'Name with abbr middle name' do
54
+ let(:nam) { Namie::Name.new('Arthur C. Clark') }
55
+
56
+ it { nam.title.must_be_nil }
57
+ it { nam.first.must_equal('Arthur') }
58
+ it { nam.middle.must_equal('C.') }
59
+ it { nam.last.must_equal('Clark') }
60
+ it { nam.suffix.must_be_nil }
61
+ end
62
+
63
+ describe 'Name with lots of middle names' do
64
+ let(:nam) { Namie::Name.new('Isabel Cristina Leopoldina Augusta Micaela Gabriela Rafaela Gonzaga de Bragança e Bourbon') } # rubocop:disable Metrics/LineLength
65
+
66
+ it { nam.title.must_be_nil }
67
+ it { nam.first.must_equal('Isabel') }
68
+ it { nam.middle.size.must_equal(74) }
69
+ it { nam.last.must_equal('Bourbon') }
70
+ it { nam.suffix.must_be_nil }
71
+ end
72
+
73
+ describe 'Name with title' do
74
+ let(:nam) { Namie::Name.new('Sir Arthur C. Clark') }
75
+
76
+ it { nam.title.must_equal('Sir') }
77
+ it { nam.first.must_equal('Arthur') }
78
+ it { nam.middle.must_equal('C.') }
79
+ it { nam.last.must_equal('Clark') }
80
+ it { nam.fullname.must_equal('Sir Arthur C. Clark') }
81
+ end
82
+
83
+ describe 'Name with suffix' do
84
+ let(:nam) { Namie::Name.new('José Maria Jr.') }
85
+
86
+ it { nam.first.must_equal('José') }
87
+ it { nam.middle.must_be_nil }
88
+ it { nam.last.must_equal('Maria') }
89
+ it { nam.fullname.must_equal('José Maria Jr.') }
90
+ it { nam.suffix.must_equal('Jr.') }
91
+ end
92
+
93
+ describe 'Name with suffix' do
94
+ let(:nam) { Namie::Name.new('José Maria Neto') }
95
+
96
+ it { nam.first.must_equal('José') }
97
+ it { nam.middle.must_be_nil }
98
+ it { nam.last.must_equal('Maria') }
99
+ it { nam.fullname.must_equal('José Maria Neto') }
100
+ it { nam.suffix.must_equal('Neto') }
101
+ end
102
+
103
+ describe 'Company name with suffix' do
104
+ let(:nam) { Namie::Name.new('ACME Enterprises Ltd.') }
105
+
106
+ it { nam.first.must_equal('ACME') }
107
+ it { nam.middle.must_be_nil }
108
+ it { nam.last.must_equal('Enterprises') }
109
+ it { nam.fullname.must_equal('ACME Enterprises Ltd.') }
110
+ it { nam.suffix.must_equal('Ltd.') }
111
+ end
112
+
113
+ describe 'Company name with suffix' do
114
+ let(:nam) { Namie::Name.new('ACME Enterprises Ltda') }
115
+
116
+ it { nam.first.must_equal('ACME') }
117
+ it { nam.middle.must_be_nil }
118
+ it { nam.last.must_equal('Enterprises') }
119
+ it { nam.fullname.must_equal('ACME Enterprises Ltda') }
120
+ it { nam.suffix.must_equal('Ltda') }
121
+ it { nam.to_s('%f - %l').must_equal('ACME - Enterprises') }
122
+ end
123
+
124
+ describe 'Name with nickname style' do
125
+ let(:nam) { Namie::Name.new('Ian Fraser "Lemmy" Kilmister') }
126
+
127
+ it { nam.title.must_be_nil }
128
+ it { nam.first.must_equal('Ian') }
129
+ it { nam.middle.must_equal('Fraser "Lemmy"') }
130
+ it { nam.last.must_equal('Kilmister') }
131
+ it { nam.fullname.must_equal('Ian Fraser "Lemmy" Kilmister') }
132
+ it { nam.suffix.must_be_nil }
133
+ end
134
+
135
+ describe 'Name aiport style' do
136
+ let(:nam) { Namie::Name.new('Hendrix, Jimmi') }
137
+
138
+ it { nam.title.must_be_nil }
139
+ it { nam.first.must_equal('Jimmi') }
140
+ it { nam.middle.must_be_nil }
141
+ it { nam.last.must_equal('Hendrix') }
142
+ it { nam.fullname.must_equal('Jimmi Hendrix') }
143
+ it { nam.suffix.must_be_nil }
144
+ it { nam.to_s(:airport).must_equal('Hendrix, Jimmi') }
145
+ end
146
+
147
+ describe 'Name aiport style' do
148
+ let(:nam) { Namie::Name.new('Hendrix, Jimmi') }
149
+
150
+ it { nam.title.must_be_nil }
151
+ it { nam.first.must_equal('Jimmi') }
152
+ it { nam.middle.must_be_nil }
153
+ it { nam.last.must_equal('Hendrix') }
154
+ it { nam.fullname.must_equal('Jimmi Hendrix') }
155
+ it { nam.suffix.must_be_nil }
156
+ it { nam.to_s(:airport).must_equal('Hendrix, Jimmi') }
157
+ end
158
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Namie::Parser do
4
+ describe 'Simple name' do
5
+ let(:parse) { Namie::Parser.new(['Jules Verne']) } # Jules Gabriel Verne
6
+
7
+ it { parse.title.must_be_nil }
8
+ it { parse.first.must_equal('Jules') }
9
+ it { parse.middle.must_be_nil }
10
+ it { parse.last.must_equal('Verne') }
11
+ end
12
+ end
@@ -0,0 +1,6 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+
3
+ require 'minitest/autorun'
4
+ require_relative '../lib/namie'
5
+
6
+ include Namie
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: namie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcos Piccinini
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Area codes for phonie
56
+ email:
57
+ - nofxx@github.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Guardfile
65
+ - README.md
66
+ - Rakefile
67
+ - lib/namie.rb
68
+ - lib/namie/formatter.rb
69
+ - lib/namie/name.rb
70
+ - lib/namie/parser.rb
71
+ - lib/namie/version.rb
72
+ - namie.gemspec
73
+ - spec/namie/formatter_spec.rb
74
+ - spec/namie/name_spec.rb
75
+ - spec/namie/parser_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage: http://github.com/nofxx/namie
78
+ licenses: []
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.3
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Area codes for phonie
100
+ test_files:
101
+ - spec/namie/formatter_spec.rb
102
+ - spec/namie/name_spec.rb
103
+ - spec/namie/parser_spec.rb
104
+ - spec/spec_helper.rb
105
+ has_rdoc: