bsielski Control Flow
A couple of classes for organizing objects in simple data flow structures (conditional loops, pipelines etc.).
When to use it?
Who knows. Maybe in pseudo-funtional programming.
Installation
Add this line to your application's Gemfile:
gem 'bsielski_control_flow'
And then execute:
$ bundle
Or install it yourself as:
$ gem install bsielski_control_flow
Usage
Reqiure proper class.
require "c_flow/pipeline"
Use it.
square_number_generator = CFlow:Pipeline.new(procs: [random_int, square])
All classes have just one public method: #call. This method takes always one argument.
Some classes have dependencies (injected in constructor) called "procs". They are objects that respond to #call method. This method takes one argument or variable number of arguments (it depends of object's role).
API
Class CFlow::Pipeline
It passes a value through every proc and returns a final value. Output of the previous proc is input of the next proc.
Source code:
module CFlow
class Pipeline
def initialize(procs:)
@procs = procs
end
def call(input)
@procs.each do |proc|
input = proc.call(input)
end
input
end
end
end
Require
require "c_flow/pipeline"
Constructor
CFlow:Pipeline.new(procs: procs)
Paramaters:
- procs - an array of procs or objects responding on
.call
message with one argument. The first proc takes the "main" input of the class. THe result is passed to the next proc as input. The output of the last proc is the output of .call
method of Pipeline class.
Class CFlow::Self
It returns unmodified argument.
Source code:
module CFlow
class Self
def call(input)
input
end
end
end
Require
require "c_flow/self"
Constructor
CFlow:Self.new
Class CFlow::True
It returns true no matter what input it takes.
Source code:
module CFlow
class True
def call(input)
true
end
end
end
Require
require "c_flow/true"
Constructor
CFlow:True.new
Class CFlow::False
It returns false no matter what input it takes.
Source code:
module CFlow
class False
def call(input)
false
end
end
end
Require
require "c_flow/false"
Constructor
CFlow:False.new
Class CFlow::Combine
It passes input to each helper proc, then it passes the outputs to one proc and returns the output.
Source code:
module CFlow
class Combine
def initialize(combine_proc:, sub_procs:)
@sub_procs = sub_procs
@combine_proc = combine_proc
end
def call(input)
sub_proc_outputs = []
@sub_procs.each do |sub_proc|
sub_proc_outputs << sub_proc.call(input)
end
@combine_proc.call(*sub_proc_outputs)
end
end
end
Require
require "c_flow/combine"
Constructor
CFlow::Combine.new(sub_procs: sub_procs, combine_proc: combine_proc)
Paramaters:
- sub_procs - an array of procs or objects responding on
.call
message with one argument. Each of them takes the "main" value a an argument and return an output. All aoutpus are pased to combine_proc. - combine_procs - a proc or object responding on
.call
message with one or many arguments. The output of this proc is the output of the .call
method of the Combine class.
Class CFlow::UntilTrueLoop
It passes input to condition_proc and if the result is not true it pases the input to loop_proc until the result is true.
Source code:
module CFlow
class UntilTrueLoop
def initialize(condition_proc:, loop_proc:)
@loop_proc = loop_proc
@condition_proc = condition_proc
end
def call(input)
until @condition_proc.call(input) do
input = @loop_proc.call(input)
end
input
end
end
end
Require
require "c_flow/until_true_loop"
Constructor
CFlow::UntilTrueLoop.new(condition_proc: condition_proc, loop_proc: loop_proc)
Paramaters:
- condition_proc - proc or object responding on
.call
message with one argument. At the beginning it takes the input from .call
method from UntilTrueLoop class. If an output is truthy the output is returned as the output of .call
message of UntilTrueLoop class. - loop_proc - a proc or objects responding on
.call
message with one argument.
To do features
- Examples in the documentation and source code.
- More classes.
License
The gem is available as open source under the terms of the MIT License.