• Stars
    star
    239
  • Rank 167,855 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 12 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python's method decorators for Ruby

MethodDecorators

Python's function decorators for Ruby.

I probably wouldn't use this in production.

Installation

gem install method_decorators

Usage

Using a decorator

Extend MethodDecorators in a class where you want to use them, and then stick +DecoratorName before your method declaration to decorate the method.

require "method_decorators/memoize"

class MyMath
  extend MethodDecorators

  +MethodDecorators::Memoize
  def self.fib(n)
    if n <= 1
      n
    else
      fib(n - 1) + fib(n - 2)
    end
  end
end

puts MyMath.fib(200)

You can also decorate with an instance of a decorator, rather than the class. This is useful for configuring specific options for the decorator.

class ExternalService
  extend MethodDecorators

  +MethodDecorators::Retry.new(3)
  def request
    ...
  end
end

You can also set multiple decorators for your methods. Each decorator executes within the previously declared decorator. i.e. they are nested, as expected to be.

class ExternalService
  extend MethodDecorators

  +MethodDecorators::Retry.new(3)
  +MethodDecorators::Within.new(2.seconds)
  def request
    ...
  end
end

Included decorators

Include these with require 'method_decorators/name_of_decorator', or all at once with require 'method_decorators/all'.

  • Memoize - caches the result of the method for each arg combination it's called with
  • Retry - retries the method up to n (passed in to the constructor) times if the method errors
  • Within - times outs if a request doesn't complete within n seconds
  • Precondition - raises an error if the precondition (passed as a block) is not met

Defining a decorator

class Transactional < MethodDecorators::Decorator
  def call(wrapped, this, *args, &blk)
    ActiveRecord::Base.transaction do
      wrapped.call(*args, &blk)
    end
  end
end

License

MethodDecorators is available under the MIT license and is freely available for all use, including personal, commercial, and academic. See LICENSE for details.

More Repositories

1

mincemeatpy

Lightweight MapReduce in python
Python
479
star
2

hasu

Faster iteration on Gosu games
Ruby
64
star
3

ezing

Easing functions for Rust
Rust
50
star
4

rust-imgui-sdl2

SDL2 Input handling for imgui-rs
Rust
48
star
5

rust-imgui-opengl-renderer

OpenGL (3+) rendering for imgui-rs
Rust
37
star
6

pong

Pong
Ruby
28
star
7

mfrs

My rust utilities
Rust
11
star
8

hybrid-chess

Rust
11
star
9

rspec-celluloid

Run your RSpec suite in parallel on top of Celluloid
Ruby
9
star
10

unicorn-heroku

[Unmaintained] Unicorn that cooperates with Heroku's signal handling
Ruby
9
star
11

rubycraft

Ruby
7
star
12

abongo

(unmaintained) Ruby A/B testing on MongoDB
Ruby
7
star
13

hnd-client

[Unmaintained] HackerNews'd! client
JavaScript
6
star
14

gro

Go's concurrency model "implemented" for Ruby
Ruby
4
star
15

mygl

Software implementation of OpenGL ES 3.2
Rust
4
star
16

clueless

My LD28 entry
Python
2
star
17

mapi-kata

Microblog API Kata
Ruby
2
star
18

opool

A simple Ruby object pool
Ruby
2
star
19

enumerable-kata

Enumerable Kata
Ruby
2
star
20

mapi-kata-tigertonic-gorp

Go
2
star
21

velocity_check

Lets you know if something is happening too often
Ruby
2
star
22

hnd-server

[Unmaintained] HackerNews'd! server
Ruby
2
star
23

mapi-kata-elixir-dynamo-ecto

Elixir
1
star
24

dotfiles-old

My dotfiles
Emacs Lisp
1
star
25

gravitas

My LD29 entry
Java
1
star
26

jokevm

A toy JVM implementation
Ruby
1
star
27

wfc

Rust implementation of wave function collapse
Rust
1
star
28

gdxpong

Getting my feet wet with libgdx
Java
1
star
29

rust-pong

Pong in rust with SDL2
Rust
1
star
30

flippy_bird

Flippy Bird
Java
1
star
31

adventofcode2020

Advent of Code 2020 in Elixir
Elixir
1
star
32

rarkanoid

A quick 'n dirty arkanoid clone in Ruby
Ruby
1
star