pretty_please 0.1.0 → 0.2.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: 35a3e562b4bd340a6628de8a9c3533f2c28704b79df394864eadc91420a6dc01
4
- data.tar.gz: b2ec7be1ae8b8f455a3571b2f5c98d2b83f876155013e2b72810a7bfc6a34e03
3
+ metadata.gz: 6fb770ff02334614334043c30be8c7b3f52ec56a352a8133c4975c3584ce2ddd
4
+ data.tar.gz: 8bb89b47187675c716ef330b51b499830d03b06850bdb6d06be0e3585c1fd94f
5
5
  SHA512:
6
- metadata.gz: 24c75e11a326b3e29c5025cdc661f446c706785f7b6921aac96d9f4dbc9ce63ba75c1264e69ee0d76434de5f4f93d192ae7956e8f9cab2f296c8e603e5368dd7
7
- data.tar.gz: fd7a14399e83fe7d9eb05807b3126174bc54c5af1eed65ac3dd655f5e193cb85403defecf123a8c804ec122df1c02982bb3ddf67444927929ed376507f804108
6
+ metadata.gz: eeb47b2b68683c32851d0fbc288794548d86d835fd663dfab01483b4005ac8f4b296c4098cba9cca0173acbba157724ea5e2833d4e17873848f343ec0a77a7b2
7
+ data.tar.gz: 2cdd90b746ef1a4428cc943b537ecb98f0097e4b26ed0c83fba6e9a26fd8bcfdd4d9ecdaa80dcdc994921b945052f854c65dce49f8a805bb28601fada82d89f1
data/README.md CHANGED
@@ -25,7 +25,7 @@ gem install pretty_please
25
25
 
26
26
  `PrettyPlease.print(object)` prints the object with ANSI highlighting.
27
27
 
28
- The `PrettyPlease.inspect` method provides a structured and human-readable representation of Ruby objects by outputting valid Ruby code.
28
+ The `PrettyPlease.prettify` method provides a structured and human-readable representation of Ruby objects by outputting valid Ruby code.
29
29
 
30
30
  It handles a variety of data types including Hashes, Arrays, Sets, Modules, and user-defined objects.
31
31
 
@@ -34,7 +34,7 @@ It handles a variety of data types including Hashes, Arrays, Sets, Modules, and
34
34
  ```ruby
35
35
  require "pretty_please"
36
36
 
37
- puts PrettyPlease.inspect({ a: 1, b: [2, 3], c: { d: 4 } })
37
+ puts PrettyPlease.prettify({ a: 1, b: [2, 3], c: { d: 4 } })
38
38
  ```
39
39
 
40
40
  **Output:**
@@ -49,11 +49,11 @@ puts PrettyPlease.inspect({ a: 1, b: [2, 3], c: { d: 4 } })
49
49
 
50
50
  **Options:**
51
51
 
52
- - `object`: (required) – The object to inspect and format.
52
+ - `object`: (required) – The object to prettify and format.
53
53
  - `tab_width`: 2 (Integer) – The number of spaces (or tabs) per indentation level.
54
54
  - `max_width`: 60 (Integer) – The maximum width before elements are split into multiple lines.
55
55
  - `max_depth`: 5 (Integer) – The maximum depth of nested structures before truncation.
56
- - `max_instance_variables`: 10 (Integer) – The maximum number of instance variables to display for objects.
56
+ - `max_items`: 10 (Integer) – The maximum number of instance variables to display for objects.
57
57
 
58
58
  ### Development
59
59
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class PrettyPlease::Inspect
3
+ class PrettyPlease::Prettifier
4
4
  Null = Object.new
5
5
 
6
6
  def self.call(object, ...)
@@ -14,6 +14,7 @@ class PrettyPlease::Inspect
14
14
  @max_items = max_items
15
15
  @max_depth = max_depth
16
16
  @indent_bytes = " " * @tab_width
17
+ @running_depth = 0
17
18
 
18
19
  @buffer = +""
19
20
  @lines = 0 # note, this is not reset within a capture
@@ -22,11 +23,11 @@ class PrettyPlease::Inspect
22
23
  end
23
24
 
24
25
  def call(object)
25
- inspect(object)
26
+ prettify(object)
26
27
  @buffer
27
28
  end
28
29
 
29
- def inspect(object)
30
+ def prettify(object)
30
31
  original_object = @original_object
31
32
 
32
33
  if original_object == Null
@@ -40,21 +41,21 @@ class PrettyPlease::Inspect
40
41
 
41
42
  @stack.push(object)
42
43
 
43
- if object.respond_to?(:pretty_please_inspect)
44
- object.pretty_please_inspect(self)
44
+ if object.respond_to?(:pretty_please)
45
+ object.pretty_please(self)
45
46
  else
46
47
  case object
47
48
  when Symbol, String, Integer, Float, Regexp, Range, Rational, Complex, TrueClass, FalseClass, NilClass
48
49
  push object.inspect
49
50
  when Module
50
51
  push object.name
