console_buddy 0.1.1
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 +7 -0
- data/lib/console_buddy/augment.rb +50 -0
- data/lib/console_buddy/base.rb +9 -0
- data/lib/console_buddy/byebug/buddy_command.rb +62 -0
- data/lib/console_buddy/byebug/hello_command.rb +34 -0
- data/lib/console_buddy/csv.rb +29 -0
- data/lib/console_buddy/helpers.rb +36 -0
- data/lib/console_buddy/http_request.rb +12 -0
- data/lib/console_buddy/initializers/byebug.rb +4 -0
- data/lib/console_buddy/initializers/rails.rb +5 -0
- data/lib/console_buddy/initializers/rspec.rb +11 -0
- data/lib/console_buddy/irb.rb +23 -0
- data/lib/console_buddy/job.rb +54 -0
- data/lib/console_buddy/jobs/active_job.rb +16 -0
- data/lib/console_buddy/jobs/resque.rb +18 -0
- data/lib/console_buddy/jobs/sidekiq.rb +18 -0
- data/lib/console_buddy/method_store.rb +12 -0
- data/lib/console_buddy/one_off_job.rb +42 -0
- data/lib/console_buddy/railtie.rb +14 -0
- data/lib/console_buddy/report.rb +28 -0
- data/lib/console_buddy/version.rb +5 -0
- data/lib/console_buddy.rb +216 -0
- data/spec/console_buddy/augment_spec.rb +65 -0
- data/spec/console_buddy/csv_spec.rb +74 -0
- data/spec/console_buddy/helpers_spec.rb +36 -0
- data/spec/console_buddy/http_request_spec.rb +27 -0
- data/spec/console_buddy/irb_spec.rb +19 -0
- data/spec/console_buddy/job_spec.rb +100 -0
- data/spec/console_buddy/method_store_spec.rb +33 -0
- data/spec/console_buddy/report_spec.rb +29 -0
- data/spec/spec_helper.rb +29 -0
- metadata +233 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1974a84f07b540eb25e77ad91b3e5271dc2a8db54b182f98a6ff7c7edcef6070
|
4
|
+
data.tar.gz: 9aa0bba710385fb7e39dc04911591f99506b187698f3466777ec5cb48646b4ba
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 87fd331b06f92bc97f36d02d5bbd0eb18ff784bd627be1d8ccedaad0c3669cbbf34713f488a8d986931bc4405babf9c72b4d41066d0eed8dc8769f8be9a4172d
|
7
|
+
data.tar.gz: e15e875ec308c774041d3dc670c5d4a793e3dc9fa9ac7b2ecf8f1838d26777b3884928499af27dcd692363fa44fbc8c645e3fbd04828ac5874be09796d390d2b
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
|
4
|
+
# Example:
|
5
|
+
#
|
6
|
+
# Here we are defining a method called `sps` on the `User` model.
|
7
|
+
# This method will return the saved posts for a given user record.
|
8
|
+
#
|
9
|
+
#
|
10
|
+
# ConsoleBuddy::Augment.define do
|
11
|
+
# method User, :sps do |args|
|
12
|
+
# saved_posts
|
13
|
+
# end
|
14
|
+
# end
|
15
|
+
|
16
|
+
module ConsoleBuddy
|
17
|
+
class Augment
|
18
|
+
class << self
|
19
|
+
def define(&block)
|
20
|
+
if block_given?
|
21
|
+
instance = ::ConsoleBuddy::Augment::DSL.new
|
22
|
+
instance.instance_eval(&block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class DSL
|
28
|
+
class InvalidTypeError < StandardError; end
|
29
|
+
|
30
|
+
def method(klass, method_name, type: :instance, &block)
|
31
|
+
raise InvalidTypeError, "Invalid method type. Must be either :instance or :class" unless %i[instance class].include?(type)
|
32
|
+
|
33
|
+
store(klass) << { method_name: method_name, block: block, method_type: type }
|
34
|
+
end
|
35
|
+
|
36
|
+
def method_alias(klass, method_name, new_method_name, type: :instance)
|
37
|
+
raise InvalidTypeError, "Invalid method type. Must be either :instance or :class" unless %i[instance class].include?(type)
|
38
|
+
|
39
|
+
block = ::Proc.new { |*args| send(method_name, *args) }
|
40
|
+
store(klass) << { method_name: new_method_name, block: block, method_type: type }
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def store(klass)
|
46
|
+
ConsoleBuddy.store.augment_helper_methods[klass.to_s]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
require 'byebug/command'
|
3
|
+
|
4
|
+
require_relative '../irb'
|
5
|
+
|
6
|
+
module ConsoleBuddy
|
7
|
+
module Byebug
|
8
|
+
class BuddyCommand < ::Byebug::Command
|
9
|
+
include ::ConsoleBuddy::IRB
|
10
|
+
#
|
11
|
+
# Return a regex to match the "buddy" command input.
|
12
|
+
#
|
13
|
+
# Example: buddy.ping "http://example.com"
|
14
|
+
def self.regexp
|
15
|
+
/^\s*buddy(?:\.(\w+))?\s*(.*)$/
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.buddy
|
19
|
+
::ConsoleBuddy::Base.new
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
# Execute the command.
|
24
|
+
#
|
25
|
+
def execute
|
26
|
+
method_name = @match[1]
|
27
|
+
args = @match[2]
|
28
|
+
|
29
|
+
if method_name
|
30
|
+
result = self.class.buddy.send(method_name, *parse_args(args))
|
31
|
+
puts result
|
32
|
+
else
|
33
|
+
puts "Buddy command executed"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
#
|
38
|
+
# Short description shown in the 'help' output.
|
39
|
+
#
|
40
|
+
def short_description
|
41
|
+
"Executes buddy commands"
|
42
|
+
end
|
43
|
+
|
44
|
+
#
|
45
|
+
# Long description shown in the 'help <command>' output.
|
46
|
+
#
|
47
|
+
def long_description
|
48
|
+
"Executes buddy commands with optional method calls, e.g., buddy.ping"
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def parse_args(args)
|
55
|
+
eval(args)
|
56
|
+
rescue SyntaxError, NameError => e
|
57
|
+
puts "Error parsing arguments: #{e.message}"
|
58
|
+
[]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'byebug'
|
2
|
+
require 'byebug/command'
|
3
|
+
|
4
|
+
module ConsoleBuddy
|
5
|
+
module Byebug
|
6
|
+
#
|
7
|
+
# A demo "hey_buddy" command for Byebug.
|
8
|
+
#
|
9
|
+
class HelloCommand < ::Byebug::Command
|
10
|
+
#
|
11
|
+
# Return a regex to match the "hey_buddy" command input.
|
12
|
+
#
|
13
|
+
def self.regexp
|
14
|
+
/^\s*hey_buddy\s*$/
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Short description shown in the 'help' output.
|
19
|
+
#
|
20
|
+
def self.description
|
21
|
+
<<-EOD
|
22
|
+
hello: Prints "Hi, I'm buddy!"
|
23
|
+
EOD
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
# Called when the user types `hey_buddy` in Byebug.
|
28
|
+
#
|
29
|
+
def execute
|
30
|
+
puts "Hi, I'm buddy!"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
|
5
|
+
module ConsoleBuddy
|
6
|
+
module CSV
|
7
|
+
def generate_csv(headers, rows, filename: DateTime.now.to_s, dir: 'tmp')
|
8
|
+
Dir.mkdir(dir) unless Dir.exist?(dir)
|
9
|
+
|
10
|
+
file_path = ::File.join(dir, "#{filename}.csv")
|
11
|
+
::CSV.open(file_path, 'w') do |csv|
|
12
|
+
csv << headers
|
13
|
+
rows.each do |row|
|
14
|
+
csv << row
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
puts "CSV created. CSV can be found at: #{file_path}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def read_csv(file_path, skip_headers: true)
|
22
|
+
data = []
|
23
|
+
::CSV.foreach(file_path, headers: skip_headers) do |row|
|
24
|
+
data << row
|
25
|
+
end
|
26
|
+
data
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Example usage
|
4
|
+
#
|
5
|
+
# ConsoleBuddy::Helpers.define do
|
6
|
+
# console_method :call_api do |status, message|
|
7
|
+
# puts "making api call"
|
8
|
+
|
9
|
+
# puts "RESPONSE: { #{status}, #{message} }"
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
|
13
|
+
# $ call_api(200, "ok")
|
14
|
+
#
|
15
|
+
# call_api can be used anywhere in your rails console
|
16
|
+
#
|
17
|
+
module ConsoleBuddy
|
18
|
+
class Helpers
|
19
|
+
class << self
|
20
|
+
def define(&block)
|
21
|
+
if block_given?
|
22
|
+
instance = ::ConsoleBuddy::Helpers::DSL.new
|
23
|
+
instance.instance_eval(&block)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class DSL
|
29
|
+
def console_method(method_name, &block)
|
30
|
+
::ConsoleBuddy.store.console_method[method_name] = block
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# TODO: Add support for other test frameworks
|
2
|
+
#
|
3
|
+
# This runs the ConsoleBuddy.start! method before the test suite is run.
|
4
|
+
if defined? RSpec
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.before(:suite) do
|
7
|
+
ConsoleBuddy.start! if ConsoleBuddy.use_in_tests
|
8
|
+
ConsoleBuddy.load_byebug! if ConsoleBuddy.use_in_debuggers
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "csv"
|
4
|
+
require_relative "http_request"
|
5
|
+
require_relative "one_off_job"
|
6
|
+
require_relative "report"
|
7
|
+
|
8
|
+
module ConsoleBuddy
|
9
|
+
module IRB
|
10
|
+
include ::ConsoleBuddy::CSV
|
11
|
+
include ::ConsoleBuddy::HttpRequest
|
12
|
+
include ::ConsoleBuddy::Report
|
13
|
+
|
14
|
+
# This method is used to define a method on the `Object` class.
|
15
|
+
# This method will return a new instance of the `ConsoleBuddy::Base` class.
|
16
|
+
# Example Usage:
|
17
|
+
# buddy.table_for(User.all)
|
18
|
+
def buddy
|
19
|
+
::ConsoleBuddy::Base.new
|
20
|
+
end
|
21
|
+
alias console_buddy buddy
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'one_off_job'
|
4
|
+
|
5
|
+
# This class is used to integrate the ConsoleBuddy::OneOffJob with Resque, Sidekiq, and ActiveJob.
|
6
|
+
# You can define your job and then run it using the `perform` method of this class.
|
7
|
+
# That will then delegate this performance to the correct "job" class based on the service type.
|
8
|
+
#
|
9
|
+
# Example Usage: ConsoleBuddy::Job.perform("foo", "bar")
|
10
|
+
module ConsoleBuddy
|
11
|
+
class Job
|
12
|
+
def perform(*args)
|
13
|
+
job_type = ::ConsoleBuddy::OneOffJob.service_type
|
14
|
+
|
15
|
+
case job_type
|
16
|
+
when :resque
|
17
|
+
require_relative "jobs/resque"
|
18
|
+
::ConsoleBuddy::Jobs::Resque.perform(*args)
|
19
|
+
when :sidekiq
|
20
|
+
require_relative "jobs/sidekiq"
|
21
|
+
::ConsoleBuddy::Jobs::Sidekiq.new.perform(*args)
|
22
|
+
when :active_job
|
23
|
+
require_relative "jobs/active_job"
|
24
|
+
::ConsoleBuddy::Jobs::ActiveJob.perform(*args)
|
25
|
+
else
|
26
|
+
::ConsoleBuddy::OneOffJob.perform(*args)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class << self
|
31
|
+
def perform_async(*args)
|
32
|
+
job_type = ::ConsoleBuddy::OneOffJob.service_type
|
33
|
+
|
34
|
+
case job_type
|
35
|
+
when :resque
|
36
|
+
require_relative "jobs/resque"
|
37
|
+
::ConsoleBuddy::Jobs::Resque.perform_later(*args)
|
38
|
+
when :sidekiq
|
39
|
+
require_relative "jobs/sidekiq"
|
40
|
+
::ConsoleBuddy::Jobs::Sidekiq.perform_async(*args)
|
41
|
+
when :active_job
|
42
|
+
require_relative "jobs/active_job"
|
43
|
+
::ConsoleBuddy::Jobs::ActiveJob.perform_later(*args)
|
44
|
+
else
|
45
|
+
::ConsoleBuddy::OneOffJob.perform(*args)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def perform_later(*args)
|
50
|
+
self.perform_async(*args)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../one_off_job"
|
4
|
+
|
5
|
+
# Example Usage: ConsoleBuddy::Jobs::ActiveJob.perform_later("foo", "bar")
|
6
|
+
#
|
7
|
+
# This class is used to integrate the ConsoleBuddy::OneOffJob with ActiveJob.
|
8
|
+
module ConsoleBuddy
|
9
|
+
module Jobs
|
10
|
+
class ActiveJob < ::ActiveJob::Base
|
11
|
+
def perform(*args)
|
12
|
+
::ConsoleBuddy::OneOffJob.perform(*args)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../one_off_job"
|
4
|
+
|
5
|
+
# Example Usage: ConsoleBuddy::Jobs::Resque.perform_later("foo", "bar")
|
6
|
+
#
|
7
|
+
# This class is used to integrate the ConsoleBuddy::OneOffJob with Resque.
|
8
|
+
module ConsoleBuddy
|
9
|
+
module Jobs
|
10
|
+
class Resque
|
11
|
+
@queue = :console_buddy
|
12
|
+
|
13
|
+
def perform(*args)
|
14
|
+
::ConsoleBuddy::OneOffJob.perform(*args)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../one_off_job"
|
4
|
+
|
5
|
+
# Example Usage: ConsoleBuddy::Jobs::Sidekiq.perform_later("foo", "bar")
|
6
|
+
#
|
7
|
+
# This class is used to integrate the ConsoleBuddy::OneOffJob with Sidekiq.
|
8
|
+
module ConsoleBuddy
|
9
|
+
module Jobs
|
10
|
+
class Sidekiq
|
11
|
+
include ::Sidekiq::Job
|
12
|
+
|
13
|
+
def perform(*args)
|
14
|
+
::ConsoleBuddy::OneOffJob.perform(*args)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ConsoleBuddy
|
4
|
+
class MethodStore
|
5
|
+
attr_accessor :augment_helper_methods, :console_method
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@augment_helper_methods = ::Hash.new { |hash, key| hash[key] = [] }
|
9
|
+
@console_method = ::Hash.new
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
#
|
3
|
+
# Example:
|
4
|
+
# ConsoleBuddy::OneOffJob.define { puts "one offer man...another one"; puts User.last.attributes; }
|
5
|
+
# Usage: ConsoleBuddy::OneOffJob.perform
|
6
|
+
#
|
7
|
+
# Example 2:
|
8
|
+
#
|
9
|
+
# ConsoleBuddy::OneOffJob.define do |user_id|
|
10
|
+
# puts User.find(user_id).attributes
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
module ConsoleBuddy
|
14
|
+
class InvalidJobServiceType < StandardError; end
|
15
|
+
|
16
|
+
class OneOffJob
|
17
|
+
SERVICE_TYPES = [:sidekiq, :resque, :active_job, :inline].freeze
|
18
|
+
class << self
|
19
|
+
def service_type
|
20
|
+
return @service_type if defined?(@service_type)
|
21
|
+
return ConsoleBuddy.one_off_job_service_type if ConsoleBuddy.one_off_job_service_type.present?
|
22
|
+
|
23
|
+
:inline
|
24
|
+
end
|
25
|
+
|
26
|
+
def service_type=(type)
|
27
|
+
raise InvalidJobServiceType, "Valid service types are #{SERVICE_TYPES.join(', ')}" unless SERVICE_TYPES.include?(type)
|
28
|
+
@service_type = type
|
29
|
+
end
|
30
|
+
|
31
|
+
def define(&block)
|
32
|
+
@block = block
|
33
|
+
end
|
34
|
+
|
35
|
+
def perform(*args)
|
36
|
+
return unless @block
|
37
|
+
|
38
|
+
@block.call(*args)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/railtie'
|
4
|
+
|
5
|
+
# Load console buddy when the Rails console is started
|
6
|
+
module ConsoleBuddy
|
7
|
+
class Railtie < ::Rails::Railtie
|
8
|
+
console do
|
9
|
+
::ConsoleBuddy.start!
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
require "console_buddy/railtie" if defined?(::Rails::Railtie)
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'table_print'
|
4
|
+
require 'terminal-table'
|
5
|
+
|
6
|
+
module ConsoleBuddy
|
7
|
+
module Report
|
8
|
+
# Example Usage:
|
9
|
+
# ConsoleBuddy::Report.table_print(User.all, "username")
|
10
|
+
#
|
11
|
+
# Example 2:
|
12
|
+
# table_print User.all, "username"
|
13
|
+
def table_print(data, options = {})
|
14
|
+
puts ::TablePrint::Printer.table_print(data, options)
|
15
|
+
end
|
16
|
+
alias print_data table_print
|
17
|
+
|
18
|
+
# Example Usage:
|
19
|
+
# ConsoleBuddy::Report.table_for([["foo", "bar"], ["baz", "qux"]], ["col1", "col2"])
|
20
|
+
#
|
21
|
+
# Example 2:
|
22
|
+
# table_for([["foo", "bar"], ["baz", "qux"]], ["col1", "col2"])
|
23
|
+
def table_for(rows, headers = [])
|
24
|
+
table = ::Terminal::Table.new(headings: headers, rows: rows)
|
25
|
+
puts table
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|