class Mongoid::Shell::Commands::Base

Attributes

args[RW]
session[RW]

Public Class Methods

arg(name, options = {}) click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 24
def arg(name, options = {})
  attr_accessor name unless instance_methods.include?(name)
  args[name] = { property: name }.merge(options)
end
command_for(session) click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 20
def command_for(session)
  new(session: session)
end
inherit_args(args) click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 11
def inherit_args(args)
  @args ||= {}
  @args.merge(args || {})
end
inherited(sublass) click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 16
def inherited(sublass)
  sublass.inherit_args(@args)
end
new(options = nil) click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 35
def initialize(options = nil)
  options ||= {}
  options[:session] ||= default_client_or_session
  options.each do |sym, val|
    send "#{sym}=", val
  end
  raise Mongoid::Shell::Errors::MissingSessionError unless @session
end
option(name, options = {}) click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 29
def option(name, options = {})
  attr_accessor name unless instance_methods.include?(name)
  args[name] = { key: "--#{name}", property: name }.merge(options)
end

Public Instance Methods

cmd() click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 44
def cmd
  self.class.name.downcase.split(':').last
end
to_s(options = {}) click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 71
def to_s(options = {})
  [cmd, vargs(options)].flatten.compact.join(' ')
end
vargs(options = {}) click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 48
def vargs(options = {})
  mask_sensitive = options[:mask_sensitive]
  mask_sensitive = '********' if mask_sensitive && mask_sensitive.is_a?(TrueClass)
  self.class.args.values.map do |arg|
    key = arg[:key]
    value = send(arg[:property])
    next unless value
    case value
    when Boolean, TrueClass then key
    when Array then value.map { |v| "#{key} #{v}" }.join(' ')
    else
      if mask_sensitive && arg[:sensitive]
        value = mask_sensitive
      else
        # TODO: quote other special characters?
        value = value.to_s
        value = '"' + value + '"' if value.include? ' '
      end
      key ? "#{key} #{value}" : value
    end
  end
end

Private Instance Methods

default_client_or_session() click to toggle source
# File lib/mongoid/shell/commands/base.rb, line 78
def default_client_or_session
  Mongoid.default_session
end