Sha256: d20746860338808dd5f39839aeb06426d890adc5e48a9f108583f5c361db9757

Contents?: true

Size: 1007 Bytes

Versions: 5

Compression:

Stored size: 1007 Bytes

Contents

# Basic usage

Creating a decorator is as simple as inheriting from `Pragma::Decorator::Base`:

```ruby
module API
  module V1
    module User
      module Decorator
        class Resource < Pragma::Decorator::Base
          property :id
          property :email
          property :full_name
        end
      end
    end
  end
end
```

Just instantiate the decorator by passing it an object to decorate, then call `#to_hash` or
`#to_json`:

```ruby
decorator = API::V1::User::Decorator::Resource.new(user)
decorator.to_json
```

This will produce the following JSON:

```json
{
  "id": 1,
  "email": "jdoe@example.com",
  "full_name": "John Doe"
}
```

Since Pragma::Decorator is built on top of [ROAR](https://github.com/apotonick/roar) (which, in
turn, is built on top of [Representable](https://github.com/apotonick/representable)), you should
consult their documentation for the basic usage of decorators; the rest of this section only covers
the features provided specifically by Pragma::Decorator.

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pragma-decorator-1.3.0 doc/01-basic-usage.md
pragma-decorator-1.2.0 doc/01-basic-usage.md
pragma-decorator-1.1.0 doc/01-basic-usage.md
pragma-decorator-1.0.1 doc/01-basic-usage.md
pragma-decorator-1.0.0 doc/01-basic-usage.md