Skip to content

Commit

Permalink
Add Faker::Relationship (faker-ruby#1346)
Browse files Browse the repository at this point in the history
* Add Faker::Relationship

* Update relationship.md

* Update CHANGELOG.md
  • Loading branch information
QuantumWaver authored and vbrazo committed Sep 2, 2018
1 parent b88e1be commit 47741ff
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [PR #1329](https://github.com/stympy/faker/pull/1329) Update docs on behavior of price [@softwaregravy](https://github.com/softwaregravy)

### Feature Request
- [PR #1346](https://github.com/stympy/faker/pull/1346) Add Faker::Relationship [@QuantumWaver](https://github.com/QuantumWaver)
- [PR #1348](https://github.com/stympy/faker/pull/1348) Add Faker::Finance.vat_number [@vbrazo](https://github.com/vbrazo)
- [PR #1342](https://github.com/stympy/faker/pull/1342) Added Faker::CryptoCoin scope [@jacksonpires](https://github.com/jacksonpires)
- [PR #1338](https://github.com/stympy/faker/pull/1338) Add new translations to the en-ZA locale [@bradleymarques](https://github.com/bradleymarques)
Expand All @@ -41,6 +42,7 @@
- [PR #1246](https://github.com/stympy/faker/pull/1246) Store list of generators with enabled uniqueness for faster clear [@MarcPer](https://github.com/MarcPer)

### Update/add locales
- [PR #1342](https://github.com/stympy/faker/pull/1342) Add Japanese Food Sushi for Japanese and English [@yizknn](https://github.com/yizknn)
- [PR #1343](https://github.com/stympy/faker/pull/1343) Update cell phone format to be phonelib compatible for Vietnam locale [@Looooong](https://github.com/Looooong)
- [PR #1340](https://github.com/stympy/faker/pull/1340) Fix typos and additions for Faker::Esport [@Mayurifag](https://github.com/Mayurifag)
- [PR #1332](https://github.com/stympy/faker/pull/1332) Fix typo in buffy.big_bads [@tragiclifestories](https://github.com/tragiclifestories)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ Contents
- [Faker::Pokemon](doc/pokemon.md)
- [Faker::PrincessBride](doc/princess_bride.md)
- [Faker::ProgrammingLanguage](doc/programming_language.md)
- [Faker::Relationship](doc/relationship.md)
- [Faker::Restaurant](doc/restaurant.md)
- [Faker::RickAndMorty](doc/rick_and_morty.md)
- [Faker::Robin](doc/robin.md)
Expand Down
20 changes: 20 additions & 0 deletions doc/relationship.md
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"
```
42 changes: 42 additions & 0 deletions lib/faker/relationship.rb
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
10 changes: 10 additions & 0 deletions lib/locales/en/relationship.yml
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']
44 changes: 44 additions & 0 deletions test/test_faker_relationship.rb
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

0 comments on commit 47741ff

Please sign in to comment.