human_name 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +44 -0
- data/Rakefile +12 -0
- data/human_name.gemspec +25 -0
- data/lib/human_name/methods.rb +23 -0
- data/lib/human_name/version.rb +3 -0
- data/lib/human_name.rb +32 -0
- data/spec/human_name/methods_spec.rb +53 -0
- data/spec/human_name_spec.rb +60 -0
- data/spec/spec_helper.rb +1 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c8bc2d175321c5d35c18a983b4141cbd9a9cd250
|
4
|
+
data.tar.gz: 0f079e5ee1799de02940fe068b745140e0f175b7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9feee94f70c46381738d0e4567b89c79f61c1d9963a5f529f3f9431fb446c96414fa701bf1c810f299075e4eca619707c349fe9ea974252651034688bcc2a09c
|
7
|
+
data.tar.gz: 1ce31adf416dc2b70c58391d7e039b56737c4b3cf399a483548f189394ba77ec6e53e675712fa369f6e6c5ae3546731e1e99d76a0d733ec1696060e827d50be0
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Maarten Claes
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# HumanName
|
2
|
+
|
3
|
+
Never write `def full_name` again!
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'human_name'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install human_name
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class Person
|
23
|
+
include HumanName::Methods.new(:first_name, :last_name)
|
24
|
+
|
25
|
+
attr_reader :first_name, :last_name
|
26
|
+
|
27
|
+
def initialize(first_name, last_name)
|
28
|
+
@first_name, @last_name = first_name, last_name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
person = Person.new('John', 'Doe')
|
33
|
+
person.human_name # => #<HumanName:0x007fe590825b68 @first_name="John", @last_name="Doe">
|
34
|
+
person.full_name # => 'John Doe'
|
35
|
+
person.name_initials # => 'J. D.'
|
36
|
+
```
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
desc 'Run mutation tests'
|
10
|
+
task :mutant do
|
11
|
+
system 'bundle exec mutant --include lib --require "human_name" --use rspec ::HumanName*'
|
12
|
+
end
|
data/human_name.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'human_name/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "human_name"
|
8
|
+
spec.version = HumanName::VERSION
|
9
|
+
spec.authors = ["Maarten Claes"]
|
10
|
+
spec.email = ["maartencls@gmail.com"]
|
11
|
+
spec.description = %q{If I have to write 'def full_name' one more time...}
|
12
|
+
spec.summary = %q{If I have to write 'def full_name' one more time...}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "mutant"
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class HumanName
|
2
|
+
class Methods < Module
|
3
|
+
def initialize(first_name_method = :first_name, last_name_method = :last_name)
|
4
|
+
@__first_name_method = first_name_method
|
5
|
+
@__last_name_method = last_name_method
|
6
|
+
end
|
7
|
+
|
8
|
+
def included(base)
|
9
|
+
fname = @__first_name_method
|
10
|
+
lname = @__last_name_method
|
11
|
+
base.class_eval do
|
12
|
+
define_method :human_name do
|
13
|
+
first_name = send fname
|
14
|
+
last_name = send lname
|
15
|
+
HumanName.new(first_name, last_name)
|
16
|
+
end
|
17
|
+
|
18
|
+
extend Forwardable
|
19
|
+
def_delegators :human_name, :full_name, :name_initials
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/human_name.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "forwardable"
|
2
|
+
require "human_name/version"
|
3
|
+
require "human_name/methods"
|
4
|
+
|
5
|
+
class HumanName
|
6
|
+
attr_reader :first_name, :last_name
|
7
|
+
def initialize(first_name, last_name)
|
8
|
+
@first_name, @last_name = first_name, last_name
|
9
|
+
end
|
10
|
+
|
11
|
+
def first_name
|
12
|
+
(@first_name || '').strip
|
13
|
+
end
|
14
|
+
|
15
|
+
def last_name
|
16
|
+
(@last_name || '').strip
|
17
|
+
end
|
18
|
+
|
19
|
+
def full_name
|
20
|
+
non_blank_names.join(' ')
|
21
|
+
end
|
22
|
+
|
23
|
+
def name_initials
|
24
|
+
non_blank_names.map { |n| "#{n[0]}." }.join(' ')
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def non_blank_names
|
30
|
+
[first_name, last_name].delete_if(&:empty?)
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HumanName::Methods do
|
4
|
+
subject { @nameable.new }
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@nameable = Class.new do
|
8
|
+
attr_accessor :first_name, :last_name
|
9
|
+
include HumanName::Methods.new(:first_name, :last_name)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#human_name' do
|
14
|
+
it { should respond_to(:human_name) }
|
15
|
+
its(:human_name) { should be_a(HumanName) }
|
16
|
+
|
17
|
+
it "creates a new HumanName using the methods passed into initialize" do
|
18
|
+
nameable = Class.new do
|
19
|
+
def fname; 'FirstName' end
|
20
|
+
def lname; 'LastName' end
|
21
|
+
include HumanName::Methods.new(:fname, :lname)
|
22
|
+
end
|
23
|
+
|
24
|
+
hn = nameable.new.human_name
|
25
|
+
expect(hn.first_name).to eq('FirstName')
|
26
|
+
expect(hn.last_name).to eq('LastName')
|
27
|
+
end
|
28
|
+
|
29
|
+
it "uses #first_name and #last_name methods by default" do
|
30
|
+
nameable = Class.new do
|
31
|
+
include HumanName::Methods.new
|
32
|
+
private
|
33
|
+
def first_name; 'FirstName' end
|
34
|
+
def last_name; 'LastName' end
|
35
|
+
end
|
36
|
+
expect(nameable.new.human_name.full_name).to eq('FirstName LastName')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#full_name' do
|
41
|
+
it "delegates to #human_name" do
|
42
|
+
expect_any_instance_of(HumanName).to receive(:full_name)
|
43
|
+
subject.full_name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#name_initials' do
|
48
|
+
it "delegates to #human_name" do
|
49
|
+
expect_any_instance_of(HumanName).to receive(:name_initials)
|
50
|
+
subject.name_initials
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HumanName do
|
4
|
+
subject { described_class.new(first_name, last_name) }
|
5
|
+
let(:first_name) { '' }
|
6
|
+
let(:last_name) { '' }
|
7
|
+
|
8
|
+
describe '#full_name' do
|
9
|
+
[
|
10
|
+
'Ron', 'Burgundy', 'Ron Burgundy',
|
11
|
+
' Ron', 'Swanson ', 'Ron Swanson',
|
12
|
+
|
13
|
+
'', 'MacGyver', 'MacGyver',
|
14
|
+
'MacGruber', nil, 'MacGruber',
|
15
|
+
|
16
|
+
'', '', '',
|
17
|
+
nil, nil, '',
|
18
|
+
].each_slice(3) do |a|
|
19
|
+
context "when first_name is '#{a[0]}' and last_name is '#{a[1]}'" do
|
20
|
+
subject { described_class.new(a[0], a[1]) }
|
21
|
+
its(:full_name) { should eq(a[2]) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
name_expectations = [
|
27
|
+
'Harrison', 'Harrison',
|
28
|
+
' Harrison ', 'Harrison',
|
29
|
+
' ', '',
|
30
|
+
nil, '',
|
31
|
+
]
|
32
|
+
%w(first_name last_name).each do |attr|
|
33
|
+
describe "##{attr}" do
|
34
|
+
name_expectations.each_slice(2) do |a|
|
35
|
+
context "when #{attr} is '#{a[0]}'" do
|
36
|
+
let(attr.to_sym) { a[0] }
|
37
|
+
its(attr.to_sym) { should eq(a[1]) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#name_initials' do
|
44
|
+
[
|
45
|
+
'Ron', 'Burgundy', 'R. B.',
|
46
|
+
' Ron', 'Swanson ', 'R. S.',
|
47
|
+
|
48
|
+
'', 'MacGyver', 'M.',
|
49
|
+
'MacGruber', nil, 'M.',
|
50
|
+
|
51
|
+
'', '', '',
|
52
|
+
nil, nil, '',
|
53
|
+
].each_slice(3) do |a|
|
54
|
+
context "when first_name is '#{a[0]}' and last_name is '#{a[1]}'" do
|
55
|
+
subject { described_class.new(a[0], a[1]) }
|
56
|
+
its(:name_initials) { should eq(a[2]) }
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'human_name'
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: human_name
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Maarten Claes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-10 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
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: rspec
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mutant
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: If I have to write 'def full_name' one more time...
|
70
|
+
email:
|
71
|
+
- maartencls@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- human_name.gemspec
|
83
|
+
- lib/human_name.rb
|
84
|
+
- lib/human_name/methods.rb
|
85
|
+
- lib/human_name/version.rb
|
86
|
+
- spec/human_name/methods_spec.rb
|
87
|
+
- spec/human_name_spec.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
homepage: ''
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.2.1
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: If I have to write 'def full_name' one more time...
|
113
|
+
test_files:
|
114
|
+
- spec/human_name/methods_spec.rb
|
115
|
+
- spec/human_name_spec.rb
|
116
|
+
- spec/spec_helper.rb
|