forked from faker-ruby/faker
-
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.
Add Faker::Relationship (faker-ruby#1346)
* Add Faker::Relationship * Update relationship.md * Update CHANGELOG.md
- Loading branch information
1 parent
b88e1be
commit 47741ff
Showing
6 changed files
with
119 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
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
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,20 @@ | ||
# Faker::Relationship | ||
### A Faker module for familial relationships. | ||
|
||
It might be available in the next version. | ||
|
||
```ruby | ||
Faker::Relationship.familial #=> "Mother" or "Grandmother" | ||
|
||
Faker::Relationship.familial('direct') #=> "Mother" or "Brother" | ||
|
||
Faker::Relationship.familial('extended') #=> "Grandmother" or "Niece" or "Aunt" | ||
|
||
Faker::Relationship.spouse #=> "Husband" or "Wife" | ||
|
||
Faker::Relationship.parent #=> "Father" or "Mother" | ||
|
||
Faker::Relationship.in_law #=> "Father-in-law" | ||
|
||
Faker::Relationship.sibling #=> "Sister" or "Brother" | ||
``` |
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Faker | ||
class Relationship < Base | ||
flexible :relationship | ||
|
||
class << self | ||
def familial(connection = nil) | ||
familial_connections = translate('faker.relationship.familial').keys | ||
|
||
if connection.nil? | ||
connection = sample(familial_connections).to_s | ||
else | ||
connection = connection.to_s.downcase | ||
|
||
unless familial_connections.include?(connection.to_sym) | ||
raise ArgumentError, | ||
"Familial connections can be left blank or #{familial_connections.join(', ')}" | ||
end | ||
end | ||
|
||
fetch('relationship.familial.' + connection) | ||
end | ||
|
||
def in_law | ||
fetch('relationship.in_law') | ||
end | ||
|
||
def spouse | ||
fetch('relationship.spouse') | ||
end | ||
|
||
def parent | ||
fetch('relationship.parent') | ||
end | ||
|
||
def sibling | ||
fetch('relationship.sibling') | ||
end | ||
end | ||
end | ||
end |
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,10 @@ | ||
en: | ||
faker: | ||
relationship: | ||
familial: | ||
direct: ['Father', 'Mother', 'Sister', 'Brother'] | ||
extended: ['Grandfather', 'Grandmother', 'Uncle', 'Aunt', 'Cousin', 'Niece', 'Nephew', 'Grandson', 'Granddaughter'] | ||
in_law: ['Father-in-law', 'Mother-in-law', 'Sister-in-law', 'Brother-in-law'] | ||
spouse: ['Husband', 'Wife'] | ||
parent: ['Father', 'Mother'] | ||
sibling: ['Sister', 'Brother'] |
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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'test_helper' | ||
|
||
class TestFakerRelationship < Test::Unit::TestCase | ||
def setup | ||
@tester = Faker::Relationship | ||
end | ||
|
||
def test_random_familial | ||
assert @tester.familial.match(/\w+/) | ||
end | ||
|
||
def test_familial_direct | ||
assert @tester.familial('direct').match(/\w+/) | ||
end | ||
|
||
def test_familial_extended | ||
assert @tester.familial('extended').match(/\w+/) | ||
end | ||
|
||
# test error on no match | ||
def test_invalid_familial_connection | ||
assert_raise ArgumentError do | ||
@tester.familial('Not Correct') | ||
end | ||
end | ||
|
||
def test_in_law | ||
assert @tester.in_law.match(/\w+/) | ||
end | ||
|
||
def test_spouse | ||
assert @tester.spouse.match(/\w+/) | ||
end | ||
|
||
def test_parent | ||
assert @tester.parent.match(/\w+/) | ||
end | ||
|
||
def test_sibling | ||
assert @tester.sibling.match(/\w+/) | ||
end | ||
end |