Class: UnimatrixCLI::Command

Inherits:
Object
  • Object
show all
Includes:
UnimatrixParser
Defined in:
lib/unimatrix_cli/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UnimatrixParser

included

Constructor Details

#initializeCommand

Returns a new instance of Command.



8
9
10
# File 'lib/unimatrix_cli/command.rb', line 8

def initialize
  @options = self.class.options
end

Class Method Details

.available_commandsObject



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/unimatrix_cli/command.rb', line 122

def available_commands
  commands = Command.descendants
  commands.delete( UnimatrixCLI::CLI )

  parsed_commands = commands.map do | command |
    command_words = command.to_s.split( '::' )
    command_words.shift
    command_words.last.slice!( 'Command' )
    command_words.map( &:underscore ).join( '::' )
  end

  parsed_commands.sort.join( "\n" )
end

.descendantsObject



118
119
120
# File 'lib/unimatrix_cli/command.rb', line 118

def descendants
  ObjectSpace.each_object( Class ).select { | klass | klass < self }
end

Instance Method Details

#read_file(path) ⇒ Object


File Reading



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/unimatrix_cli/command.rb', line 43

def read_file( path )
  begin
    file = File.open( path, 'r' )
    extension = path.split( '.' ).last.downcase
    contents = file_to_hash( file, extension )
  rescue
    write( 
      message: "There was a problem accessing and reading #{ path }",
      error: true
    )
  ensure
    file.close
  end
end

#validate(object, type) ⇒ Object



24
25
26
27
28
# File 'lib/unimatrix_cli/command.rb', line 24

def validate( object, type )
  object.present? &&
  object.is_a?( "Unimatrix::#{ type }".constantize ) &&
  object.errors.blank? rescue nil
end

#validate_collection(objects, type) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/unimatrix_cli/command.rb', line 30

def validate_collection( objects, type )
  if objects.present?
    objects.all? do | object |
      validate( object, type )
    end
  else
    false
  end
end

#write(options) ⇒ Object

TODO add color options



16
17
18
19
20
21
22
# File 'lib/unimatrix_cli/command.rb', line 16

def write( options )
  if options[ :error ]
    $stderr.puts options[ :message ]
  else
    $stdout.puts options[ :message ]
  end
end