Class: Bauk::Gen::Module

Inherits:
Object
  • Object
show all
Includes:
ConfigUtils, Utils::Log
Defined in:
lib/bauk/gen/module.rb

Overview

This class is the base module that all others should extend from. It contains methods for generating items and creating default config. It itself is not a valid module as modules need names All config for a module needs to be in the config modules->#name It takes 3 arguments:

  • Calling generator

  • Config generated by the generator

  • Map of options This map contains the possible keys:

    • disable_by_default: Whether to not enable this generator unless the

      config explicity sets this value to true or a hash.
      e.g {modules:{moduleA:true}
      

Instance Method Summary collapse

Methods included from ConfigUtils

#underscore, #validate_config_item

Constructor Details

#initialize(generator, config, map = {}) ⇒ Module

Returns a new instance of Module.



29
30
31
32
33
# File 'lib/bauk/gen/module.rb', line 29

def initialize(generator, config, map = {})
  @generator_config = config
  @generator = generator
  @map = map
end

Instance Method Details

#active?Boolean

Function to tell the calling generator whether this module is active andshould be used to generate items. It depends on whether the config privided

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/bauk/gen/module.rb', line 49

def active?
  if @map[:disable_by_default] then @config
  else !@config.eql?(false)
  end
end

#configObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bauk/gen/module.rb', line 35

def config
  return @config if @config
  @config = default_config.deep_merge! @generator_config
  @config = @generator_config
  @config[:modules] ||= {}
  @config[:modules][name] ||= {}
  @config[:modules][name] = {} if config[:modules][name].eql? true
  # Overwrite values with this module values
  @config = @config.deep_merge! @config[:modules][name]
  config
end

#input_generatorsObject

This function contains the default list of inputs It defaults to using the inputs from the root generator



62
63
64
# File 'lib/bauk/gen/module.rb', line 62

def input_generators
  @generator.input_generators
end

#input_items(items) ⇒ Object

This function gets the items from the inputs. It does so by itterating though each input and passing it the global list of items to add to.



69
70
71
72
73
74
# File 'lib/bauk/gen/module.rb', line 69

def input_items(items)
  log.info "Inputting items from module: #{name} (#{self.class.name})"
  input_generators.each do |i_gen|
    i_gen.new(self, config).input_items(items)
  end
end

#nameObject

Method to get generator name from class



77
78
79
80
81
82
83
84
# File 'lib/bauk/gen/module.rb', line 77

def name
  return @name if @name

  @name = underscore(self.class.name.split('::').join('_').sub(/_*module$/i, '')).to_sym
  raise "Invalid nameless module provided: #{self.class.name}" if @name.empty?

  @name
end

#validate_config(_generator_config) ⇒ Object

Method to check validate the config being provided to the generator. This overrride also takes an argument of the generator config in case default values want to be derived.



58
# File 'lib/bauk/gen/module.rb', line 58

def validate_config(_generator_config); end