Created
July 15, 2015 18:45
-
-
Save scottmessinger/c62bb892087957a315f5 to your computer and use it in GitHub Desktop.
Importer with Transproc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
preprocessor = Preprocessor.build | |
processor = Processor.build | |
evaluator = InputFunctions[:eval_values, [{ 'jd' => 'jd001' }]] | |
create_authors_with_books = rom.command([ | |
:authors, [:create, [:books, [:create]]] | |
]) | |
module Flow | |
extend Transproc::Registry | |
def self.preprocess(input) | |
preprocessor.call(input) | |
end | |
def self.validate(input) | |
if input.all? { |author| Validator.new(author).valid? } | |
input | |
end | |
end | |
def self.process(input) | |
processor.call(input).map { |author| evaluator.call(author) } if input | |
end | |
def self.persist(input) | |
create_authors_with_books.call(authors: input) if input | |
end | |
end | |
result = Flow(:preprocess) >> Flow(:validate) >> Flow(:process) >> Flow(:persist) | |
result.call(raw_input) |
Great questions.
- A couple reasons. To my knowledge, Transproc needs class (or in this case, module) methods. Second, and this is a personal thing, I prefer module methods because it forces them to be stateless and pure functions. @solnic does something fascinating with ROM where he uses instances but all the instance methods don't mutate the instance, essentially making them class methods operating on a private state. Perhaps how OO should be but certainly not how it's practiced.
- That's a great question. I'm not sure, but I think so. If it could, I'm unsure if it would add more clarity. When I've used this pattern, I find I really like having the
if
logic in the method because then I can add the method to a flow and it will only execute if the params have the right parameters. Makes the methods far more resuseable and easier to compose.
We have :guard
in transproc that goes like this:
irb(main):004:0> require 'transproc/all'
=> true
irb(main):005:0> include Transproc::Registry
irb(main):006:0> import Transproc::Conditional
=> main
irb(main):007:0> t(:guard, -> input { input > 1 }, -> input { puts 'MORE THAN 1!' })[2]
MORE THAN 1!
=> nil
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have 2 questions:
Flow
module instead of class methods? Why class methods?if input
part be abstracted or moved toTransproc
?