Class: FakeData::Method::Control
- Inherits:
-
FakeData::Method
- Object
- FakeData::Method
- FakeData::Method::Control
- Defined in:
- lib/fake_data/method/control.rb
Constant Summary
Constants inherited from FakeData::Method
ARGUMENT_SPLIT_MATCHER, LEFT_BRACKET_MATCHER, METHOD_MATCHER, RIGHT_BRACKET_MATCHER
Instance Attribute Summary collapse
-
#call_block ⇒ Object
readonly
Returns the value of attribute call_block.
Attributes inherited from FakeData::Method
#arguments, #klass, #method, #raw
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(raw, block_content = nil, &block) ⇒ Control
constructor
A new instance of Control.
- #source ⇒ Object
Methods inherited from FakeData::Method
Constructor Details
#initialize(raw, block_content = nil, &block) ⇒ Control
Returns a new instance of Control.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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 |
Instance Attribute Details
#call_block ⇒ Object (readonly)
Returns the value of attribute call_block.
4 5 6 |
# File 'lib/fake_data/method/control.rb', line 4 def call_block @call_block end |
Class Method Details
.maybe(probability = 50, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# 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 |
.repeat(count, params = {}, &block) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 |
# 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 |
Instance Method Details
#source ⇒ Object
44 45 46 |
# File 'lib/fake_data/method/control.rb', line 44 def source "#{super} do #{@call_block} end" if @call_block end |