Module: ConsoleUtils

Extended by:
ActiveSupport::Autoload
Defined in:
lib/console_utils.rb,
lib/console_utils/railtie.rb,
lib/console_utils/version.rb,
lib/console_utils/repl_state.rb,
lib/console_utils/json_output.rb,
lib/console_utils/bench_utils.rb,
lib/console_utils/other_utils.rb,
lib/console_utils/request_utils.rb,
lib/console_utils/active_record_utils.rb,
lib/console_utils/json_output/jq_formatter.rb,
lib/console_utils/json_output/base_formatter.rb,
lib/console_utils/json_output/default_formatter.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ActiveRecordUtils, BenchUtils, JSONOutput, OtherUtils, RequestUtils Classes: Railtie, ReplState

Constant Summary collapse

JSON_FORMATTERS =
[
  :default,
  :jq
]
MODULES =
[
  :ActiveRecordUtils,
  :RequestUtils,
  :BenchUtils,
  :OtherUtils
]
VERSION =
"0.7.0"

Class Method Summary collapse

Class Method Details

.auto_token_for(id) ⇒ Object


190
191
192
193
# File 'lib/console_utils.rb', line 190

def auto_token_for(id)
  user = find_user(id, scope: user_model.select([:id, user_token_column]))
  user.public_send(user_token_column)
end

.configure {|config| ... } ⇒ Object

:method: self.configure

Yields:


150
151
152
# File 'lib/console_utils.rb', line 150

def configure # :yields:
  yield(config)
end

.enabled_modulesObject

:yields:


154
155
156
157
158
159
160
161
162
# File 'lib/console_utils.rb', line 154

def enabled_modules # :yields:
  unless block_given?
    return to_enum(__method__) { ConsoleUtils::MODULES.size - disabled_modules.size }
  end

  (ConsoleUtils::MODULES - disabled_modules).each do |mod|
    yield(const_get(mod))
  end
end

.find_user(id, scope: nil) ⇒ Object

Finds user_model by user_primary_key. If the first argument is :any, gets a random user.


180
181
182
183
184
185
186
187
188
# File 'lib/console_utils.rb', line 180

def find_user(id, scope: nil)
  if id == :any
    user_scope(scope).anyone.tap do |u|
      puts "random user #{user_primary_key}: #{u.public_send(user_primary_key)}"
    end
  else
    user_scope(scope).where(user_primary_key => id).first!
  end
end

.pastelObject


205
206
207
# File 'lib/console_utils.rb', line 205

def pastel
  @pastel ||= Pastel.new
end

.pry!Object

Setup enabled modules for Pry context


196
197
198
# File 'lib/console_utils.rb', line 196

def pry!
  setup_modules_to(TOPLEVEL_BINDING.eval('self'))
end

.setup_modules_to(context = nil) ⇒ Object

Setup enabled modules by extending given context


201
202
203
# File 'lib/console_utils.rb', line 201

def setup_modules_to(context = nil)
  ReplState.setup(context)
end

.user_modelObject Also known as: user_class

Returns User's class set in the :user_class_name


165
166
167
# File 'lib/console_utils.rb', line 165

def user_model
  Object.const_get(user_model_name)
end

.user_scope(scope = nil) ⇒ Object


170
171
172
173
174
175
176
# File 'lib/console_utils.rb', line 170

def user_scope(scope = nil)
  case scope
  when nil    then user_model
  when Symbol then user_model.public_send(scope)
              else user_model.all.merge(scope)
  end
end