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 =
[:string, :fixnum]
COMPLEX_TYPES =
[:hash, :array]

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, sample, shuffle, translate, unique, with_locale

Class Method Details

.array(len = 1) ⇒ Object


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

def array(len=1)
  Array.new.tap do |ar|
    len.times do
      ar.push self.random_type
    end
  end
end

.characterObject


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

def character
  sample(CHARACTERS)
end

.complex_hash(key_count = 1) ⇒ Object


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

def complex_hash(key_count=1)
  Hash.new.tap do |hsh|
    key_count.times do
      hsh.merge!({self.string.to_sym => self.random_complex_type})
    end
  end
end

.hash(key_count = 1) ⇒ Object


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

def hash(key_count=1)
  Hash.new.tap do |hsh|
    key_count.times do
      hsh.merge!({self.string.to_sym => self.random_type})
    end
  end
end

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


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

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

.random_complex_typeObject


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/faker/types.rb', line 61

def random_complex_type
  types = SIMPLE_TYPES + COMPLEX_TYPES
  type_to_use = types[rand(0..types.length - 1)]
  case type_to_use
  when :string
    self.string
  when :fixnum
    self.integer
  when :hash
    self.hash
  when :array
    self.array
  else
    self.integer
  end
end

.random_typeObject


49
50
51
52
53
54
55
56
57
58
59
# 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
    self.string
  when :fixnum
    self.integer
  else
    self.integer
  end
end

.string(words = 1) ⇒ Object


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

def string(words=1)
  resolved_num = resolve(words)
  word_list = (
    translate('faker.lorem.words')
  )
  word_list = word_list * ((resolved_num / word_list.length) + 1)
  shuffle(word_list)[0, resolved_num].join(" ")
end