selective_inspect 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: faf6d9978167dd55099c7dd09fa404b2793084e2
4
+ data.tar.gz: 724072a56db579c3fae03d8021e37240c6451e9a
5
+ SHA512:
6
+ metadata.gz: a64b0ddb253889fdfb59420b2060e07561dc6a3d08893c5086babe297efa241fa1b37689df31e580e66e922cb8f84ea352f7defd8e39b2b958fac0fa0b1a5c2a
7
+ data.tar.gz: 2ef844a20f8113f58b72746750b80ff30533755146965106b8e285e1e22aae0b6d5882c07bfed0544996f9e130475cd8b42e638482e31a27440cf9b1e802f992
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in selective_inspect.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Miguel Camba
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Miguel Camba
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,78 @@
1
+ # Selective Inspect
2
+
3
+ Simple gem to customize the output of the `#inspect` method.
4
+
5
+ Ruby's default `#inspect` implementation prints EVERY instance variable in your object objects, which can be really painful sometimes.
6
+
7
+ This gem allows to define a whitelist of the instance variables you want to output or a blacklist of those you don't.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'selective_inspect'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install selective_inspect
22
+
23
+ ## Usage
24
+
25
+ ```rb
26
+ class Game
27
+ # ...
28
+ end
29
+
30
+ game = Game.new(difficulty: 'hard', mode: 'deathmatch')
31
+
32
+ game.inspect
33
+ # => #<Game:0x70238562842620 @difficulty="hard", @mode="deathmatch"> # Defaults inspect
34
+
35
+ SelectiveInspect.perform_inspect(game, :mode)
36
+ # => #<Game:0x70238562842620 @mode="deathmatch"> # Custom inspection
37
+
38
+ # You can also include the module in those classes you want to be inspected
39
+ # in a certain way by default, and add a whitelist or blacklist of variables.
40
+ #
41
+
42
+ class Player
43
+ include SelectiveInspect
44
+ inspectable_vars :id, :nickname, :score, :health, :ip_address
45
+ # ...
46
+ end
47
+
48
+ class Weapon
49
+ include SelectiveInspect
50
+ uninspectable_vars :range, :damage
51
+ # ...
52
+ end
53
+ weapon = Weapon.new(type: 'Bazooka', ammo: 3, range: 600, damage: 'max')
54
+ player = Player.new(id: 1, name: 'John', health: 100, weapon: weapon)
55
+
56
+ player.inspect
57
+ # =>
58
+
59
+ # Even if you have included the module, you still can pass a whitelist
60
+ # of variables to inspect.
61
+ #
62
+
63
+ player.inspect(:nickname, :ip_address)
64
+ # =>
65
+ ```
66
+
67
+ ## TODOs (by priority)
68
+ 1. Allow to pass a list of instance methods to inspect along with the instance variables
69
+ 2. Avoid infinite recursion checking for cycles.
70
+ 3. Rails environment integration: Only enable it for development and test.
71
+
72
+ ## Contributing
73
+
74
+ 1. Fork it
75
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
76
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
77
+ 4. Push to the branch (`git push origin my-new-feature`)
78
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,81 @@
1
+ require "ruby_selective_inspect/version"
2
+
3
+ module RubySelectiveInspect
4
+ module ClassMethods
5
+ def inspectionable_vars(*vars)
6
+ @_selective_inspector_whitelist = vars
7
+ end
8
+
9
+ def uninspectionable_vars(*vars)
10
+ @_selective_inspector_blacklist = vars
11
+ end
12
+
13
+ def get_inspectionable_vars
14
+ @_selective_inspector_whitelist ||= []
15
+ end
16
+
17
+ def get_uninspectionable_vars
18
+ @_selective_inspector_blacklist ||= []
19
+ end
20
+ end
21
+
22
+ module InstanceMethods
23
+ # Public: Inspects this object in a customizable way.
24
+ #
25
+ # whitelist - (optional) The names of the instance variables to be inspected
26
+ #
27
+ # Examples
28
+ #
29
+ # player = Player.new(id: 1, name: 'John', health: 100, ip_address: '192.168.1.133')
30
+ #
31
+ # player.inspect # =>
32
+ #
33
+ # player.inspect(:name) # =>
34
+ #
35
+ #
36
+ # Returns the String that describes this object and its internals.
37
+ def inspect(*whitelist)
38
+ RubySelectiveInspect.perform_inspect(self, *whitelist)
39
+ end
40
+ end
41
+
42
+ # Public: Inspects the given object in a customizable way.
43
+ #
44
+ # target - The Object to be inspected.
45
+ # whitelist - The names of the instance variables to output.
46
+ #
47
+ # Examples
48
+ #
49
+ # RubySelectiveInspect.inspect(player, :id, :name)
50
+ # # =>
51
+ #
52
+ # Returns the String that describes the objects and its internals.
53
+ def self.perform_inspect(target, *whitelist)
54
+ klass = target.class
55
+ return target.inspect if !klass.include?(self) && whitelist.size == 0
56
+
57
+ whitelist = klass.get_inspectionable_vars if whitelist.size == 0
58
+ if whitelist.size == 0
59
+ whitelist = target.instance_variables.map{ |s| s[1..-1] } - klass.get_uninspectionable_vars
60
+ end
61
+
62
+ fields = whitelist.map do |var_name|
63
+ name = '@' + var_name.to_s
64
+ var_content = target.instance_variable_get(name)
65
+ "#{name}=#{var_content.inspect}"
66
+ end
67
+
68
+ string = "#<#{klass.name}:#{target.object_id} "
69
+ string + fields.join(", ") + ">"
70
+ end
71
+
72
+ # Add to classes that include this module some convenient class mathods to blacklist
73
+ # or whitelist methods by default.
74
+ def self.included(base_class)
75
+ base_class.class_eval do
76
+ alias_method :default_inspect, :inspect
77
+ end
78
+ base_class.extend ClassMethods
79
+ base_class.include InstanceMethods
80
+ end
81
+ end
@@ -0,0 +1,81 @@
1
+ require "selective_inspect/version"
2
+
3
+ module SelectiveInspect
4
+ module ClassMethods
5
+ def inspectionable_vars(*vars)
6
+ @_selective_inspector_whitelist = vars
7
+ end
8
+
9
+ def uninspectionable_vars(*vars)
10
+ @_selective_inspector_blacklist = vars
11
+ end
12
+
13
+ def get_inspectionable_vars
14
+ @_selective_inspector_whitelist ||= []
15
+ end
16
+
17
+ def get_uninspectionable_vars
18
+ @_selective_inspector_blacklist ||= []
19
+ end
20
+ end
21
+
22
+ module InstanceMethods
23
+ # Public: Inspects this object in a customizable way.
24
+ #
25
+ # whitelist - (optional) The names of the instance variables to be inspected
26
+ #
27
+ # Examples
28
+ #
29
+ # Player.new(id: 1, name: 'John', health: 100, ip_address: '192.168.1.133')
30
+ # # =>
31
+ #
32
+ # Returns the String that describes this object and its internals.
33
+ def inspect(*whitelist)
34
+ SelectiveInspect.perform_inspect(self, *whitelist)
35
+ end
36
+ end
37
+
38
+ # Public: Inspects the given object in a customizable way.
39
+ #
40
+ # target - The Object to be inspected.
41
+ # whitelist - The names of the instance variables to output.
42
+ #
43
+ # Examples
44
+ #
45
+ # SelectiveInspect.inspect(player, :id, :name)
46
+ # # =>
47
+ #
48
+ # Returns the String that describes the objects and its internals.
49
+ def self.perform_inspect(target, *whitelist)
50
+ klass = target.class
51
+ return target.default_inspect if !klass.include?(self) && whitelist.size == 0
52
+
53
+
54
+ whitelist = klass.get_inspectionable_vars.map { |name| '@' + name.to_s }
55
+ if whitelist.size == 0
56
+ whitelist = target.instance_variables - klass.get_uninspectionable_vars.map { |name| '@' + name.to_s }
57
+ end
58
+
59
+ fields = whitelist.map do |var_name|
60
+ var_content = target.instance_variable_get(var_name)
61
+ "#{var_name}=#{var_content.inspect}"
62
+ end
63
+
64
+ string = "#<#{klass.name}:0x#{target.object_id} "
65
+ string + fields.join(", ") + ">"
66
+ end
67
+
68
+ # Store a reference to the default implementation
69
+ alias :default_inspect :inspect
70
+
71
+
72
+ # Add to classes that include this module some convenient class mathods to blacklist
73
+ # or whitelist methods by default.
74
+ def self.included(base_class)
75
+ base_class.class_eval do
76
+ alias_method :default_inspect, :inspect
77
+ end
78
+ base_class.extend ClassMethods
79
+ base_class.include InstanceMethods
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ module SelectiveInspect
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'selective_inspect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "selective_inspect"
8
+ spec.version = SelectiveInspect::VERSION
9
+ spec.authors = ["Miguel Camba"]
10
+ spec.email = ["miguel.camba@gmail.com"]
11
+ spec.description = %q{Simple gem to allow to customize the output of the inspect methods}
12
+ spec.summary = %q{Customize the inspect's output of your objects white/black listing instance variables}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: selective_inspect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Miguel Camba
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Simple gem to allow to customize the output of the inspect methods
42
+ email:
43
+ - miguel.camba@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/ruby_selective_inspect.rb
55
+ - lib/selective_inspect.rb
56
+ - lib/selective_inspect/version.rb
57
+ - selective_inspect.gemspec
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Customize the inspect's output of your objects white/black listing instance
82
+ variables
83
+ test_files: []