Last active
December 18, 2018 15:34
-
-
Save serradura/c3ff443e8b7882e7b17b840404a4c4a4 to your computer and use it in GitHub Desktop.
Exemplo de rake task com Functional Objects (Rubyconf Brasil 2018)
This file contains hidden or 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
| class TaskArgumentsCalculator | |
| def initialize(operator) | |
| @operator = operator | |
| end | |
| def call(_task, args) | |
| puts map_numbers(args).reduce(&@operator) | |
| end | |
| def to_proc | |
| -> (*args) { call(*args) } | |
| end | |
| private | |
| def map_numbers(args) | |
| [:a, :b].map { |key| args[key].to_f } | |
| end | |
| end | |
| SumTaskArgs = TaskArgumentsCalculator.new(:+) | |
| SubtractTaskArgs = TaskArgumentsCalculator.new(:-) | |
| desc 'adds two numbers' | |
| task :sum, [:a, :b], &SumTaskArgs | |
| desc 'subtract two numbers.' | |
| task :sub, [:a, :b], &SubtractTaskArgs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment