fantasy_faker 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fab71ef1b33640b72154b8789896a266f66a6912
4
- data.tar.gz: 7f9b3361ed0716a389a001bb2c9d61fd05063239
2
+ SHA256:
3
+ metadata.gz: 6eae81f0044d69b3f458937a8a0a5a5aaea72d58b471f856f1eed03f69355468
4
+ data.tar.gz: 6e597da61c4230eff1ec99c05b13e99e7e4e0dd30103ca58a0e38383d304fca4
5
5
  SHA512:
6
- metadata.gz: 6031aed127874c3c0738b805f2c79b6ef8527de299c3e0556095ef1a23cf529a8079a835f46bf57f4570d1ac3a7f16ffe8f4fa57422ea1771d1faddf882de343
7
- data.tar.gz: 4b42a9011e53bbd68c406c3fc392db9a0c9641c44572fe9a0dde1035d6cdd2c0c2215398dcaa899cd2eb819e454ec00cc31f3395a49f733a78d1587912f20a3d
6
+ metadata.gz: a08f888fcac552c710a9a37aca542a8430c10bc373ef5aaaae459419c020db9fbc5a698c2619282e584e8b3aeeb53aee235ed9bebebe231c66379f87459cba54
7
+ data.tar.gz: e2bf56cb821542d9ddf99ef2cd9f47308946f6d9c8cab7348ee1f347a4491344a9f35963c581c8cf6f72236a2773414be813a400414fa462a5f904073d2e11ff
data/README.md CHANGED
@@ -1,31 +1,29 @@
1
- [![Gem Version](https://badge.fury.io/rb/fantasy_faker.svg)](http://badge.fury.io/rb/fantasy_faker)
2
- [![Code Climate](https://codeclimate.com/github/msx2/fantasy_faker/badges/gpa.svg)](https://codeclimate.com/github/msx2/fantasy_faker)
3
- [![Codeship Status for msx2/fantasy_faker](https://codeship.com/projects/367f3fe0-7500-0132-c61e-22ab3bab314c/status?branch=master)](https://codeship.com/projects/55108)
1
+ [![Gem Version](https://badge.fury.io/rb/fantasy_faker.svg)](https://badge.fury.io/rb/fantasy_faker)
4
2
 
5
3
  # FantasyFaker
6
4
 
7
5
  Inspired by [ffaker](https://github.com/EmmanuelOga/ffaker), used to easily generate fake random data: e.g. name, nickname.
8
6
 
9
- Created on a sole purpose to be used in fantasy-climate projects. Currently supports only Polish language.
7
+ Created on a sole purpose to be used in fantasy-climate projects. Requires Ruby 3.0+. Supports English (`:en`, default) and Polish (`:pl`) locales.
10
8
 
11
9
  ## Installation
12
10
 
13
11
  Add this line to your application's Gemfile:
14
12
 
15
- ```
13
+ ```ruby
16
14
  gem 'fantasy_faker'
17
15
  ```
18
16
 
19
17
  And then execute:
20
18
 
21
- ```
22
- $ bundle
19
+ ```sh
20
+ bundle install
23
21
  ```
24
22
 
25
23
  Or install it yourself as:
26
24
 
27
- ```
28
- $ gem install fantasy_faker
25
+ ```sh
26
+ gem install fantasy_faker
29
27
  ```
30
28
 
31
29
  ## Usage
@@ -33,19 +31,39 @@ $ gem install fantasy_faker
33
31
  ```ruby
34
32
  require 'fantasy_faker'
35
33
 
34
+ # Default locale is :en (or read automatically from I18n.locale in Rails)
36
35
  FantasyFaker::Character.male_first_name => "Sarillon"
37
- FantasyFaker::Character.male_nickname => "Dziecię Pustyni"
38
- FantasyFaker::Character.male_name => "Sarillon Dziecię Pustyni" # OR "Sarillon"
36
+ FantasyFaker::Character.male_nickname => "Child of the Desert"
37
+ FantasyFaker::Character.male_name => "Sarillon Child of the Desert" # OR "Sarillon"
39
38
 
40
39
  FantasyFaker::Character.female_first_name => "Linani"
40
+ FantasyFaker::Character.female_nickname => "the Bashful"
41
+ FantasyFaker::Character.female_name => "Linani the Bashful" # OR "Linani"
42
+
43
+ # Force a specific locale (globally or with a block)
44
+ FantasyFaker.use_locale(:pl)
45
+ FantasyFaker::Character.male_nickname => "Dziecię Pustyni"
41
46
  FantasyFaker::Character.female_nickname => "Wstydliwa"
42
- FantasyFaker::Character.female_name => "Linani Wstydliwa" # OR "Linani"
47
+
48
+ FantasyFaker.use_locale(:pl) do
49
+ FantasyFaker::Character.male_name => "Sarillon Dziecię Pustyni"
50
+ end
51
+ ```
52
+
53
+ ## Development
54
+
55
+ Dictionary data is stored in YAML files under `locales/`:
56
+ - `locales/common.yml`: language-independent lists (e.g. name prefixes and suffixes), grouped by module name (`character:`)
57
+ - `locales/en/<module>.yml`: English entries for a module
58
+ - `locales/pl/<module>.yml`: Polish entries for a module
59
+
60
+ ```sh
61
+ bundle install
62
+ bundle exec rspec
63
+ bundle exec rubocop
64
+ bundle exec rake # both
43
65
  ```
44
66
 
45
- ## Contributing
67
+ ## License
46
68
 
47
- 1. Fork it
48
- 2. Create your feature branch (`git checkout -b my-new-feature`)
49
- 3. Commit your changes (`git commit -am 'Add some feature'`)
50
- 4. Push to the branch (`git push origin my-new-feature`)
51
- 5. Create new Pull Request
69
+ MIT, see [LICENSE.txt](LICENSE.txt).
@@ -1,17 +1,16 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module FantasyFaker
4
-
5
4
  module Character
6
5
  extend BaseModule
7
6
  extend self
8
7
 
9
8
  def male_first_name
10
- "%s%s" % [NAME_PREFIXES_MALE.rand, NAME_SUFIXES_MALE.rand]
9
+ '%s%s' % [fetch_data(:name_prefixes_male).rand, fetch_data(:name_sufixes_male).rand]
11
10
  end
12
11
 
13
12
  def male_nickname
14
- NICKNAMES_MALE.rand
13
+ fetch_data(:nicknames_male).rand
15
14
  end
16
15
 
17
16
  def male_name
@@ -21,11 +20,11 @@ module FantasyFaker
21
20
  end
22
21
 
23
22
  def female_first_name
24
- "%s%s" % [NAME_PREFIXES_FEMALE.rand, NAME_SUFIXES_FEMALE.rand]
23
+ '%s%s' % [fetch_data(:name_prefixes_female).rand, fetch_data(:name_sufixes_female).rand]
25
24
  end
26
25
 
27
26
  def female_nickname
28
- NICKNAMES_FEMALE.rand
27
+ fetch_data(:nicknames_female).rand
29
28
  end
30
29
 
31
30
  def female_name
@@ -33,7 +32,5 @@ module FantasyFaker
33
32
 
34
33
  ["#{first_name} #{female_nickname}", first_name].sample
35
34
  end
36
-
37
35
  end
38
-
39
36
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module FantasyFaker
2
4
  module ArrayUtils
3
5
  def self.const_array(argument)
@@ -1,19 +1,47 @@
1
- require 'fantasy_faker/utils/array_utils'
1
+ # frozen_string_literal: true
2
2
 
3
- module FantasyFaker
3
+ require 'yaml'
4
+ require_relative 'array_utils'
4
5
 
6
+ module FantasyFaker
5
7
  module BaseModule
6
-
7
8
  def const_missing(const_name)
8
- file_content = File.readlines(data_path(const_name)).map(&:strip)
9
- data = FantasyFaker::ArrayUtils.const_array(file_content)
9
+ fetch_data(const_name)
10
+ end
11
+
12
+ def fetch_data(key)
13
+ normalized_key = underscore(key.to_s)
14
+ current_locale = FantasyFaker.locale
15
+ locale_data(current_locale)[normalized_key] || common_data[normalized_key] ||
16
+ raise(KeyError, "Unknown data key: #{normalized_key}")
17
+ end
18
+
19
+ def locale_data(locale = FantasyFaker.locale)
20
+ @locale_data ||= {}
21
+ @locale_data[locale] ||= load_yaml_arrays(locale_data_path(locale))
22
+ end
10
23
 
11
- const_set(const_name, data)
12
- data
24
+ def common_data
25
+ @common_data ||= begin
26
+ raw = YAML.safe_load_file(common_data_path) || {}
27
+ section = raw[underscore(module_name)] || raw
28
+ section.transform_values { |values| FantasyFaker::ArrayUtils.const_array(values) }
29
+ end
13
30
  end
14
31
 
15
- def data_path(file_name)
16
- "#{FantasyFaker::BASE_LIB_PATH}/fantasy_faker/data/#{underscore(module_name)}/#{underscore(file_name.to_s)}"
32
+ def locale_data_path(locale)
33
+ "#{FantasyFaker::ROOT_PATH}/locales/#{locale}/#{underscore(module_name)}.yml"
34
+ end
35
+
36
+ def common_data_path
37
+ "#{FantasyFaker::ROOT_PATH}/locales/common.yml"
38
+ end
39
+
40
+ def load_yaml_arrays(path)
41
+ return {} unless File.exist?(path)
42
+
43
+ raw = YAML.safe_load_file(path) || {}
44
+ raw.transform_values { |values| FantasyFaker::ArrayUtils.const_array(values) }
17
45
  end
18
46
 
19
47
  def module_name
@@ -21,13 +49,11 @@ module FantasyFaker
21
49
  end
22
50
 
23
51
  def underscore(string)
24
- string.gsub(/::/, '/').
25
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
26
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
27
- tr("-", "_").
28
- downcase
52
+ string.gsub('::', '/')
53
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
54
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
55
+ .tr('-', '_')
56
+ .downcase
29
57
  end
30
-
31
58
  end
32
-
33
59
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FantasyFaker
4
+ VERSION = '0.1.0'
5
+ end
data/lib/fantasy_faker.rb CHANGED
@@ -1,15 +1,51 @@
1
- module FantasyFaker
2
- VERSION = '0.0.3'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'fantasy_faker/version'
4
+ require_relative 'fantasy_faker/utils/array_utils'
5
+ require_relative 'fantasy_faker/utils/base_module'
6
+ require_relative 'fantasy_faker/character'
3
7
 
4
- require 'fantasy_faker/utils/array_utils'
5
- require 'fantasy_faker/utils/base_module'
8
+ module FantasyFaker
9
+ DEFAULT_LOCALE = :en
10
+ SUPPORTED_LOCALES = %i[en pl].freeze
6
11
 
7
12
  extend BaseModule
8
13
 
9
- BASE_LIB_PATH = File.expand_path('..', __FILE__)
14
+ ROOT_PATH = File.expand_path('../..', __FILE__)
15
+
16
+ class << self
17
+ def use_locale(locale)
18
+ return @forced_locale = normalize_locale(locale) unless block_given?
19
+
20
+ previous_locale = @forced_locale
21
+ @forced_locale = normalize_locale(locale)
22
+ yield
23
+ ensure
24
+ @forced_locale = previous_locale if block_given?
25
+ end
26
+
27
+ def reset_locale
28
+ @forced_locale = nil
29
+ end
30
+
31
+ def locale
32
+ candidate = @forced_locale || i18n_locale || DEFAULT_LOCALE
33
+ SUPPORTED_LOCALES.include?(candidate) ? candidate : DEFAULT_LOCALE
34
+ end
35
+
36
+ private
37
+
38
+ def i18n_locale
39
+ return unless defined?(I18n) && I18n.respond_to?(:locale)
40
+
41
+ normalize_locale(I18n.locale)
42
+ end
43
+
44
+ def normalize_locale(locale)
45
+ return if locale.nil?
10
46
 
11
- # Load all constants.
12
- Dir["#{BASE_LIB_PATH}/fantasy_faker/*.rb"].sort.each do |filename|
13
- require "fantasy_faker/#{File.basename(filename, '.rb')}"
47
+ base = locale.to_s.split(/[-_]/).first
48
+ base&.downcase&.to_sym
49
+ end
14
50
  end
15
51
  end
@@ -0,0 +1,185 @@
1
+ ---
2
+ character:
3
+ name_prefixes_male:
4
+ - Ae
5
+ - Arn
6
+ - Abi
7
+ - Alk
8
+ - Baa
9
+ - Beia
10
+ - Bea
11
+ - Chai
12
+ - Cen
13
+ - Dorn
14
+ - Dreg
15
+ - Dov
16
+ - Dap
17
+ - Kyth
18
+ - Lave
19
+ - Tig
20
+ - Aven
21
+ - Kyl
22
+ - My
23
+ - Ang
24
+ - Dil
25
+ - Sar
26
+ - Bip
27
+ - Grai
28
+ - Soan
29
+ - Rugha
30
+ - Jadea
31
+ - Flan
32
+ - Nys
33
+ - Nil
34
+ - Amil
35
+ - Lial
36
+ - Jem
37
+ - Timo
38
+ - Est
39
+ - Esmer
40
+ - Path
41
+ - Marc
42
+ - Paer
43
+ - Aga
44
+ - Khaz
45
+ - Ach
46
+ - Peri
47
+ - Art
48
+ - Fall
49
+ - Kaa
50
+ - Miy
51
+ - Borl
52
+ - Mik
53
+ - Cind
54
+ - Wol
55
+ name_sufixes_male:
56
+ - red
57
+ - illon
58
+ - drak
59
+ - ilo
60
+ - ker
61
+ - anian
62
+ - ath
63
+ - ius
64
+ - ven
65
+ - isab
66
+ - lay
67
+ - vite
68
+ - hun
69
+ - ger
70
+ - gons
71
+ - ving
72
+ - rol
73
+ - lianth
74
+ - kall
75
+ - ven
76
+ - sharr
77
+ - cil
78
+ - unube
79
+ - ahm
80
+ - lino
81
+ - iba
82
+ - ivo
83
+ - freano
84
+ - avo
85
+ - ilo
86
+ - donar
87
+ - jako
88
+ - mina
89
+ - rinh
90
+ - lion
91
+ - manti
92
+ - onga
93
+ - tarea
94
+ - intre
95
+ - scire
96
+ - philus
97
+ - vadro
98
+ - arok
99
+ - olites
100
+ - ka
101
+ - mythe
102
+ - pil
103
+ - reni
104
+ - ra
105
+ - uan
106
+ - dester
107
+ name_prefixes_female:
108
+ - Ail
109
+ - Aid
110
+ - Ais
111
+ - Alla
112
+ - Bar
113
+ - Ber
114
+ - Cai
115
+ - Car
116
+ - Cel
117
+ - Dan
118
+ - Dar
119
+ - Dek
120
+ - Dor
121
+ - Eil
122
+ - Eal
123
+ - Fin
124
+ - Gud
125
+ - Is
126
+ - Ink
127
+ - Ill
128
+ - Jus
129
+ - Jer
130
+ - Kat
131
+ - Lin
132
+ - Lus
133
+ - Lis
134
+ - Mer
135
+ - Mud
136
+ - Nao
137
+ - Ner
138
+ - Nin
139
+ - Oi
140
+ - Oks
141
+ - Pur
142
+ - Per
143
+ - Red
144
+ - Ris
145
+ - Set
146
+ - Sus
147
+ - Tir
148
+ - Tan
149
+ - Vis
150
+ - Ver
151
+ - Xis
152
+ - Xar
153
+ - Zer
154
+ name_sufixes_female:
155
+ - ara
156
+ - ani
157
+ - asa
158
+ - aris
159
+ - ana
160
+ - ara
161
+ - era
162
+ - eris
163
+ - ena
164
+ - eka
165
+ - ea
166
+ - iri
167
+ - isi
168
+ - ira
169
+ - ina
170
+ - ien
171
+ - oka
172
+ - ona
173
+ - oli
174
+ - odi
175
+ - oni
176
+ - una
177
+ - uni
178
+ - ura
179
+ - uri
180
+ - uka
181
+ - ukis
182
+ - lini
183
+ - liri
184
+ - lida
185
+ - lisa