Class: Stic::Generator

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

Overview

A Generator generates blobs or specific subclasses based on some input.

As an example see Stic::Generators::Static that generates blobs for static files.

A generator will be initialized by the system and it’s #run method will be invoked. The generator now can add blobs to the ‘site`. Subclass must override the #run method providing custom logic.

Direct Known Subclasses

Stic::Generators::Static

Attributes collapse

Construction collapse

Accessors collapse

Actions collapse

Constructor Details

#initialize(site, config) ⇒ Generator

Initialize new Stic::Generator.

Parameters:

  • site (Site)

    Site object.

  • config (Hash)

    Hash object containing options for all generators. The generator picks it own options from the passed hash based on the generator’s #name.



40
41
42
43
# File 'lib/stic/generator.rb', line 40

def initialize(site, config)
  @site   = site
  @config = ::ActiveSupport::HashWithIndifferentAccess.new((config && config[name]) || {})
end

Instance Attribute Details

#configHashWithIndifferentAccess (readonly)

The generator specific configuration. The values are passed from Site and are loaded from ‘generators.<generator name>`.

Returns:

  • (HashWithIndifferentAccess)

    Option hash.



29
30
31
# File 'lib/stic/generator.rb', line 29

def config
  @config
end

#siteSite (readonly)

The Site object.

Returns:



21
22
23
# File 'lib/stic/generator.rb', line 21

def site
  @site
end

Instance Method Details

#disabled?Boolean

Check if generator is disabled.

A generator can be disabled by setting the generator’s ‘disable` configuration key to either ’true’ or ‘yes’.

Returns:

  • (Boolean)

    True unless generator is disabled.



63
64
65
# File 'lib/stic/generator.rb', line 63

def disabled?
  %w(true yes).includes? config['disable'].to_s
end

#nameString

Return generator name. The name is derived from the class name as an underscore string with ‘_generator` striped from the end.

Returns:

  • (String)

    Generator name.



52
53
54
# File 'lib/stic/generator.rb', line 52

def name
  self.class.name.underscore.gsub(/_generator$/, '')
end

#runObject

Run this Stic::Generator. This is the place to implement custom logic and add Blobs to the site.

The #run method must be implemented by a subclass.



74
75
76
# File 'lib/stic/generator.rb', line 74

def run
  raise NotImplementedError.new "#{self.class.name}#run not implemented."
end