Yieldable

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file lib/yieldable. To experiment with that code, run bin/console for an interactive prompt.

Yieldable is meta-mixin, which makes your module, class or it's instance to respond to method +to_proc+, for example, to easily build instances during iteration:

["1", "2", "3"].map(&SomeObject)

Installation

Add this line to your application's Gemfile:

gem 'yieldable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install yieldable

Usage

It is 2 different ways to use this module: +extend+ or +prepend+ it to your class.

== extend Yieldable

class SomeObject extend Yieldable # => singleton_class.to_proc = SomeObject.method(:call).to_proc end

This way defines an attribute reader +to_proc+ in a singleton class. It sets a proc only once for a class, so it will not generate a new +Proc+ object every time, like if you would use just a method(:call).to_proc. By default it uses the +call+ class method as a source method for a proc. You can change the source method name by using brackets:

class SomeObject extend Yieldable[:new] # => singleton_class.to_proc = SomeObject.method(:new).to_proc end

It's better to ensure that a source method is defined before extending a class with +Yieldable+. In the other way it will add the +singletone_method_added+ hook and wait. After a source method will be added, the hook method will be removed.

You can avoid to removing the hook with an optional once: false passed into the brackets:

extend Yieldable[:new, once: false]

== prepend Yieldable

This way defines a +to_proc+ instance method once for a class, but a proc will be generated once for each object. You can also use a brackets like within an +extend+ way.

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/estum/yieldable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

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

Code of Conduct

Everyone interacting in the Yieldable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.