Class: YAMLCommand::Command::SortCommand

Inherits:
YAMLCommand::Command show all
Defined in:
lib/yaml_command/sort.rb

Overview

Sort command.

Very limited at this point as it will only sort toplevel sequence or mappings.

Instance Attribute Summary collapse

Attributes inherited from YAMLCommand::Command

#file

Instance Method Summary collapse

Methods inherited from YAMLCommand::Command

command_name, #debug=, #debug?, #h!, #help!, #inspect=, #inspect?, #json=, #json?, #mute=, #mute?, #yaml=, #yaml?

Instance Attribute Details

#orderObject

Select ordering, descending or ascending (default). Use descending or reverse or abbreviations d or r for opposite order.



16
17
18
# File 'lib/yaml_command/sort.rb', line 16

def order
  @order
end

Instance Method Details

#call(ypath = nil) ⇒ Object

Sort in ascending or descending order.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yaml_command/sort.rb', line 20

def call(ypath=nil)
  if ypath
    raise NotImplementedError, "YPath not supported yet."
  end

  case data
  when Array
    new_data = data.sort
  when Hash
    new_data = {}
    data.keys.sort.each do |k|
      new_data[k] = data[k]
    end
  end

  #save
  output new_data
end