dumps 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1a4b720e24a2952ef166ad9407bdc99f918af21b5388511f910f925868a4595b
4
+ data.tar.gz: c072c9f8dbd1f541ed9a1f9a5505bba2835a4b8d865014738f66bd54fd6877b8
5
+ SHA512:
6
+ metadata.gz: 923e4d7998498c73e8307b69e5b4281e676821e0518d98d899dbe05fd3c9caddf75308b0796b39bd5afa0e2b043a2ab74d80603cc1238af642f3a2925428eada
7
+ data.tar.gz: cfaa9a583c2d4a833accb900315a8e5f741e93775ea82cb5475d4cd88db38a3e34aaf0cee1168682e323c65b51959da8f10e49a6ff6cbbc13c3ffe8cf246f12c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-3.1.2
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in dump.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Claus Rasmussen
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.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Dump
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dump`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dump.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/TODO ADDED
@@ -0,0 +1,10 @@
1
+
2
+ o Allow "*" or :* as an exclude/DUMP_EXCLUDE argument that wipes the inherited
3
+ list of attributes. Then remove DUMP_NEW
4
+ o Allow multi-level attribute lists: ":a, { b: [:c] }, :d"
5
+ o Have class included in a: { sub: { subsub: { ... } }:
6
+ a: (Klass)
7
+ a: (SubKlass)
8
+ a: (SubSubKlass)
9
+ o Consider switching to using StringIO everywhere. Can cost a lot of
10
+ performance but simplifies the implementation
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dumps
4
+ VERSION = "1.0.0"
5
+ end
data/lib/dumps.rb ADDED
@@ -0,0 +1,261 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "dumps/version"
4
+
5
+ require 'indented_io'
6
+ require 'constrain'
7
+ include Constrain
8
+
9
+ module Dumps
10
+ def self.dp(object) = $stderr.puts object.inspect
11
+ def self.dputs(object = "") = $stderr.puts object
12
+
13
+ # Enable/disable time zone in timestamps. Default is disabled
14
+ def self.timezone=(b)
15
+ constrain b, TrueClass, FalseClass
16
+ @@timezone = b
17
+ end
18
+
19
+ # Returns the current time
20
+ def self.timezone? = @@timezone
21
+
22
+ # The eponymic method of the Dumps module. It is actually just a method for
23
+ # the lazy developer in the tcase where dumps should be seperated by a blank
24
+ # line to enhance readability
25
+ def self.dumps(...) dump(...); puts end
26
+
27
+ # :call-seq:
28
+ # dump(object, label|ident|index|range..., new: false, exclude: nil)
29
+ #
30
+ # ::dump also supports an :index option but it is only meant to be used
31
+ # internally
32
+ #
33
+ def self.dump(object, *args, **opts)
34
+ constrain args, [String, Symbol, Integer, Range, nil]
35
+ constrain object.is_a?(Dumps) || opts.slice(:new, :exclude).empty?, true
36
+ label = args.select { |arg| arg.nil? || arg.is_a?(String) }.last
37
+ idents = args.select { |arg| arg.is_a?(Symbol) }
38
+ indexes = args.select { |arg| arg.is_a?(Integer) || arg.is_a?(Range) }
39
+ idents.empty? || indexes.empty? or raise ArgumentError, "Symbols and Integers can't be combined"
40
+
41
+ # Register if this is an anonymous top-level call to ::dump (ie. the first
42
+ # #dump method to be called from the application)
43
+ self.anonymous = label.nil? if empty?
44
+
45
+ case object
46
+ when Array; dump_array(object, label, *indexes)
47
+ when Hash; dump_hash(object, label, *idents)
48
+ when Dumps; dump_object(object, label, *idents, **opts)
49
+ else
50
+ idents.empty? or raise ArgumentError
51
+ Dumps.dump_label(label, false); dump_value(object)
52
+ end
53
+ end
54
+
55
+ def self.dump_label(label, newline = true)
56
+ constrain label, String, nil
57
+ print "#{label}:#{newline ? "\n" : " "}" if label
58
+ end
59
+
60
+ def self.dump_array(array, label, *indexes, **opts)
61
+ constrain array, Array
62
+ constrain indexes, [Integer, Range]
63
+ indexes =
64
+ if indexes.empty?
65
+ array.each_index
66
+ else
67
+ indexes.map { |e|
68
+ e.is_a?(Range) ? e.each.to_a : e
69
+ }.flatten
70
+ end
71
+ context(array, label, **opts) {
72
+ dump_label(label)
73
+ level = label ? 1 : 0
74
+ indent(level) {
75
+ indexes.each { |i| print "- "; indent(bol: false) { dump(array[i], index: i) } }
76
+ }
77
+ }
78
+ end
79
+
80
+ def self.dump_hash(hash, label, *idents, **opts)
81
+ constrain hash, Hash
82
+ constrain idents, [Symbol]
83
+ keys = idents.empty? ? hash.keys : idents
84
+ context(hash, label, **opts) {
85
+ dump_label(label)
86
+ level = label ? 1 : 0
87
+ indent(level) {
88
+ keys.each { |key| dump(hash[key], key.to_s) }
89
+ }
90
+ }
91
+ end
92
+
93
+ def self.dump_object(object, label, *idents, new: false, exclude: nil, **opts)
94
+ constrain object, Dumps
95
+ constrain label, String, nil
96
+ constrain idents, [Symbol]
97
+ constrain new, false, true
98
+ constrain exclude, nil, [Symbol]
99
+ idents = (new ? [] : object.class.dump_identifiers) \
100
+ + idents \
101
+ - (exclude.nil? ? [] : exclude)
102
+ idents.uniq!
103
+ if idents.empty?
104
+ context(object, label, **opts) { dump_label(label, false); dump_id(object) }
105
+ else
106
+ context(object, label, **opts) {
107
+ dump_label(label)
108
+ level = label ? 1 : 0
109
+ indent(level) {
110
+ idents.each { |ident|
111
+ attr_method = :"dump_attr_#{ident}"
112
+ value_method = :"dump_value_#{ident}"
113
+
114
+ if overrides?(object, attr_method)
115
+ object.send(attr_method)
116
+ elsif overrides?(object, :dump_attr)
117
+ object.send(:dump_attr, ident)
118
+ elsif overrides?(object, value_method)
119
+ value = capture { object.send(value_method) }
120
+ newline = value =~ /\n./
121
+ dump_label(ident.to_s, newline)
122
+ print value
123
+ elsif overrides?(object, :dump_value)
124
+ value = capture { object.send(:dump_value, ident) }
125
+ newline = value =~ /\n./
126
+ dump_label(ident.to_s, newline)
127
+ print value
128
+ else
129
+ dump(object.send(ident), ident.to_s)
130
+ end
131
+ }
132
+ }
133
+ }
134
+ end
135
+ end
136
+
137
+ def self.dump_value(value)
138
+ case value
139
+ when Time; puts value.strftime(timezone? ? "%F %T (%z)" : "%F %T")
140
+ when Date; puts value.strftime("%F")
141
+ else
142
+ puts value.inspect
143
+ end
144
+ end
145
+
146
+ def self.dump_id(object)
147
+ if object.class.to_s.start_with?("#")
148
+ puts "<#{object.object_id}> (Class@<#{object.class.object_id}>)"
149
+ else
150
+ puts "<#{object.object_id}> (#{object.class})"
151
+ end
152
+ end
153
+
154
+ def self.dump_reference(object) = puts reference(object)
155
+
156
+ # Object-level versions of the class methods
157
+ def dump(...) = Dumps.dump(self, ...)
158
+ def dumps(...) = Dumps.dumps(self, ...)
159
+
160
+ # def dump_attr <- doesn't exist here, can be defined in derived classes
161
+ def dump_value(ident) = Dumps.dump_value(self.send(ident))
162
+ def dump_id(...) = Dumps.dump_id(self, ...)
163
+ def dump_reference(...) = Dumps.dump_reference(self, ...)
164
+
165
+ def self.overrides?(object, method)
166
+ object.respond_to?(method) && object.method(method).owner != Dumps
167
+ end
168
+
169
+ private
170
+ def self.capture(&block)
171
+ s = StringIO.new
172
+ saved_stdout = $stdout
173
+ begin
174
+ $stdout = s
175
+ yield
176
+ ensure
177
+ $stdout = saved_stdout
178
+ end
179
+ s.string
180
+ end
181
+
182
+ # Include time zone in timestamps
183
+ @@timezone = false
184
+
185
+ # Stack of values begin processed
186
+ @@dump_stack = []
187
+
188
+ # Map from all known objects to ref. It is cleared at the beginning of the
189
+ # top-level object's #dump method. This allows test programs to check the
190
+ # when @@dump_stack becomes
191
+ # empty (this happens when the top-level #dump call terminates)
192
+ @@references = {}
193
+
194
+ @@anonymous = true
195
+
196
+ def self.anonymous? = @@anonymous
197
+ def self.anonymous=(b) @@anonymous = b end
198
+
199
+ # Stack and ref methods. These are used to avoid endless recursion when the
200
+ # dump objects contain circular references. It is somewhat expensive, though
201
+ def self.top = @@dump_stack.last
202
+ def self.push(elem) = @@dump_stack.push(elem)
203
+ def self.pop(elem) = @@dump_stack.pop
204
+ def self.empty? = @@dump_stack.empty?
205
+ def self.reset
206
+ @@references = {}
207
+ @@anonymous = true
208
+ end
209
+ def self.reference(object) = @@references[object]
210
+ def self.registered?(object) = @@references.key?(object)
211
+
212
+ # Compute reference and register the object in @@references
213
+ def self.register(object, label, index: nil)
214
+ if empty?
215
+ ref = "*#{label}"
216
+ elsif index
217
+ ref = "#{reference(top)}[#{index}]"
218
+ else
219
+ ref = "#{reference(top)}.#{label || object.object_id.to_s}"
220
+ end
221
+ @@references[object] = ref
222
+ end
223
+
224
+ # Register object and execute block with object on the top of the stack. If
225
+ # the object is already registered a reference is written and the block is
226
+ # not called
227
+ def self.context(object, label, index: nil, &block)
228
+ reset if empty?
229
+ if registered?(object)
230
+ dump_label(label, false); dump_reference(object)
231
+ else
232
+ register object, label, index: index
233
+ begin
234
+ push object
235
+ yield
236
+ ensure
237
+ pop object
238
+ end
239
+ end
240
+ end
241
+
242
+ module Dump__ClassMethods
243
+ def dump_identifiers
244
+ @dump_identifiers ||=
245
+ if const_defined?(:DUMP_NEW, false)
246
+ const_get(:DUMP_NEW)
247
+ else
248
+ (superclass.include?(Dumps) ? superclass.dump_identifiers : []) +
249
+ (const_defined?(:DUMP, false) ? const_get(:DUMP) : []) -
250
+ (const_defined?(:DUMP_EXCLUDE, false) ? const_get(:DUMP_EXCLUDE) : [])
251
+ end.map(&:to_sym)
252
+ @dump_identifiers
253
+ end
254
+ end
255
+
256
+ def self.included(klass)
257
+ super
258
+ klass.extend Dump__ClassMethods # Add class method to the including klass
259
+ end
260
+ end
261
+
data/sig/dump.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Dump
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dumps
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Claus Rasmussen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-06-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: indented_io
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: constrain
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Gem dumps
56
+ email:
57
+ - claus.l.rasmussen@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - ".ruby-version"
64
+ - Gemfile
65
+ - LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - TODO
69
+ - lib/dumps.rb
70
+ - lib/dumps/version.rb
71
+ - sig/dump.rbs
72
+ homepage: http://www.nowhere.com/
73
+ licenses: []
74
+ metadata:
75
+ homepage_uri: http://www.nowhere.com/
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 2.6.0
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.3.18
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Gem dumps
95
+ test_files: []