Class: Faker::Types

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/types.rb

Constant Summary collapse

CHARACTERS =
('0'..'9').to_a + ('a'..'z').to_a
SIMPLE_TYPES =
%i[string fixnum].freeze
COMPLEX_TYPES =
%i[hash array].freeze

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, generate, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, shuffle!, translate, unique, with_locale

Class Method Details

.characterString

Produces a random character from the a-z, 0-9 ranges.

Examples:

Faker::Types.character #=> "n"

Returns:

  • (String)

Available since:

  • 1.8.6



35
36
37
# File 'lib/faker/default/types.rb', line 35

def character
  sample(CHARACTERS)
end

.complex_rb_hash(number: 1) ⇒ Hash

Produces a random complex hash with random keys and values where the values may include other hashes and arrays.

Examples:

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"]}

Parameters:

  • number (Integer) (defaults to: 1)

    Specifies the number of key-value pairs.

Returns:

  • (Hash)

Available since:

  • 1.8.6



85
86
87
# File 'lib/faker/default/types.rb', line 85

def complex_rb_hash(number: 1)
  rb_hash(number: number, type: -> { random_complex_type })
end

.random_complex_typeString, Integer

Produces a random complex type that’s either a String, an Integer, an array or a hash.

Examples:

Faker::Types.random_complex_type #=> 1 or "a" or "bob" or {foo: "bar"}

Returns:

  • (String, Integer)

Available since:

  • 1.8.6



137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/faker/default/types.rb', line 137

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

.random_typeString, Integer

Produces a random type that’s either a String or an Integer.

Examples:

Faker::Types.random_type #=> 1 or "a" or "bob"

Returns:

  • (String, Integer)

Available since:

  • 1.8.6



118
119
120
121
122
123
124
125
126
# File 'lib/faker/default/types.rb', line 118

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

.rb_array(len: 1, type: -> { random_type }) ⇒ Array

Produces a random array.

Examples:

Faker::Types.rb_array #=> ["a"]
Faker::Types.rb_array(len: 4) #=> ["a", 1, 2, "bob"]
Faker::Types.rb_array(len: 2, type: -> { Faker::Types.rb_string }) #=> ["cat", "foo"]

Parameters:

  • len (Integer) (defaults to: 1)

    Specifies the number of elements in the array.

Returns:

  • (Array)

Available since:

  • 1.8.6



101
102
103
104
105
106
107
# File 'lib/faker/default/types.rb', line 101

def rb_array(len: 1, type: -> { random_type })
  [].tap do |ar|
    len.times do
      ar.push type.is_a?(Proc) ? type.call : type
    end
  end
end

.rb_hash(number: 1, type: -> { random_type }) ⇒ Hash

Produces a random hash with random keys and values.

Examples:

Faker::Types.rb_hash #=> {name: "bob"}
Faker::Types.rb_hash(number: 1) #=> {name: "bob"}
Faker::Types.rb_hash(number: 2) #=> {name: "bob", last: "marley"}

Parameters:

  • number (Integer) (defaults to: 1)

    Specifies the number of key-value pairs.

Returns:

  • (Hash)

Available since:

  • 1.8.6



64
65
66
67
68
69
70
71
# File 'lib/faker/default/types.rb', line 64

def rb_hash(number: 1, type: -> { random_type })
  {}.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

.rb_integer(from: 0, to: 100) ⇒ Integer

Produces a random integer.

Examples:

Faker::Types.rb_integer #=> 1

Returns:

  • (Integer)

Available since:

  • 1.8.6



48
49
50
# File 'lib/faker/default/types.rb', line 48

def rb_integer(from: 0, to: 100)
  rand(from..to).to_i
end

.rb_string(words: 1) ⇒ String

Produces a random String created from word (Faker::Lorem.word)

Examples:

Faker::Types.rb_string #=> "foobar"

Returns:

  • (String)

Available since:

  • 1.8.6



19
20
21
22
23
24
# File 'lib/faker/default/types.rb', line 19

def rb_string(words: 1)
  resolved_num = resolve(words)
  word_list = translate('faker.lorem.words')
  word_list *= ((resolved_num / word_list.length) + 1)
  sample(word_list, resolved_num).join(' ')
end