rubanok

Parameters-based transformation DSL

MIT License

Downloads
74.4K
Stars
206
Committers
5

Bot releases are visible (Hide)

rubanok - 0.5.0 Latest Release

Published by palkan 11 months ago

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"})
rubanok - 0.4.0

Published by palkan over 3 years ago

Added RBS and Ruby 3.0 support.

rubanok - 0.3.0

Published by palkan almost 4 years ago

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
rubanok - 0.2.0

Published by palkan about 5 years ago

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
Package Rankings
Top 12.92% on Rubygems.org
Badges
Extracted from project README
Gem Version