Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cibernox/selective_inspector
Simple gem that allows you to customize how should your objects be inspected.
https://github.com/cibernox/selective_inspector
Last synced: 17 days ago
JSON representation
Simple gem that allows you to customize how should your objects be inspected.
- Host: GitHub
- URL: https://github.com/cibernox/selective_inspector
- Owner: cibernox
- License: mit
- Created: 2014-04-17T11:38:02.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-04-17T14:33:28.000Z (over 10 years ago)
- Last Synced: 2024-10-27T03:18:08.841Z (2 months ago)
- Language: Ruby
- Size: 152 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Selective Inspect
Simple gem to customize the output of the `#inspect` method.
Ruby's default `#inspect` implementation prints EVERY instance variable in your object objects, which can be really painful sometimes.
This gem allows to define a whitelist of the instance variables you want to output or a blacklist of those you don't.
## Installation
Add this line to your application's Gemfile:
gem 'selective_inspect'
And then execute:
$ bundle
Or install it yourself as:
$ gem install selective_inspect
## Usage
```rb
class Game
# ...
endgame = Game.new(difficulty: 'hard', mode: 'deathmatch')
game.inspect
# => # # Defaults inspectSelectiveInspect.perform_inspect(game, :mode)
# => # # Custom inspection# You can also include the module in those classes you want to be inspected
# in a certain way by default, and add a whitelist or blacklist of variables.
#class Player
include SelectiveInspect
inspectable_vars :id, :nickname, :score, :health, :ip_address
# ...
endclass Weapon
include SelectiveInspect
uninspectable_vars :range, :damage
# ...
end
weapon = Weapon.new(type: 'Bazooka', ammo: 3, range: 600, damage: 'max')
player = Player.new(id: 1, name: 'John', health: 100, weapon: weapon)player.inspect
# =># Even if you have included the module, you still can pass a whitelist
# of variables to inspect.
#player.inspect(:nickname, :ip_address)
# =>
```## TODOs (by priority)
1. Allow to pass a list of instance methods to inspect along with the instance variables
2. Avoid infinite recursion checking for cycles.
3. Rails environment integration: Only enable it for development and test.## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request