Class: Yaml::Sort::List

Inherits:
Value
  • Object
show all
Defined in:
lib/yaml/sort/list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Value

#comments

Constructor Details

#initialize(items = []) ⇒ List

Returns a new instance of List.



8
9
10
11
# File 'lib/yaml/sort/list.rb', line 8

def initialize(items = [])
  super()
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



6
7
8
# File 'lib/yaml/sort/list.rb', line 6

def items
  @items
end

Class Method Details

.create(dash, value) ⇒ Object



17
18
19
20
21
# File 'lib/yaml/sort/list.rb', line 17

def self.create(dash, value)
  list = List.new
  list.add_item(dash, value)
  list
end

Instance Method Details

#add_item(dash, value) ⇒ Object



13
14
15
# File 'lib/yaml/sort/list.rb', line 13

def add_item(dash, value)
  @items << [dash, value]
end

#sortObject



29
30
31
32
# File 'lib/yaml/sort/list.rb', line 29

def sort
  # TODO: Add an option to sort scalar values
  List.new(items.map { |i| [i[0], i[1].sort] })
end

#to_sObject



23
24
25
26
27
# File 'lib/yaml/sort/list.rb', line 23

def to_s
  super + items.map do |item|
    "#{item[0]}#{item[1].to_s(skip_first_indent: true)}"
  end.join("\n")
end