class Faker::Types

Constants

CHARACTERS
COMPLEX_TYPES
SIMPLE_TYPES

Public Class Methods

array(len=1) click to toggle source
# 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
character() click to toggle source
# File lib/faker/types.rb, line 17
def character
  sample(CHARACTERS)
end
complex_hash(key_count=1) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/faker/types.rb, line 21
def integer(from=0, to=100)
  rand(from..to).to_i
end
random_complex_type() click to toggle source
# 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_type() click to toggle source
# 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) click to toggle source
# 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

Private Class Methods

resolve(value) click to toggle source
# File lib/faker/types.rb, line 84
def resolve(value)
  case value
  when Array then sample(value)
  when Range then rand value
  else value
  end
end
titleize(word) click to toggle source
# File lib/faker/types.rb, line 80
def titleize(word)
  word.split(/(\W)/).map(&:capitalize).join
end