-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #18
- Loading branch information
Showing
9 changed files
with
269 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
module Rubanok | ||
module DSL | ||
# Adds `.process` method to Processor to define a nested processor: | ||
# | ||
# process :filter do | ||
# map :status do |status:| | ||
# raw.where(status:) | ||
# end | ||
# | ||
module Process | ||
class Rule < Rubanok::Rule | ||
METHOD_PREFIX = "__process" | ||
|
||
def initialize(...) | ||
super | ||
raise ArgumentError, "Nested processor requires exactly one field" if fields.size != 1 | ||
@field = fields.first | ||
end | ||
|
||
def define_processor(superclass, &block) | ||
@processor = Class.new(superclass, &block) | ||
end | ||
|
||
def process(input, params) | ||
return input if params.nil? | ||
|
||
subparams = fetch_value(params, field) | ||
return input if subparams == UNDEFINED | ||
|
||
return input unless subparams.respond_to?(:transform_keys) | ||
|
||
# @type var subparams : params | ||
processor.call(input, subparams) | ||
end | ||
|
||
private | ||
|
||
attr_reader :processor, :field | ||
|
||
def build_method_name | ||
"#{METHOD_PREFIX}#{super}" | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def process(field, superclass: ::Rubanok::Processor, activate_on: [field], activate_always: false, ignore_empty_values: Rubanok.ignore_empty_values, filter_with: nil, &block) | ||
filter = filter_with | ||
|
||
if filter.is_a?(Symbol) | ||
respond_to?(filter) || raise( | ||
ArgumentError, | ||
"Unknown class method #{filter} for #{self}. " \ | ||
"Make sure that a filter method is defined before the call to .map." | ||
) | ||
filter = method(filter) | ||
end | ||
|
||
rule = Rule.new([field], activate_on: activate_on, activate_always: activate_always, ignore_empty_values: ignore_empty_values, filter_with: filter) | ||
rule.define_processor(superclass, &block) | ||
|
||
define_method(rule.to_method_name) do |params = {}| | ||
rule.process(raw, params) | ||
end | ||
|
||
add_rule rule | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module Rubanok | ||
module DSL | ||
module Process : _Processor | ||
class Rule < Rubanok::Rule | ||
METHOD_PREFIX: String | ||
|
||
attr_reader field: field | ||
attr_reader processor: singleton(Processor) | ||
|
||
%a{rbs:test:skip} def define_processor: (singleton(Processor)) { () -> void } -> void | ||
def process: (input, params | nil) -> input | ||
|
||
private | ||
def build_method_name: () -> String | ||
end | ||
|
||
module ClassMethods : Module, _RulesAdding | ||
%a{rbs:test:skip} def process: (field, superclass: singleton(Processor), ?activate_on: (field | Array[field]), ?activate_always: bool, ?ignore_empty_values: bool, ?filter_with: Symbol) { () -> input } -> void | ||
def define_method: (String | Symbol) ?{ () [self: Rubanok::Processor] -> void } -> Symbol | ||
end | ||
end | ||
end | ||
end |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# frozen_string_literal: true | ||
|
||
describe "Plane.process" do | ||
let(:input) do | ||
[ | ||
{ | ||
name: "Dexter", | ||
occupation: "vocal", | ||
status: "forever" | ||
}, | ||
{ | ||
name: "Noodles", | ||
occupation: "guitar", | ||
status: "forever" | ||
}, | ||
{ | ||
name: "Ron", | ||
occupation: "drums", | ||
status: "past" | ||
}, | ||
{ | ||
name: "Greg", | ||
occupation: "bas", | ||
status: "past" | ||
}, | ||
{ | ||
name: "Todd", | ||
occupation: "bas", | ||
status: "active" | ||
} | ||
].freeze | ||
end | ||
|
||
let(:params) { {} } | ||
|
||
subject { plane.call(input, params) } | ||
|
||
context "single argument" do | ||
let(:plane) do | ||
Class.new(Rubanok::Plane) do | ||
process :filter do | ||
map :status do |status:| | ||
raw.select { _1[:status] == status } | ||
end | ||
|
||
map :occupation do |occupation:| | ||
raw.select { _1[:occupation] == occupation } | ||
end | ||
end | ||
end | ||
end | ||
|
||
specify "no matching params" do | ||
expect(subject).to eq input | ||
end | ||
|
||
specify "with matching param and value (status)" do | ||
params[:filter] = {status: "past"} | ||
|
||
expect(subject).to match_array( | ||
[ | ||
{ | ||
name: "Ron", | ||
occupation: "drums", | ||
status: "past" | ||
}, | ||
{ | ||
name: "Greg", | ||
occupation: "bas", | ||
status: "past" | ||
} | ||
] | ||
) | ||
end | ||
|
||
specify "with matching param and value (occupation)" do | ||
params[:filter] = {occupation: "bas"} | ||
|
||
expect(subject).to match_array([ | ||
{ | ||
name: "Greg", | ||
occupation: "bas", | ||
status: "past" | ||
}, | ||
{ | ||
name: "Todd", | ||
occupation: "bas", | ||
status: "active" | ||
} | ||
]) | ||
end | ||
end | ||
|
||
context "nested process" do | ||
let(:plane) do | ||
Class.new(Rubanok::Plane) do | ||
process :filter do | ||
map :status do |status:| | ||
raw | ||
end | ||
|
||
process :name do | ||
map :dexter do |*| | ||
raw.select { _1[:name] == "Dexter" } | ||
end | ||
|
||
map :noodles do |*| | ||
raw.select { _1[:name] == "Noodles" } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
specify do | ||
params[:filter] = {name: {"noodles" => "1"}} | ||
|
||
expect(subject).to match_array([ | ||
{ | ||
name: "Noodles", | ||
occupation: "guitar", | ||
status: "forever" | ||
} | ||
]) | ||
end | ||
|
||
specify "when nested value is not a Hash-like" do | ||
params[:filter] = {name: "noodles"} | ||
|
||
expect(subject).to eq input | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
begin | ||
require "pry-byebug" | ||
require "debug" | ||
rescue LoadError | ||
end | ||
|
||
|