class FakeData::Method::Control

Attributes

call_block[R]

Public Class Methods

maybe(probability = 50) { || ... } click to toggle source
# File lib/fake_data/method/control.rb, line 19
def maybe probability = 50, &block
  if probability <= 0
    raise "Probability must be greater than 0%"
  elsif probability >= 100
    raise "Probability must be lesser than 100%"
  end

  yield if rand(0..100) <= probability
end
new(raw, block_content = nil, &block) click to toggle source
Calls superclass method FakeData::Method::new
# File lib/fake_data/method/control.rb, line 30
def initialize raw, block_content = nil, &block
  super(raw)

  @call_block = if block_given?
    "#{block.call}"
  elsif block_content
    raise "Block does not implements .call" unless block_content.respond_to?(:call)

    "#{block_content}"
  else
    raise "Block is not provided"
  end
end
repeat(count, params = {}) { || ... } click to toggle source
# File lib/fake_data/method/control.rb, line 7
def repeat(count, params = {}, &block)
  if count.is_a?(Range)
    count = rand(count)
  end

  result = count.times.map do
    yield
  end

  result.length == 0 && params[:nil] ? nil : result
end

Public Instance Methods

source() click to toggle source
# File lib/fake_data/method/control.rb, line 44
def source
  "#{super} do #{@call_block} end" if @call_block
end

Private Instance Methods

parse_klass(klass_string) click to toggle source
# File lib/fake_data/method/control.rb, line 54
def parse_klass klass_string
  raise_custom NameError, "'#{klass_string}' can't be presented" if klass_string
  "::FakeData::Method::Control"
end
raise_custom(error, reason) click to toggle source
# File lib/fake_data/method/control.rb, line 50
def raise_custom error, reason
  raise error, "FakeData: can't parse '#{raw}'. Reason: #{reason}. For usage see: https://github.com/stympy/faker"
end