Module: MuchFactory

Extended by:
MuchFactory
Included in:
MuchFactory
Defined in:
lib/much-factory.rb,
lib/much-factory/version.rb

Defined Under Namespace

Modules: Random, TypeConverter

Constant Summary collapse

DAYS_IN_A_YEAR =
365
SECONDS_IN_DAY =
24 * 60 * 60
VERSION =
"0.2.2"

Instance Method Summary collapse

Instance Method Details

#binaryObject



80
81
82
# File 'lib/much-factory.rb', line 80

def binary
  type_cast(Random.binary, :binary)
end

#booleanObject



84
85
86
# File 'lib/much-factory.rb', line 84

def boolean
  type_cast(Random.integer.even?, :boolean)
end

#dateObject



23
24
25
26
# File 'lib/much-factory.rb', line 23

def date
  @date ||= type_cast(Random.date_string, :date)
  @date + Random.integer(DAYS_IN_A_YEAR)
end

#datetimeObject



33
34
35
36
# File 'lib/much-factory.rb', line 33

def datetime
  @datetime ||= type_cast(Random.datetime_string, :datetime)
  @datetime + (Random.float(DAYS_IN_A_YEAR) * SECONDS_IN_DAY).to_i
end

#dir_path(length = nil) ⇒ Object Also known as: path



62
63
64
# File 'lib/much-factory.rb', line 62

def dir_path(length = nil)
  type_cast(Random.dir_path_string(length), :string)
end

#email(domain = nil, length = nil) ⇒ Object



76
77
78
# File 'lib/much-factory.rb', line 76

def email(domain = nil, length = nil)
  type_cast(Random.email_string(domain, length), :string)
end

#file_name(length = nil) ⇒ Object



58
59
60
# File 'lib/much-factory.rb', line 58

def file_name(length = nil)
  type_cast(Random.file_name_string(length), :string)
end

#file_pathObject



66
67
68
# File 'lib/much-factory.rb', line 66

def file_path
  type_cast(Random.file_path_string, :string)
end

#float(max = nil, precision: Factory.integer(5)) ⇒ Object



15
16
17
18
# File 'lib/much-factory.rb', line 15

def float(max = nil, precision: Factory.integer(5))
  factor = (10 ** precision).to_f
  (type_cast(Random.float(max), :float) * factor).round / factor
end

#hex(length = nil) ⇒ Object



54
55
56
# File 'lib/much-factory.rb', line 54

def hex(length = nil)
  type_cast(Random.hex_string(length), :string)
end

#integer(max = nil) ⇒ Object



11
12
13
# File 'lib/much-factory.rb', line 11

def integer(max = nil)
  type_cast(Random.integer(max), :integer)
end

#slug(length = nil) ⇒ Object



50
51
52
# File 'lib/much-factory.rb', line 50

def slug(length = nil)
  type_cast(Random.string(length || 5), :string)
end

#string(length = nil) ⇒ Object



38
39
40
# File 'lib/much-factory.rb', line 38

def string(length = nil)
  type_cast(Random.string(length || 10), :string)
end

#symbol(*args) ⇒ Object



42
43
44
# File 'lib/much-factory.rb', line 42

def symbol(*args)
  string(*args).to_sym
end

#text(length = nil) ⇒ Object



46
47
48
# File 'lib/much-factory.rb', line 46

def text(length = nil)
  type_cast(Random.string(length || 20), :string)
end

#timeObject



28
29
30
31
# File 'lib/much-factory.rb', line 28

def time
  @time ||= type_cast(Random.time_string, :time)
  @time + (Random.float(DAYS_IN_A_YEAR) * SECONDS_IN_DAY).to_i
end

#type_cast(value, type) ⇒ Object



88
89
90
# File 'lib/much-factory.rb', line 88

def type_cast(value, type)
  type_converter.send(type, value)
end

#type_converterObject



92
93
94
# File 'lib/much-factory.rb', line 92

def type_converter
  TypeConverter
end

#url(host = nil, length = nil) ⇒ Object



72
73
74
# File 'lib/much-factory.rb', line 72

def url(host = nil, length = nil)
  type_cast(Random.url_string(host, length), :string)
end