Module: Tekido::Methods::ClassMethods

Defined in:
lib/tekido/methods/date.rb,
lib/tekido/methods/array.rb,
lib/tekido/methods/string.rb,
lib/tekido/methods/boolean.rb,
lib/tekido/methods/numeric.rb,
lib/tekido/methods/percentage.rb

Constant Summary collapse

UPPER_CHARS =
('A'..'Z').to_a
LOWER_CHARS =
('a'..'z').to_a
NUMBER_CHARS =
('0'..'9').to_a
EXAMPLE_DOMAINS =
["example.com", "exmple.net", "example.org"] +
["example.jp", "example.co.jp", "example.ne.jp"] +
(0..9).map { |i| ["example#{i}.jp", "example#{i}.co.jp", "example#{i}.ne.jp"] }.flatten

Instance Method Summary collapse

Instance Method Details

#birthday(arg = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tekido/methods/date.rb', line 23

def birthday(arg = nil)
  if arg.nil?
    min = (Date.today << 12 * 101) + 1
    date(min..Date.today)
  elsif arg.is_a?(Integer)
    min = (Date.today << 12 * (arg + 1)) + 1
    max = Date.today << 12 * arg
    date(min..max)
  elsif arg.is_a?(Range) && arg.min.is_a?(Integer)
    min = (Date.today << 12 * (arg.max + 1)) + 1
    max = Date.today << 12 * arg.min
    date(min..max)
  end
end

#date(arg = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tekido/methods/date.rb', line 7

def date(arg = nil)
  min, max = nil, nil
  if arg.nil?
    min, max = Date.new(1, 1, 1), Date.new(9999, 12, 31)
  elsif arg.is_a?(Integer)
    min, max = Date.new(arg, 1, 1), Date.new(arg, 12, 31)
  elsif arg.is_a?(Range)
    if arg.min.is_a?(Integer)
      min, max = Date.new(arg.min, 1, 1), Date.new(arg.max, 12, 31)
    elsif arg.min.is_a?(Date)
      min, max = arg.min, arg.max
    end
  end
  min ? min + integer((max - min).to_i) : nil
end

#email(*bases) ⇒ Object



23
24
25
26
27
# File 'lib/tekido/methods/string.rb', line 23

def email(*bases)
  local = string(size: 3..32, components: [:lower, :number])
  domains = bases.empty? ? EXAMPLE_DOMAINS : bases.map { |b| b.index("@") ? b[b.index('@').to_i+1..b.size] : b }
  "#{local}@#{domains.sample}"
end

#float(arg = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/tekido/methods/numeric.rb', line 14

def float(arg = nil)
  if arg.nil?
    rand
  elsif arg.is_a?(Range)
    rand(arg.min.to_f..arg.max.to_f)
  else
    rand * arg.to_f
  end
end

#integer(arg = nil) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/tekido/methods/numeric.rb', line 5

def integer(arg = nil)
  if arg.nil?
    max = 2 ** 30 - 1
    rand(0..max)
  else
    rand(arg).to_i
  end
end

#list(size, value_defs) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/tekido/methods/array.rb', line 5

def list(size, value_defs)
  range_and_values = range_and_values_from(value_defs)

  Array.new(size) do |i|
    per = percent_as(:float)
    match = range_and_values.find { |item| item.first.include?(per) }
    match ? match.last : nil
  end
end

#percentObject



5
6
7
# File 'lib/tekido/methods/percentage.rb', line 5

def percent
  percent_as(:integer)
end

#percent_as(type) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/tekido/methods/percentage.rb', line 9

def percent_as(type)
  per = rand(0.0..100.0)
  case type
  when :float
    per
  when :integer
    per.to_i
  when :mo5
    per.to_i / 5 * 5
  when :deca, :mo10
    per.to_i / 10 * 10
  end
end

#string(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/tekido/methods/string.rb', line 12

def string(options = {})
  size = size_from(options[:size])
  chars = chars_from(options[:chars], *options[:components])

  "".tap do |str|
    size.times do
      str << chars.sample
    end
  end
end

#yes?(percent = 50) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/tekido/methods/boolean.rb', line 5

def yes?(percent = 50)
  percent_as(:float) <= (percent.is_a?(Numeric) ? percent : 50)
end