Repository navigation
Expand file tree
/
Copy pathtypes.rb
More file actions
182 lines (165 loc) · 5.07 KB
/
Copy pathtypes.rb
File metadata and controls
182 lines (165 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# frozen_string_literal: true
module Faker
class Types < Base
CHARACTERS = ('0'..'9').to_a + ('a'..'z').to_a
SIMPLE_TYPES = %i[string fixnum].freeze
COMPLEX_TYPES = %i[hash array].freeze
class << self
##
# Produces a random String created from word (Faker::Lorem.word)
#
# @return [String]
#
# @example
# Faker::Types.rb_string #=> "foobar"
#
# @faker.version 1.8.6
def rb_string(legacy_words = NOT_GIVEN, words: 1)
warn_for_deprecated_arguments do |keywords|
keywords << :words if legacy_words != NOT_GIVEN
end
resolved_num = resolve(words)
word_list =
translate('faker.lorem.words')
word_list *= ((resolved_num / word_list.length) + 1)
shuffle(word_list)[0, resolved_num].join(' ')
end
##
# Produces a random character from the a-z, 0-9 ranges.
#
# @return [String]
#
# @example
# Faker::Types.character #=> "n"
#
# @faker.version 1.8.6
def character
sample(CHARACTERS)
end
##
# Produces a random integer.
#
# @return [Integer]
#
# @example
# Faker::Types.rb_integer #=> 1
#
# @faker.version 1.8.6
def rb_integer(legacy_from = NOT_GIVEN, legacy_to = NOT_GIVEN, from: 0, to: 100)
warn_for_deprecated_arguments do |keywords|
keywords << :from if legacy_from != NOT_GIVEN
keywords << :to if legacy_to != NOT_GIVEN
end
rand(from..to).to_i
end
##
# Produces a random hash with random keys and values.
#
# @param number [Integer] Specifies the number of key-value pairs.
# @return [Hash]
#
# @example
# Faker::Types.rb_hash #=> {name: "bob"}
# Faker::Types.rb_hash(number: 1) #=> {name: "bob"}
# Faker::Types.rb_hash(number: 2) #=> {name: "bob", last: "marley"}
#
# @faker.version 1.8.6
def rb_hash(legacy_number = NOT_GIVEN, legacy_type = NOT_GIVEN, number: 1, type: -> { random_type })
warn_for_deprecated_arguments do |keywords|
keywords << :number if legacy_number != NOT_GIVEN
keywords << :type if legacy_type != NOT_GIVEN
end
{}.tap do |hsh|
Lorem.words(number: number * 2).uniq.first(number).each do |s|
value = type.is_a?(Proc) ? type.call : type
hsh.merge!(s.to_sym => value)
end
end
end
##
# Produces a random complex hash with random keys and values where the values may include other hashes and arrays.
#
# @param number [Integer] Specifies the number of key-value pairs.
# @return [Hash]
#
# @example
# Faker::Types.complex_rb_hash #=> {user: {first: "bob", last: "marley"}}
# Faker::Types.complex_rb_hash(number: 1) #=> {user: {first: "bob", last: "marley"}}
# Faker::Types.complex_rb_hash(number: 2) #=> {user: {first: "bob", last: "marley"}, son: ["damien", "marley"]}
#
# @faker.version 1.8.6
def complex_rb_hash(legacy_number = NOT_GIVEN, number: 1)
warn_for_deprecated_arguments do |keywords|
keywords << :number if legacy_number != NOT_GIVEN
end
rb_hash(number: number, type: -> { random_complex_type })
end
##
# Produces a random array.
#
# @param len [Integer] Specifies the number of elements in the array.
# @return [Array]
#
# @example
# Faker::Types.rb_array #=> ["a"]
# Faker::Types.rb_array(len: 4) #=> ["a", 1, 2, "bob"]
#
# @faker.version 1.8.6
def rb_array(legacy_len = NOT_GIVEN, len: 1)
warn_for_deprecated_arguments do |keywords|
keywords << :len if legacy_len != NOT_GIVEN
end
[].tap do |ar|
len.times do
ar.push random_type
end
end
end
##
# Produces a random type that's either a String or an Integer.
#
# @return [String, Integer]
#
# @example
# Faker::Types.random_type #=> 1 or "a" or "bob"
#
# @faker.version 1.8.6
def random_type
type_to_use = SIMPLE_TYPES[rand(0..SIMPLE_TYPES.length - 1)]
case type_to_use
when :string
rb_string
when :fixnum
rb_integer
end
end
##
# Produces a random complex type that's either a String, an Integer, an array or a hash.
#
# @return [String, Integer]
#
# @example
# Faker::Types.random_complex_type #=> 1 or "a" or "bob" or {foo: "bar"}
#
# @faker.version 1.8.6
def random_complex_type
types = SIMPLE_TYPES + COMPLEX_TYPES
type_to_use = types[rand(0..types.length - 1)]
case type_to_use
when :string
rb_string
when :fixnum
rb_integer
when :hash
rb_hash
when :array
rb_array
end
end
private
def titleize(word)
word.split(/(\W)/).map(&:capitalize).join
end
end
end
end