Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sheaf

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sheaf

  • 0.1.1
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Sheaf

Immutable, composable, multi-purpose middleware stack for Ruby.

Installation

Add this line to your application's Gemfile:

gem 'sheaf'

And then execute:

$ bundle

Or install it yourself as:

$ gem install sheaf

Inside of your Ruby program, require Sheaf with:

require 'sheaf'

Usage

First of all, let's save us some typing:

include Sheaf

Using Sheaf::Stack is similar to other Ruby middleware implementations. In fact, any Object that responde to #call can be added to the stack:

class MyMiddleware
  def call(n)
    yield n + 2
  end
end

stack = Stack.new(MyMiddleware.new)

# A (optional) block can be passed to #call to capture the result.
stack.call(40) do |result|
  result
end
# => 42

Stacks can nested:

stack_a = Stack.new(MyMiddleware.new)
stack_b = Stack.new(MyMiddleware.new).add(stack_a)

stack_b.call(38) do |result|
  result
end
# => 42

Note that calling Stack#add will always return a new Stack:

stack_a = Stack.new(MyMiddleware)
stack_b = stack_a.add(MyMiddleware)

stack_b.to_a
# => [MyMiddleware, MyMiddleware]

stack_a.to_a
# => [MyMiddleware]

Stacks implement Enumerable and also respond to #fmap, which will returns a new Stack:

stack = Stack[MyMiddleware, MyMiddleware]

stack.fmap(&:new).call(38) do |result|
  result
end
# => 42

Sheaf::Stack also gives you a more traditional, block-based DSL to build your stacks:

stack = Stack.build do
  add MyMiddleware
end

stack.fmap(&:new).call(40) do |result|
  result
end
# => 42

License

Copyright 2014-2015 Tobias Svensson

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

FAQs

Package last updated on 15 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc