Class: Feiku::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/feiku/generator.rb

Overview

Generator is a behind-scene class that generates fake data

Constant Summary collapse

RANDOM_STRING =
[*"a".."z", *"A".."Z"].freeze
RANDOM_INTEGER =
[*0..9].freeze

Instance Method Summary collapse

Constructor Details

#initialize(format:, value:, length:, pool_size: 0, &block) ⇒ Generator

rubocop:disable Metrics/MethodLength



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/feiku/generator.rb', line 14

def initialize(format:, value:, length:, pool_size: 0, &block) # rubocop:disable Metrics/MethodLength
  @length = length
  @value = value
  @format, @size = if value.is_a?(Hash)
                     [format, value.size]
                   else
                     n = 0
                     f = format.gsub(/%\{[^}]+\}/) { n += 1 and "%s" }
                     [f, n]
                   end
  @pool = pool_size.zero? ? nil : Array.new(pool_size) { _generate }
  self.class.class_eval(&block) if block_given?
end

Instance Method Details

#generateObject



28
29
30
# File 'lib/feiku/generator.rb', line 28

def generate
  @pool.nil? ? _generate : @pool.sample
end