object_shadow 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ebcf54f524f3ab83fac7d05282f3510682a8e75b1c6f4744616aef84f2291f0
4
- data.tar.gz: 6db42ef5f1cfff3b3927662cfddc774b779196521c023d3b224bf3b37a41e258
3
+ metadata.gz: 503cc7d716417cc40c00e2b193b89ffb8f3946f140d3c04699b6ca36ed31c6d1
4
+ data.tar.gz: 3fc4e0ed0b2f42afd5d0ec4f670ba361214fe992bc6a78d7fbe47f04318361be
5
5
  SHA512:
6
- metadata.gz: 7350dd92d83503c51dc1d4a3172d8eda8713f2dcee47d41fdc324c289ab809ff93410cb49a18f5297218536f5bb0dfbb38ca8a1b42d603049ce724a9afdac7dc
7
- data.tar.gz: 516a2e61c66368c4cfef098878e1493eccb536e5a26788b861013875be14bccdaedf415674d08c8adef07f5906d5810c378da830556f723c45bfeeada5659a16
6
+ metadata.gz: f27bf9d3d9df00608b20b6c4500720c2db309f89e01df17a85ea0297b63a3ced0715c3d8608975e430db58015c8a850cdfdac1b63344bd914e0e6587eb12d835
7
+ data.tar.gz: cdb3f92aeaf8b2a3718ee211037b87037ded6f020359af6d3418437b88648f5f61d579742f241d4a66c837dc1926cd155b8888b91c84fa7865364ffe7e67782a
@@ -1,5 +1,9 @@
1
1
  ## CHANGELOG
2
2
 
3
+ ### 1.1.0
4
+
5
+ * Add DeepInspect mode
6
+
3
7
  ### 1.0.0
4
8
 
5
9
  * Initial release
data/Gemfile CHANGED
@@ -4,3 +4,6 @@ gemspec
4
4
 
5
5
  gem "minitest"
6
6
  gem "rake"
7
+ gem "irb"
8
+ gem "wirb"
9
+ gem "paint"
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
- # Object#shadow [![[version]](https://badge.fury.io/rb/object.svg)](https://badge.fury.io/rb/object) [![[travis]](https://travis-ci.org/janlelis/object.svg)](https://travis-ci.org/janlelis/object)
1
+ # Object#shadow [![[version]](https://badge.fury.io/rb/object_shadow.svg)](https://badge.fury.io/rb/object_shadow) [![[travis]](https://travis-ci.org/janlelis/object_shadow.svg)](https://travis-ci.org/janlelis/object_shadow)
2
2
 
