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

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Base

Returns a new instance of Base.



35
36
37
38
39
40
41
42
# 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

Class Attribute Details

.argsObject

Returns the value of attribute args.



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

def args
  @args
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

.arg(name, options = {}) ⇒ Object



24
25
26
27
# 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) ⇒ Object



20
21
22
# File 'lib/mongoid/shell/commands/base.rb', line 20

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

.inherit_args(args) ⇒ Object



11
12
13
14
# File 'lib/mongoid/shell/commands/base.rb', line 11

def inherit_args(args)
  @args ||= {}
  @args.merge(args || {})
end

.inherited(sublass) ⇒ Object



16
17
18
# File 'lib/mongoid/shell/commands/base.rb', line 16

def inherited(sublass)
  sublass.inherit_args(@args)
end

.option(name, options = {}) ⇒ Object



29
30
31
32
# 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

Instance Method Details

#cmdObject



44
45
46
# File 'lib/mongoid/shell/commands/base.rb', line 44

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

#to_s(options = {}) ⇒ Object



71
72
73
# File 'lib/mongoid/shell/commands/base.rb', line 71

def to_s(options = {})
  [cmd, vargs(options)].flatten.compact.join(' ')
end

#vargs(options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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