<commit_before># ComposableDecorator

A simple, composable decorator for Rails models.

## Example
```ruby
class User < ActiveRecord::Base
  decorate_with NameDecorator, PhoneNumberDecorator
end
```

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'composable_decorator'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install composable_decorator

## Usage

```ruby
# Decorators are declared in the /decorators folder
#
# app/decorators/name.rb
module Name
  def full_name
    "#{first_Name} #{last_name}"
  end
end

module PhoneNumber
  def full_phone_number
    "(#{area_code}) #{phone_number}"
  end
end

# a #decorate method is declared in the model, that lists the order of decorators
# to be called on the instance. The name of this method must be the same across
# multiple models in order to decorate the AR relationships
#
# /app/models/user.rb
class User < ActiveRecord::Base
  decorate_with Name, PhoneNumber
end

# this is roughly the equivalent of wrapping the instance in first the Name decorator,
# then the PhoneNumber decorator, i.e.
class User < ActiveRecord::Base
  extend Name
  extend PhoneNumber
end

# we can then decorate the model in the controller
#
# /app/controllers/users_controller.rb
class UsersController
  def show
    ...
    @user.decorate
  end
end

# and we can access the decorated methods in the view
#
# /app/views/users/show.html.slim
@user.full_name
@user.full_phone_number

# decorators are inherited
#
#
module HatDecorator
  def hat_decorated_method
    "hat decorator method"
  end
end

class Hat < ActiveRecord::Base
  decorate_with HatDecorator
end

class Stetson < Hat
end

Stetson.new.decorate.hat_decorator_method #=> "hat decorator method"

# DELEGATIONS
#
# We can delegate the association's decorated methods to the model
class Address < ActiveRecord::Base
  decorate_with AddressDecorator
end

class User
  delegate_decorated_to :address
end

# which delegates all the decorated methods created in Address#decorate to User
#
# /app/views/users/show.html.slim
@user.address_simple_format
```
<commit_msg>[docs] clarify readme<commit_after># ComposableDecorator

A simple, composable decorator for Rails models.

## Example
```ruby
class User < ActiveRecord::Base
  decorate_with NameDecorator, PhoneNumberDecorator
end
```

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'composable_decorator'
```

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install composable_decorator

## Usage

Decorators are declared as modules.

```ruby
module NameDecorator
  def full_name
    "#{first_Name} #{last_name}"
  end
end
```

A `#decorate_with` method is called in the model that lists the order of decorators to be added to the instance.

```ruby
# /app/models/user.rb
class User < ActiveRecord::Base
  decorate_with NameDecorator, PhoneNumberDecorator
end
```

This is roughly the equivalent of wrapping the instance in first the Name decorator, then the PhoneNumber decorator, i.e.:

```ruby
class User < ActiveRecord::Base
  extend NameDecorator
  extend PhoneNumberDecorator
end
```

We can then decorate the model in the controller and access the decorated methods in the view.

```ruby
# /app/controllers/users_controller.rb
class UsersController
  def show
    ...
    @user.decorate
  end
end

# /app/views/users/show.html.slim
@user.full_name
@user.full_phone_number

```

Decorators are inherited

```ruby
module HatDecorator
  def hat_decorated_method
    "hat decorator method"
  end
end

class Hat < ActiveRecord::Base
  decorate_with HatDecorator
end

class Stetson < Hat
end

Stetson.new.decorate.hat_decorator_method #=> "hat decorator method"
```


### Delegating an association's decorated methods

We can delegate the association's decorated methods to the instance all at once.

```ruby
module AddressDecorator
  def simple_format
    "#{street}, #{city}"
  end
end

class Address < ActiveRecord::Base
  decorate_with AddressDecorator
end

class User
  delegate_decorated_to :address
end

# /app/views/users/show.html.slim
User.find(1).decorate.address_simple_format
```

{'fraction_non_alphanumeric': 0.07842664092664092, 'fraction_numerical': 0.00024131274131274132, 'mean_word_length': 4.461133069828722, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '158332', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1278, 'n_words': 443}