Sha256: 3a3f7f587006d4169f51d164e2656cbd90b2fc84b0a4a5a935e794247f0767f2

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module Mongoid
  module Shell
    module Commands
      class Base

        attr_accessor :session

        class << self

          def command_for(session)
            self.new({ session: session })
          end

        end

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

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

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

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

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-shell-0.2.0 lib/mongoid/shell/commands/base.rb