3
- Have you ever been [confused by some of Ruby's meta-programming methods](https://idiosyncratic-ruby.com/25-meta-methodology.html). If your answer is *Yes*, you have come to the right place:
3
+ Have you ever been [confused by some of Ruby's meta-programming methods?](https://idiosyncratic-ruby.com/25-meta-methodology.html)
4
+
5
+ If your answer is *Yes*, you have come to the right place:
4
6
 
5
7
  ![Object and Shadow](/object_shadow.png)
6
8
 
@@ -55,8 +57,8 @@ end
55
57
  ### # Get an Overview
56
58
 
57
59
  ```ruby
58
- object.shadow
59
- # ObjectShadow of Object #47023274596520
60
+ require "object_shadow"
61
+ object.shadow # ObjectShadow of Object #47023274596520
60
62
 
61
63
  ## Lookup Chain
62
64
 
@@ -188,6 +190,18 @@ Some of Ruby's core classes use `@`-less instance variables, such as [Structs](h
188
190
 
189
191
  Only some aspects of Ruby meta-programming are covered. However, **shadow** aims to cover all kinds of meta-programming. Maybe you have an idea about how to integrate `eval`, `method_missing`, and friends?
190
192
 
193
+ ### Does this Gem Include a Secret Mode which Activates an Improved Shadow Inspect?
194
+
195
+ Yes, run the following command.
196
+
197
+ ```ruby
198
+ ObjectShadow.include(ObjectShadow::DeepInspect)
199
+ 42.shadow
200
+ ```
201
+
202
+ Requires the following gems: **paint**, **wirb**, **io-console**
203
+
204
+
191
205
  ## J-_-L
192
206
 
193
207
  Copyright (C) 2019 Jan Lelis <https://janlelis.com>. Released under the MIT license.
@@ -8,6 +8,7 @@ require_relative "object_shadow/wrap"
8
8
  require_relative "object_shadow/instance_variables"
9
9
  require_relative "object_shadow/method_introspection"
10
10
  require_relative "object_shadow/info_inspect"
11
+ require_relative "object_shadow/deep_inspect"
11
12
 
12
13
  class ObjectShadow
13
14
  include Wrap
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require "paint"
5
+ require "wirb"
6
+ require "io/console"
7
+ rescue LoadError
8
+ warn "Not loading ObjectShadow::DeepInspect (required gems missing/not bundled)"
9
+ end
10
+
11
+ class ObjectShadow
12
+ # Improve shadow#inspect
13
+ # Optional, because it requires the following gems to be installed:
14
+ # - paint
15
+ # - wirb
16
+ # - io-console
17
+ module DeepInspect
18
+ def inspect
19
+ public_methods = methods(scope: :public)
20
+ protected_methods = methods(scope: :protected)
21
+ private_methods = methods(scope: :private)
22
+
23
+ inherit_till = object.instance_of?(Object) ? :all : Object
24
+ lookup_chain = (method_lookup_chain(inherit: inherit_till) + ["…"]).inspect
25
+
26
+ res = \
27
+ Paint["# ObjectShadow of Object #%{object_id}\n\n", :underline] +
28
+ Paint["## Lookup Chain\n\n", :bold] +
29
+ "%{method_lookup_chain}\n\n"
30
+
31
+ unless variables.empty?
32
+ res += Paint["## %{variables_count} Instance Variable%{variables_plural}\n\n%{variables}\n\n", :bold]
33
+ end
34
+
35
+ unless public_methods.empty?
36
+ res += Paint["## %{public_methods_count} Public Method%{public_methods_plural} (Non-Class/Object)\n\n%{public_methods}\n\n", :bold]
37
+ end
38
+
39
+ unless protected_methods.empty?
40
+ res += Paint["## %{protected_methods_count} Protected Method%{protected_methods_plural} (Non-Class/Object)\n\n%{protected_methods}\n\n", :bold]
41
+ end
42
+
43
+ unless private_methods.empty?
44
+ res += Paint["## %{private_methods_count} Private Method%{private_methods_plural} (Non-Class/Object)\n\n%{private_methods}\n\n", :bold]
45
+ end
46
+
47
+ res += \
48
+ Paint["## Object Inspect\n\n", :bold] +
49
+ "%{object_inspect}\n\n"
50
+
51
+ res % {
52
+ object_id: object.object_id,
53
+ method_lookup_chain: ::Wirb.colorize_result(DeepInspect.column100(lookup_chain)),
54
+ variables_count: variables.size,
55
+ variables_plural: variables.size == 1 ? "" : "s",
56
+ variables: ::Wirb.colorize_result(DeepInspect.column100(variables.inspect)),
57
+ public_methods_count: public_methods.size,
58
+ public_methods_plural: public_methods.size == 1 ? "" : "s",
59
+ public_methods: ::Wirb.colorize_result(DeepInspect.column100(public_methods.inspect)),
60
+ protected_methods_count: protected_methods.size,
61
+ protected_methods_plural: protected_methods.size == 1 ? "" : "s",
62
+ protected_methods: ::Wirb.colorize_result(DeepInspect.column100(protected_methods.inspect)),
63
+ private_methods_count: private_methods.size,
64
+ private_methods_plural: private_methods.size == 1 ? "" : "s",
65
+ private_methods: ::Wirb.colorize_result(DeepInspect.column100(private_methods.inspect)),
66
+ object_inspect: ::Wirb.colorize_result(DeepInspect.column100(object.inspect))
67
+ }
68
+ end
69
+
70
+ class << self
71
+ def column100(input)
72
+ terminal_size = STDOUT.winsize[1] || 100
73
+ words = input.split(" ")
74
+ lines = [""]
75
+ words.each{ |word|
76
+ if lines[-1].size + word.size < terminal_size - 9 # x - 1 word space - 4 indent spaces x2
77
+ lines[-1] = lines[-1] + word + " "
78
+ else
79
+ lines << word + " "
80
+ end
81
+ }
82
+
83
+ lines.map{ |line|
84
+ " #{line}"
85
+ }.join("\n")
86
+ end
87
+ end
88
+ end
89
+ end
@@ -3,5 +3,5 @@
3
3
  require_relative "basic_object"
4
4
 
5
5
  class ObjectShadow
6
- VERSION = "1.0.0"
6
+ VERSION = "1.1.0"
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_shadow
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Lelis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-08 00:00:00.000000000 Z
11
+ date: 2019-04-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: provides a simple convenient API for accessing an object's state.
14
14
  email:
@@ -28,6 +28,7 @@ files:
28
28
  - Rakefile
29
29
  - lib/object_shadow.rb
30
30
  - lib/object_shadow/basic_object.rb
31
+ - lib/object_shadow/deep_inspect.rb
31
32
  - lib/object_shadow/info_inspect.rb
32
33
  - lib/object_shadow/instance_variables.rb
33
34
  - lib/object_shadow/method_introspection.rb