eitil 0.3.5 → 0.3.10
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.
- checksums.yaml +4 -4
- data/README.md +63 -5
- data/app/models/eitil/stack_trace/audit.rb +15 -0
- data/app/models/eitil/stack_trace/call.rb +17 -0
- data/app/models/eitil/stack_trace/stack.rb +28 -0
- data/config/initializers/monkeys/application_record.rb +18 -4
- data/config/initializers/monkeys/float.rb +9 -0
- data/config/initializers/monkeys/string.rb +96 -0
- data/config/initializers/wrappers/jobs/active_job_macros.rb +35 -7
- data/config/initializers/wrappers/jobs/single_method_job.rb +2 -4
- data/lib/eitil/version.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a5f03357f8feee5c40fa17fdf11acc544229eb85e822578929d18bab25f5ec4
|
4
|
+
data.tar.gz: 32ecc96ed0088b7a447fac59505d76e1f5c0e4131b38a615b2ccdf6bdbdf4b13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 706798e2c7e5e1f81cd413a5698fa030eedcc606dae5a2a6ade7fd74bc21cfa1e485cc3a7c782097aa7cac2271d879f91e8d0791627540df9acd03e8bc873273
|
7
|
+
data.tar.gz: c33996734e261cc3419ad9dfcec4dd271ecb40be39cc0dac2ae44156f750329a179a3988ba0a4d574a27fa8d411b1542f230c566544d9bb67ef4f6c60d93dc1a
|
data/README.md
CHANGED
@@ -103,6 +103,22 @@ include_concerns_of(*directories, namespace: nil)
|
|
103
103
|
|
104
104
|
|
105
105
|
|
106
|
+
## String
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
is_num?
|
110
|
+
# returns true if a string matches the Rubinius source code regex for strings and integers, false otherwise
|
111
|
+
# comes in handy since ruby plays it safe on string to number conversion ('aaa'.to_f returns 0.0, thus is valid)
|
112
|
+
# this method is also implemented for all other classes, such as Integer, Float, NilClass, TrueClass, Hash and so on...
|
113
|
+
|
114
|
+
is_nan?
|
115
|
+
# returns true if a string does NOT match the Rubinius source code regex for strings and integers, false otherwise
|
116
|
+
# comes in handy since ruby plays it safe on string to number conversion ('aaa'.to_f returns 0.0, thus is valid)
|
117
|
+
# this method is also implemented for all other classes, such as Integer, Float, NilClass, TrueClass, Hash and so on...
|
118
|
+
```
|
119
|
+
|
120
|
+
|
121
|
+
|
106
122
|
## Hash
|
107
123
|
|
108
124
|
```ruby
|
@@ -114,6 +130,17 @@ auto_dig(_hash = self, _key)
|
|
114
130
|
|
115
131
|
|
116
132
|
|
133
|
+
## Float
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
safe_to_i
|
137
|
+
# converts a float to an integer, only when no decimal values are lost.
|
138
|
+
# 8.00.safe_to_i returns 8 (Integer)
|
139
|
+
# 8.88.safe_to_i returns 8.88 (Float)
|
140
|
+
```
|
141
|
+
|
142
|
+
|
143
|
+
|
117
144
|
## ApplicationController
|
118
145
|
|
119
146
|
```ruby
|
@@ -131,6 +158,14 @@ slice_params(*args)
|
|
131
158
|
self.all_associations
|
132
159
|
# returns all associations for a given model
|
133
160
|
# call as: Environment.all_associations
|
161
|
+
|
162
|
+
self.where_like(_hash)
|
163
|
+
# runs .where with your string field made into a wildcard and case insensitive
|
164
|
+
# call as: User.where_like(name: 'jurriaan')
|
165
|
+
|
166
|
+
self.find_by_like(_hash)
|
167
|
+
# runs .find_by with your string field made into a wildcard and case insensitive
|
168
|
+
# call as: User.find_by_like(name: 'jurriaan')
|
134
169
|
```
|
135
170
|
|
136
171
|
|
@@ -227,14 +262,18 @@ The router wrapper is a single module which is automatically included into your
|
|
227
262
|
|
228
263
|
### Info
|
229
264
|
|
230
|
-
The Eitil jobs wrapper enables you to create perform_later jobs on the fly, without the need to create a new class and file. The macro new_job accepts a :method as argument and defines a new method :method_job. The newly defined :method_job, when called, performs the orginal :method in the background. The new_job macro accepts both instance methods and singleton methods, which are defined within there own method scope.
|
265
|
+
The Eitil jobs wrapper enables you to create perform_now and perform_later jobs on the fly, without the need to create a new class and file. The macro new_job accepts a :method as argument and defines a new method :method_job. The newly defined :method_job, when called, performs the orginal :method in the background. The new_job macro accepts both instance methods and singleton methods, which are defined within there own method scope. In contrast, the new_job_debugger macro defines a new :method_job_debugger method, which performs in the foreground.
|
231
266
|
|
232
267
|
### Job Macro
|
233
268
|
|
234
269
|
```ruby
|
235
|
-
new_job(_method)
|
236
|
-
# assuming a method :
|
237
|
-
# =>
|
270
|
+
new_job(_method, *args, **kwargs)
|
271
|
+
# assuming a method :hello_world, call as: new_job :hello_world
|
272
|
+
# => defines :hello_world_job
|
273
|
+
|
274
|
+
new_job_debugger(_method, *args, **kwargs)
|
275
|
+
# assuming a method :hello_world, call as: new_job_debugger :hello_world
|
276
|
+
# => defines :hello_world_job_debugger
|
238
277
|
```
|
239
278
|
|
240
279
|
- method is
|
@@ -305,7 +344,7 @@ Your models should inherit the module Eitil::DefaultScopes through inclusion in
|
|
305
344
|
|
306
345
|
|
307
346
|
|
308
|
-
# Modules
|
347
|
+
# Modules & Classes
|
309
348
|
|
310
349
|
|
311
350
|
|
@@ -332,3 +371,22 @@ Eitil::Dir.lines(directory='app')
|
|
332
371
|
```
|
333
372
|
|
334
373
|
|
374
|
+
|
375
|
+
## Eitil::StackTrace
|
376
|
+
|
377
|
+
### Info
|
378
|
+
|
379
|
+
The two classes Eitil::StackTrace::Stack and Eitil::StackTrace::Call, and the single module Eitil::StackTrace::Audit, provide the utility of easily viewing and storing the current stacktrace anywhere in your code. By initializing stack = Eitil::StackTrace::Stack.new, you can call methods as stack.report (returns the stacktrace as array), stack.show (pretty prints the stacktrace) and stack.find (accepts a block to find a call).
|
380
|
+
|
381
|
+
You can also store the stacktrace of a record's update action into its audit. In order to do so, you need 1) to include Eitil::StackTrace::Audit into your model, and 2) add a :stacktrace column to your audits through the following migration.
|
382
|
+
|
383
|
+
### Migration
|
384
|
+
|
385
|
+
```ruby
|
386
|
+
def change
|
387
|
+
add_column :audits, :stacktrace, :text, array: true, default: []
|
388
|
+
end
|
389
|
+
```
|
390
|
+
|
391
|
+
|
392
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Eitil::StackTrace::Audit
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
included do
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
after_update :add_stacktrace_to_audit
|
8
|
+
|
9
|
+
def add_stacktrace_to_audit
|
10
|
+
stacktrace = Eitil::StackTrace::Stack.new.report
|
11
|
+
self.audits.last.update(stacktrace: stacktrace)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Eitil::StackTrace
|
2
|
+
class Call
|
3
|
+
|
4
|
+
attr_reader :program, :line, :meth
|
5
|
+
|
6
|
+
CALL_RE = /(.*):(\d+):in `(.*)'/
|
7
|
+
|
8
|
+
def initialize(string)
|
9
|
+
@program, @line, @meth = CALL_RE.match(string).captures
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_s
|
13
|
+
"#{"#{meth}".ljust(50)} #{"(#{line})".ljust(6)} #{program}"
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Eitil::StackTrace
|
2
|
+
class Stack
|
3
|
+
|
4
|
+
attr_reader :stack, :backtrace
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@stack = caller[1..]
|
8
|
+
@backtrace = stack.map { |call| Eitil::StackTrace::Call.new(call) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def report
|
12
|
+
backtrace.map(&:to_s)
|
13
|
+
end
|
14
|
+
|
15
|
+
def show
|
16
|
+
ap report
|
17
|
+
end
|
18
|
+
|
19
|
+
def find(&block)
|
20
|
+
backtrace.find(&block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.parse(array_as_string)
|
24
|
+
array_as_string.sub('[', ' ').reverse.sub(']','').reverse.split(',').flatten
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -4,10 +4,24 @@ module ApplicationRecordMonkey
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
included do
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def all_associations
|
10
|
+
reflect_on_all_associations.map do |assoc|
|
11
|
+
{ assoc.name => assoc.association_class.to_s.demodulize }
|
12
|
+
end.inject &:merge
|
13
|
+
end
|
14
|
+
|
15
|
+
def where_like(_hash)
|
16
|
+
column, like = _hash.first
|
17
|
+
where("LOWER(#{column}) LIKE ?", "%#{like.downcase}%")
|
18
|
+
end
|
19
|
+
|
20
|
+
def find_by_like(_hash)
|
21
|
+
column, like = _hash.first
|
22
|
+
find_by("LOWER(#{column}) LIKE ?", "%#{like.downcase}%")
|
23
|
+
end
|
24
|
+
|
11
25
|
end
|
12
26
|
|
13
27
|
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
# The NumberRegex belows comes from the Rubinius source code and
|
4
|
+
# identifies both integers and floats correctly.
|
5
|
+
|
6
|
+
NumberRegex = /^\s*[+-]?((\d+_?)*\d+(\.(\d+_?)*\d+)?|\.(\d+_?)*\d+)(\s*|([eE][+-]?(\d+_?)*\d+)\s*)$/
|
7
|
+
|
8
|
+
def is_nan?
|
9
|
+
self !~ NumberRegex
|
10
|
+
end
|
11
|
+
|
12
|
+
def is_num?
|
13
|
+
!is_nan?
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
# Descending classes which are always numeric
|
19
|
+
|
20
|
+
class Numeric
|
21
|
+
|
22
|
+
def is_nan?
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
26
|
+
def is_num?
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
# Classes which are never numeric
|
33
|
+
|
34
|
+
class NilClass
|
35
|
+
|
36
|
+
def is_nan?
|
37
|
+
true
|
38
|
+
end
|
39
|
+
|
40
|
+
def is_num?
|
41
|
+
false
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
class TrueClass
|
48
|
+
|
49
|
+
def is_nan?
|
50
|
+
true
|
51
|
+
end
|
52
|
+
|
53
|
+
def is_num?
|
54
|
+
false
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
class FalseClass
|
61
|
+
|
62
|
+
def is_nan?
|
63
|
+
true
|
64
|
+
end
|
65
|
+
|
66
|
+
def is_num?
|
67
|
+
false
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
class Hash
|
74
|
+
|
75
|
+
def is_nan?
|
76
|
+
true
|
77
|
+
end
|
78
|
+
|
79
|
+
def is_num?
|
80
|
+
false
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
class Array
|
87
|
+
|
88
|
+
def is_nan?
|
89
|
+
true
|
90
|
+
end
|
91
|
+
|
92
|
+
def is_num?
|
93
|
+
false
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
@@ -6,19 +6,47 @@ Kernel.module_eval do
|
|
6
6
|
# being that 'id' works for objects that have a database record, while '_self'
|
7
7
|
# works for non database supported instanes, such as an Exporter instance.
|
8
8
|
|
9
|
-
def new_job(_method)
|
9
|
+
def new_job(_method, *args, **kwargs)
|
10
10
|
|
11
11
|
if instance_methods(false).include? _method
|
12
|
-
define_method "#{_method}_job" do |_self = nil|
|
13
|
-
|
12
|
+
define_method "#{_method}_job" do |_self = nil, *args, **kwargs|
|
13
|
+
|
14
|
+
Eitil::SingleMethodJob.perform_later(
|
15
|
+
*args, _class: self.class.to_s, _method: _method.to_s, id: safe_send(:id), _self: self.to_json, **kwargs
|
16
|
+
)
|
14
17
|
end
|
15
18
|
|
16
|
-
elsif singleton_methods(false).include? _method
|
17
|
-
define_singleton_method "#{_method}_job" do
|
18
|
-
|
19
|
+
elsif singleton_methods(false).include? _method
|
20
|
+
define_singleton_method "#{_method}_job" do |*args, **kwargs|
|
21
|
+
|
22
|
+
Eitil::SingleMethodJob.perform_later(
|
23
|
+
*args, _class: to_s, _method: _method.to_s, **kwargs
|
24
|
+
)
|
19
25
|
end
|
20
|
-
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# BEWARE: This is an exact copy of the above method, except for .perform_now instead of .perform_later and the method's name.
|
30
|
+
# The reason for not using helper methods is to not unnecessarily fill and potentially fuck up the Kernel environment.
|
21
31
|
|
32
|
+
def new_job_debugger(_method, *args, **kwargs)
|
33
|
+
|
34
|
+
if instance_methods(false).include? _method
|
35
|
+
define_method "#{_method}_job_debugger" do |_self = nil, *args, **kwargs|
|
36
|
+
|
37
|
+
Eitil::SingleMethodJob.perform_now(
|
38
|
+
*args, _class: self.class.to_s, _method: _method.to_s, id: safe_send(:id), _self: self.to_json, **kwargs
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
elsif singleton_methods(false).include? _method
|
43
|
+
define_singleton_method "#{_method}_job_debugger" do |*args, **kwargs|
|
44
|
+
|
45
|
+
Eitil::SingleMethodJob.perform_now(
|
46
|
+
*args, _class: to_s, _method: _method.to_s, **kwargs
|
47
|
+
)
|
48
|
+
end
|
49
|
+
end
|
22
50
|
end
|
23
51
|
|
24
52
|
end
|
@@ -8,9 +8,7 @@ module Eitil
|
|
8
8
|
# being that 'id' works for objects that have a database record, while '_self'
|
9
9
|
# works for non database supported instanes, such as an Exporter instance.
|
10
10
|
|
11
|
-
def perform(_class:, _method:, id: nil, _self: nil)
|
12
|
-
binding.pry
|
13
|
-
|
11
|
+
def perform(*args, _class:, _method:, id: nil, _self: nil, **kwargs)
|
14
12
|
object =
|
15
13
|
if id
|
16
14
|
_class.constantize.find(id)
|
@@ -20,7 +18,7 @@ module Eitil
|
|
20
18
|
_class.constantize
|
21
19
|
end
|
22
20
|
|
23
|
-
object.send _method
|
21
|
+
object.send _method, *args, **kwargs
|
24
22
|
end
|
25
23
|
end
|
26
24
|
end
|
data/lib/eitil/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eitil
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jurriaan Schrofer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -52,14 +52,19 @@ files:
|
|
52
52
|
- app/jobs/eitil/application_job.rb
|
53
53
|
- app/mailers/eitil/application_mailer.rb
|
54
54
|
- app/models/eitil/application_record.rb
|
55
|
+
- app/models/eitil/stack_trace/audit.rb
|
56
|
+
- app/models/eitil/stack_trace/call.rb
|
57
|
+
- app/models/eitil/stack_trace/stack.rb
|
55
58
|
- config/initializers/modules/dir.rb
|
56
59
|
- config/initializers/monkeys/application_controller.rb
|
57
60
|
- config/initializers/monkeys/application_record.rb
|
58
61
|
- config/initializers/monkeys/date_time.rb
|
62
|
+
- config/initializers/monkeys/float.rb
|
59
63
|
- config/initializers/monkeys/hash.rb
|
60
64
|
- config/initializers/monkeys/kernel.rb
|
61
65
|
- config/initializers/monkeys/module.rb
|
62
66
|
- config/initializers/monkeys/object.rb
|
67
|
+
- config/initializers/monkeys/string.rb
|
63
68
|
- config/initializers/wrappers/decorators/application_decorator.rb
|
64
69
|
- config/initializers/wrappers/decorators/controller_decorator.rb
|
65
70
|
- config/initializers/wrappers/jobs/active_job_macros.rb
|