Class: Faker::Types

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/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::Letters, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

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

Class Method Details

.characterObject



17
18
19
# File 'lib/faker/types.rb', line 17

def character
  sample(CHARACTERS)
end

.complex_rb_hash(key_count = 1) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/faker/types.rb', line 33

def complex_rb_hash(key_count = 1)
  {}.tap do |hsh|
    Lorem.words(key_count * 2).uniq.first(key_count).each do |s|
      hsh.merge!(s.to_sym => random_complex_type)
    end
  end
end

.random_complex_typeObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/faker/types.rb', line 59

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_typeObject



49
50
51
52
53
54
55
56
57
# File 'lib/faker/types.rb', line 49

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) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/faker/types.rb', line 41

def rb_array(len = 1)
  [].tap do |ar|
    len.times do
      ar.push random_type
    end
  end
end

.rb_hash(key_count = 1) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/faker/types.rb', line 25

def rb_hash(key_count = 1)
  {}.tap do |hsh|
    Lorem.words(key_count * 2).uniq.first(key_count).each do |s|
      hsh.merge!(s.to_sym => random_type)
    end
  end
end

.rb_integer(from = 0, to = 100) ⇒ Object



21
22
23
# File 'lib/faker/types.rb', line 21

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

.rb_string(words = 1) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/faker/types.rb', line 8

def rb_string(words = 1)
  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