Class: KXI::CLI::PropertyList

Inherits:
Object
  • Object
show all
Defined in:
lib/kxi/cli/property_list.rb

Overview

Property list renderer

Instance Method Summary collapse

Constructor Details

#initializePropertyList

Instantiates the KXI::CLI::PropertyList class



8
9
10
11
# File 'lib/kxi/cli/property_list.rb', line 8

def initialize
	@align = 0
	@data = {}
end

Instance Method Details

#field(name, value) ⇒ Object

Sets a property

Parameters:

  • name (string)

    Name of the property

  • value (string, Array<string>)

    Value of the property



16
17
18
19
# File 'lib/kxi/cli/property_list.rb', line 16

def field(name, value)
	@data[name] = value
	@align = name.length if name.length > @align
end

#render(cols = 4) ⇒ Object

Renders the property list into stdout

Parameters:

  • cols (integer) (defaults to: 4)

    Determines the maximal number of columns for array rendering



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kxi/cli/property_list.rb', line 23

def render(cols = 4)
	@data.each_pair do |k, v|
		if v.kind_of?(Array)
			print("#{pad(k)}: ")
			v.each_index do |idx|
				if idx > 0 and idx % cols == 0
					puts('')
					print(' ' * (@align + 2))
				elsif idx > 0
					print(', ')
				end
				print(v[idx])
			end
			puts('')
		else
			puts("#{pad(k)}: #{v}")
		end
	end
end