Project

batch_any

0.0
No commit activity in last 3 years
No release in over 3 years
Allows you to use the batching service both for grouping requests into one and a single \ access to api. It makes it easy to integrate the batching service into the current logic without huge refactoring.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.14
~> 10.0
~> 3.0
 Project Readme

BatchAny

Allows you to use the batching service both for grouping requests into one and a single access to api. It makes it easy to integrate the batching service into the current logic without huge refactoring. Internally it uses Fibers to pause current control flow and resumes after batches are formed.

Installation

Add this line to your application's Gemfile:

gem 'batch_any'

And then execute:

$ bundle

Or install it yourself as:

$ gem install batch_any

Usage

STORAGE = [:a, :b].freeze

class Service < BatchAny::Service
  @@fetch_count = 0

  def self.fetch_count
    @@fetch_count
  end

  def can_serve?(item)
    item.class == Request
  end

  def fetch
    @@fetch_count += 1
    items.each { |item| item.result { STORAGE.fetch(item.index) } }
  end
end

class Request < BatchAny::Item
  attr_reader :index

  def initialize(index)
    @index = index
  end

  def service_class
    Service
  end
end

a = nil
b = nil

batching_manager = BatchAny::Manager.new
batching_manager.add_computation { a = Request.new(0).fetch }
batching_manager.add_computation { b = Request.new(1).fetch }
batching_manager.run

puts a
# => a
puts b
# => b
puts Service.fetch_count
# => 1 # not 2

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/DmitryBochkarev/batch_any.

License

The gem is available as open source under the terms of the MIT License.