Sha256: b98d1969e0d7f28a4a0ebc1d19ba74bc29b3f87101dfde50d99288a296a96476

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

# Object Identifier

[![Gem Version](https://badge.fury.io/rb/object_identifier.png)](http://badge.fury.io/rb/object_identifier)

Object Identifier allows quick, easy, and uniform identification of an object
by inspecting its class name and any desirable attributes/methods. This is great
for logging, notifications or any other purpose.

For example, instead of typing out string interpolations such as
`"#{some_object.class.name}[id:#{some_object.id}, name:'#{some_object.name}']"`
all over the place in controllers or in rescue blocks in models, etc., you can
now just use `"#{some_object.identify(:id, :name)}"`.


## Compatibility

Tested with:

* Ruby: MRI 2.0.0
* Rails: 4.0.1

## Installation

Add this line to your application's Gemfile:

```ruby
gem "object_identifier"
```

And then execute:

```ruby
bundle
```

## Usage

<b>Defaults</b>

Outputs the `id` attribute by default, if possible and if no other attributes
are given:

```ruby
some_object.identify  # => Movie[id:1]
```

Also works with methods:

```ruby
some_object.identify(:get_rating)  # => Movie[get_rating:"7/10"]
```

<b>Unknown Attributes/Methods</b>

If the object doesn't respond to a specified attribute/method it is simply
ignored:

```ruby
some_object.identify(:gobble_gobble, :id)  # => Movie[id:1]
```

<b>Collections</b>

Works great with collections:

```ruby
[some_object, some_other_object].identify(:id, :name)
  # => Movie[id:1, name:"Pi"], Contact[id:23, name:"Bob"]
```

Also allows limiting of results:

```ruby
[some_object, some_other_object].identify(:id, :name, limit: 1)
  # => Movie[id:1, name:"Pi"], ... (1 more)
```

<b>Overriding the Class Name</b>

```ruby
some_object.identify(klass: "MyMovie")       # => MyMovie[id:1]
some_object.identify(klass: nil)             # => [id:1]
delayed_job.identify(klass: "Delayed::Job")  # => Delayed::Job[id:1]
```

<b>Nils and Empty Collections</b>

```ruby
nil.identify(:id, :name)  # => [no objects]
[].identify               # => [no objects]
```

## Authors

- Paul Dobbins
- Evan Sherwood

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
object_identifier-0.0.3 README.md
object_identifier-0.0.2 README.md