Class: Huck::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/huck/generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'lib/huck/generator.rb', line 5

def config
  @config
end

Class Method Details

.factory(kwargs = {}) ⇒ Object

Given a generator’s name (or no name), return a new generator instance

Parameters:

name

The name of the generator, or empty/nil to guess

config

A configuration hash

Returns:

A Huck::Generator instance



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/huck/generator.rb', line 37

def self.factory kwargs = {}
  name = Huck::getarg kwargs, :name, nil
  config = Huck::getarg kwargs, :config, nil

  if name.nil?
    if Huck::try_load 'facter'
      name = 'facter'
    elsif Huck::try_load 'ohai'
      name = 'ohai'
    else
      raise RuntimeError, 'unable to load any generators'
    end
  end

  case name
  when 'facter'
    g = Generators::FacterGenerator.new
  when 'ohai'
    g = Generators::OhaiGenerator.new
  when 'yaml'
    g = Generators::YamlGenerator.new
  when 'json'
    g = Generators::JsonGenerator.new
  else
    raise RuntimeError, "bad generator: #{name}"
  end

  g.config = config
  return g
end

Instance Method Details

#dump(kwargs = {}) ⇒ Object

This method will call the generation method, and return the data in the desired format.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/huck/generator.rb', line 9

def dump kwargs = {}
  format = Huck::getarg kwargs, :format, 'json'
  data = generate
  if !data.is_a? Hash
    raise RuntimeError, 'cannot handle non-hash data'
  end

  case format
  when 'json'
    return JSON.dump data
  when 'yaml'
    return YAML.dump data
  else
    raise RuntimeError, "unknown format '#{format}'"
  end
end