diff --git a/CHANGELOG.md b/CHANGELOG.md index 10370e8be9..5b8e6df01d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) @@ -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) diff --git a/README.md b/README.md index badce951c6..b34a0cb072 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/doc/relationship.md b/doc/relationship.md new file mode 100644 index 0000000000..63be1388d5 --- /dev/null +++ b/doc/relationship.md @@ -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" +``` diff --git a/lib/faker/relationship.rb b/lib/faker/relationship.rb new file mode 100644 index 0000000000..3e9e48fb08 --- /dev/null +++ b/lib/faker/relationship.rb @@ -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 diff --git a/lib/locales/en/relationship.yml b/lib/locales/en/relationship.yml new file mode 100644 index 0000000000..a5ff7b6434 --- /dev/null +++ b/lib/locales/en/relationship.yml @@ -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'] diff --git a/test/test_faker_relationship.rb b/test/test_faker_relationship.rb new file mode 100644 index 0000000000..ef60a79908 --- /dev/null +++ b/test/test_faker_relationship.rb @@ -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