51
- when Pathname, File
52
+ when File, defined?(Pathname) && Pathname
52
53
  push %(#{object.class.name}("#{object.to_path}"))
53
54
  when MatchData, (defined?(Date) && Date), (defined?(DateTime) && DateTime), (defined?(Time) && Time), (defined?(URI) && URI)
54
55
  push %(#{object.class.name}("#{object}"))
55
56
  when Array
56
57
  push "["
57
- map(object) { |it| capture { inspect(it) } }
58
+ map(object) { |it| capture { prettify(it) } }
58
59
  push "]"
59
60
  when Exception
60
61
  push %(#{object.class.name}("#{object.message}"))
@@ -63,10 +64,10 @@ class PrettyPlease::Inspect
63
64
  map(object, around_inline: " ") do |key, value|
64
65
  case key
65
66
  when Symbol
66
- "#{key.name}: #{capture { inspect(value) }}"
67
+ "#{key.name}: #{capture { prettify(value) }}"
67
68
  else
68
- key = capture { inspect(key) }
69
- value = capture { inspect(value) }
69
+ key = capture { prettify(key) }
70
+ value = capture { prettify(value) }
70
71
  "#{key} => #{value}"
71
72
  end
72
73
  end
@@ -74,16 +75,27 @@ class PrettyPlease::Inspect
74
75
  when Struct, defined?(Data) && Data
75
76
  push "#{object.class.name}("
76
77
  items = object.members.map { |key| [key, object.__send__(key)] }
77
- map(items) { |key, value| "#{key}: #{capture { inspect(value) }}" }
78
+ map(items) { |key, value| "#{key}: #{capture { prettify(value) }}" }
78
79
  push ")"
79
80
  when defined?(Set) && Set
80
81
  push "Set["
81
- map(object.to_a.sort) { |it| capture { inspect(it) } }
82
+ map(object) { |it| capture { prettify(it) } }
82
83
  push "]"
84
+ when defined?(ActiveRecord::Base) && ActiveRecord::Base
85
+ max_items_before = @max_items
86
+ @max_items = object.attributes.length
87
+
88
+ push "#{object.class.name}("
89
+ map(object.attributes) do |(key, value)|
90
+ "#{key}: #{capture { prettify(value) }}"
91
+ end
92
+ push ")"
93
+
94
+ @max_items = max_items_before
83
95
  else
84
96
  push "#{object.class.name}("
85
97
  map(object.instance_variables) do |name|
86
- "#{name} = #{capture { inspect(object.instance_variable_get(name)) }}"
98
+ "#{name} = #{capture { prettify(object.instance_variable_get(name)) }}"
87
99
  end
88
100
  push ")"
89
101
  end
@@ -98,6 +110,8 @@ class PrettyPlease::Inspect
98
110
  return
99
111
  end
100
112
 
113
+ @running_depth += 1
114
+
101
115
  return unless object.any?
102
116
 
103
117
  length = 0
@@ -105,6 +119,7 @@ class PrettyPlease::Inspect
105
119
 
106
120
  original_lines = @lines
107
121
  exceeds_max_items = object.length > @max_items
122
+ current_running_depth = @running_depth
108
123
 
109
124
  items = indent do
110
125
  object.take(@max_items).map do |item|
@@ -114,7 +129,7 @@ class PrettyPlease::Inspect
114
129
  end
115
130
  end
116
131
 
117
- if (@lines > original_lines) || (length > @max_width)
132
+ if (@lines > original_lines) || (length > @max_width) || (@running_depth > current_running_depth)
118
133
  indent do
119
134
  items.each do |item|
120
135
  newline
@@ -129,7 +144,7 @@ class PrettyPlease::Inspect
129
144
  end
130
145
  newline
131
146
 
132
- else
147
+ else # inline
133
148
  push around_inline
134
149
  push items.join(", ")
135
150
  push ", ..." if exceeds_max_items
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PrettyPlease
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/pretty_please.rb CHANGED
@@ -4,13 +4,18 @@ require "pretty_please/version"
4
4
  require "dispersion"
5
5
 
6
6
  module PrettyPlease
7
- autoload :Inspect, "pretty_please/inspect"
7
+ autoload :Prettifier, "pretty_please/prettifier"
8
8
 
9
9
  def self.print(object)
10
10
  puts Dispersion.ansi(inspect(object))
11
11
  end
12
12
 
13
13
  def self.inspect(...)
14
- Inspect::(...)
14
+ warn "PrettyPlease.inspect is deprecated. Use PrettyPlease.prettify instead."
15
+ prettify(...)
16
+ end
17
+
18
+ def self.prettify(...)
19
+ Prettifier::(...)
15
20
  end
16
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_please
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Drapper
8
8
  - Marco Roth
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-02-04 00:00:00.000000000 Z
11
+ date: 2025-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dispersion
@@ -35,7 +35,7 @@ files:
35
35
  - LICENSE.txt
36
36
  - README.md
37
37
  - lib/pretty_please.rb
38
- - lib/pretty_please/inspect.rb
38
+ - lib/pretty_please/prettifier.rb
39
39
  - lib/pretty_please/version.rb
40
40
  homepage: https://github.com/joeldrapper/pretty_please
41
41
  licenses: