Skip to content

Releases: palkan/rubanok

0.5.0

04 Dec 14:56
Compare
Choose a tag to compare

Features

  • Added nested processors support.

Example:

class MyProcessor < Rubanok::Processor
  process :filter do
    match :status do
      having "draft" do
        raw.where(draft: true)
      end

      having "deleted" do
        raw.where.not(deleted_at: nil)
      end
    end
end

MyProcessor.call({filter: {status: "draft"})

0.4.0

05 Mar 18:24
Compare
Choose a tag to compare

Added RBS and Ruby 3.0 support.

0.3.0

21 Oct 14:38
Compare
Choose a tag to compare

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

0.2.0

24 Aug 01:38
Compare
Choose a tag to compare

Changes

Naming is changed from "planes" to "processors":

  • Rubanok::Plane -> Rubanok::Processor
  • planish -> rubanok_process
  • implicit_plane_class -> implicit_rubanok_class.

The older, planish, API is still available and hasn't been deprecated.

See the discussion.

Features

  • Added Process.project and rubanok_scope methods to get the Hash of recognized params.

Example:

class PostsProcessor < Rubanok::Processor
  map :q { ... }
  match :page, :per_page, activate_on: :page { ... }
end

PostsProcessor.project(q: "search_me", filter: "smth", page: 2)
# => { q: "search_me", page: 2 }

class PostsController < ApplicationController
  def index
    @filter_params = rubanok_scope
    # or
    @filter_params = rubanok_scope params.require(:filter), with: PostsProcessor
    # ...
  end
end
  • Added fail_when_no_matches parameter to match method.

The Rubanok::UnexpectedInputError is raised if there is no matching value when fail_when_no_matches: true is set:

match :filter, fail_when_no_matches: true do
  having "active" do
    raw.active
  end

  having "finished" do
    raw.finished
  end
end

You can also set this option globally:

Rubanok.fail_when_no_matches = true