Skip to content

0.3.0

Compare
Choose a tag to compare
@palkan palkan released this 21 Oct 14:38
· 32 commits to master since this release

Features

  • Add filter_with: * option to map to allow filtering the input value.

The main use case is to filter arrays to reject blank values:

class PostsProcessor < Rubanok::Processor
  # We can pass a Proc
  map :ids, filter_with: ->(vals) { vals.reject(&:blank?).presence } do |ids:|
    raw.where(id: ids)
  end

  # or define a class method
  def self.non_empty_array(val)
    non_blank = val.reject(&:blank?)
    return if non_blank.empty?

    non_blank
  end

  # and pass its name as a filter_with value
  map :ids, filter_with: :non_empty_array do |ids:|
    raw.where(id: ids)
  end
end

# Filtered values are used in rules
PostsProcessor.call(Post.all, {ids: ["1", ""]}) == Post.where(id: ["1"])

# When filter returns empty value, the rule is not applied
PostsProcessor.call(Post.all, {ids: [nil, ""]}) == Post.all
  • Add prepare DSL method to transform the input before the first rule is activated.

Useful when you want to perform some default transformations or build the input scaffold (when no input is specified):

class CourseSearchQueryProcessor < Rubanok::Processor
  prepare do
    next if raw&.dig(:query, :bool)

    {query: {bool: {filters: []}}}
  end

  map :ids do |ids:|
    raw.dig(:query, :bool, :filters) << {terms: {id: ids}}
    raw
  end
end

Changes

  • Allow specifying ignore_empty_values: * per rule.

Now you choose which rules should ignore (or not) empty values:

map :name, ignore_empty_values: false do |name:|
  raw.where(name: name)
end