Sha256: be1c9653b278d2e4a9788a664f582dd3be4d477c26857eabe4e42bf26a4ead98

Contents?: true

Size: 887 Bytes

Versions: 1

Compression:

Stored size: 887 Bytes

Contents

# option_initializer

Provides syntactic sugar for constructing objects with method chaining.

## Installation

```
gem install option_initializer
```

## Usage

```ruby
require 'option_initializer'

class Person
  include OptionInitializer
  option_initializer :id, :name, :age, :greetings

  def initialize opts
    @options = opts
  end

  def say_hello
    puts @options[:greetings].call @options[:name]
  end
end

# Then
john = Person.
         name('John Doe').
         age(19).
         greetings { |name| "Hi, I'm #{name}!" }.
         id(1000).
         new

# becomes equivalent to
john = Person.new(
         :id => 1000,
         :name => 'John Doe',
         :age => 19,
         :greetings => proc { |name| "Hi, I'm #{name}!" }
       )

# Method call shortcut
Person.
  name('John Doe').
  age(19).
  greetings { |name| "Hi, I'm #{name}!" }.
  id(1000).
  say_hello
```

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
option_initializer-1.1.0 README.md