Class: ConfCtl::NixFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/nix_format.rb

Overview

Format Ruby values in Nix

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sort: true) ⇒ NixFormat

Returns a new instance of NixFormat.

Parameters:

  • sort (Boolean) (defaults to: true)

    sort hash keys



14
15
16
17
18
# File 'lib/confctl/nix_format.rb', line 14

def initialize(sort: true)
  @sort = sort
  @output = ''
  @level = 0
end

Class Method Details

.to_nix(value, sort: true) ⇒ String

Parameters:

  • value (any)
  • sort (Boolean) (defaults to: true)

    sort hash keys

Returns:

  • (String)


9
10
11
# File 'lib/confctl/nix_format.rb', line 9

def self.to_nix(value, sort: true)
  new(sort:).to_nix(value)
end

Instance Method Details

#indentObject



34
35
36
37
38
# File 'lib/confctl/nix_format.rb', line 34

def indent
  @level += 2
  yield
  @level -= 2
end

#pad(str, indent: true) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/confctl/nix_format.rb', line 26

def pad(str, indent: true)
  if indent
    "#{' ' * @level}#{str}"
  else
    str
  end
end

#to_nix(value) ⇒ String

Parameters:

  • value (any)

Returns:

  • (String)


22
23
24
# File 'lib/confctl/nix_format.rb', line 22

def to_nix(value)
  format_value(value).strip
end