Module: Berkshelf::Formatters

Defined in:
lib/berkshelf/formatters.rb,
lib/berkshelf/formatters/json.rb,
lib/berkshelf/formatters/null.rb,
lib/berkshelf/formatters/human_readable.rb

Defined Under Namespace

Modules: AbstractFormatter Classes: HumanReadable, JSON, Null

Constant Summary collapse

@@formatters =
Hash.new

Class Method Summary collapse

Class Method Details

.formattersHash

Access the formatters map that links string symbols to Formatter implementations

Returns:

  • (Hash)


10
11
12
# File 'lib/berkshelf/formatters.rb', line 10

def formatters
  @@formatters
end

.get(id) ⇒ ~AbstractFormatter? Also known as: []

Parameters:

  • id (#to_sym)

Returns:



38
39
40
41
42
43
44
# File 'lib/berkshelf/formatters.rb', line 38

def get(id)
  unless id.respond_to?(:to_sym)
    raise ArgumentError, "Invalid Formatter ID: must respond to #to_sym. You gave: #{id}"
  end

  self.formatters.fetch(id.to_sym, nil)
end

.register(id, klass) ⇒ Hash

Returns a hash of registered formatters.

Parameters:

  • id (#to_sym)
  • klass (Constant)

Returns:

  • (Hash)

    a hash of registered formatters

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/berkshelf/formatters.rb', line 22

def register(id, klass)
  unless id.respond_to?(:to_sym)
    raise ArgumentError, "Invalid Formatter ID: must respond to #to_sym. You gave: #{id}"
  end

  id = id.to_sym
  if self.formatters.has_key?(id)
    raise Berkshelf::InternalError, "Formatter ID '#{id}' already registered"
  end

  self.formatters[id] = klass
end