Class: Mongoid::Shell::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/shell/commands/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
19
20
# File 'lib/mongoid/shell/commands/base.rb', line 13

def initialize(options = nil)
  options ||= {}
  options[:session] ||= default_client_or_session
  options.each do |sym, val|
    send "#{sym}=", val
  end
  fail Mongoid::Shell::Errors::MissingSessionError unless @session
end

Instance Attribute Details

#sessionObject

Returns the value of attribute session.



5
6
7
# File 'lib/mongoid/shell/commands/base.rb', line 5

def session
  @session
end

Class Method Details

.command_for(session) ⇒ Object



8
9
10
# File 'lib/mongoid/shell/commands/base.rb', line 8

def command_for(session)
  new(session: session)
end

Instance Method Details

#cmdObject



22
23
24
# File 'lib/mongoid/shell/commands/base.rb', line 22

def cmd
  self.class.name.downcase.split(':').last
end

#to_sObject



42
43
44
# File 'lib/mongoid/shell/commands/base.rb', line 42

def to_s
  [cmd, vargs].flatten.compact.join(' ')
end

#vargs(args = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mongoid/shell/commands/base.rb', line 26

def vargs(args = {})
  args.map do |key, property|
    value = send(property)
    next unless value
    case value
    when Boolean, TrueClass then key
    when Array then value.map { |v| "#{key} #{v}" }.join(' ')
    else
      value = value.to_s
      # TODO: quote other special characters?
      value = '"' + value + '"' if value.include? ' '
      key[0] == '-' ? "#{key} #{value}" : value
    end
  end
end