Class: Yaml::Sort::Dictionary
- Defined in:
- lib/yaml/sort/dictionary.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Class Method Summary collapse
Instance Method Summary collapse
- #add_item(key, value) ⇒ Object
-
#initialize(items = []) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #sort ⇒ Object
- #to_s(skip_first_indent: false) ⇒ Object
Methods inherited from Value
Constructor Details
#initialize(items = []) ⇒ Dictionary
Returns a new instance of Dictionary.
8 9 10 11 |
# File 'lib/yaml/sort/dictionary.rb', line 8 def initialize(items = []) super() @items = items end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
6 7 8 |
# File 'lib/yaml/sort/dictionary.rb', line 6 def items @items end |
Class Method Details
.create(key, value) ⇒ Object
17 18 19 20 21 |
# File 'lib/yaml/sort/dictionary.rb', line 17 def self.create(key, value) dict = Dictionary.new dict.add_item(key, value) dict end |
Instance Method Details
#add_item(key, value) ⇒ Object
13 14 15 |
# File 'lib/yaml/sort/dictionary.rb', line 13 def add_item(key, value) @items << [key, value] end |
#sort ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/yaml/sort/dictionary.rb', line 35 def sort Dictionary.new(items.map { |a| [a[0], a[1].sort] }.sort do |a, b| if a[0].value == "lookup_options:" -1 elsif b[0].value == "lookup_options:" 1 else a[0] <=> b[0] end end) end |
#to_s(skip_first_indent: false) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/yaml/sort/dictionary.rb', line 23 def to_s(skip_first_indent: false) n = -1 super + items.map do |k, v| n += 1 case v when List, Dictionary then "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)}\n#{v}" when Scalar, Alias then "#{k.to_s(skip_first_indent: skip_first_indent && n.zero?)} #{v}" else raise "Unexpected type" end end.join("\n") end |