Class: Locomotive::Wagon::Generators::Site::List

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/locomotive/wagon/generators/site.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Returns a new instance of List.



62
63
64
# File 'lib/locomotive/wagon/generators/site.rb', line 62

def initialize
  self._list = []
end

Instance Attribute Details

#_listObject

Returns the value of attribute _list.



60
61
62
# File 'lib/locomotive/wagon/generators/site.rb', line 60

def _list
  @_list
end

Instance Method Details

#get(name) ⇒ Object

Return the information about a generator from its name.

Parameters:

  • name (String)

    The name of the generator

Returns:

  • (Object)

    The information of the found generator or nil



72
73
74
# File 'lib/locomotive/wagon/generators/site.rb', line 72

def get(name)
  self._list.detect { |entry| entry.name == name.to_sym }
end

#register(name, klass, description = nil) ⇒ Boolean

Register a generator by adding it to the list of existing generators.

Parameters:

  • name (String)

    The name of the generator

  • klass (Class)

    The class of the generator

  • description (String) (defaults to: nil)

    The description of the generator (can be nil)

Returns:

  • (Boolean)

    True if the registration has been successful, false otherwise.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/locomotive/wagon/generators/site.rb', line 84

def register(name, klass, description = nil)
  return false unless self.get(name).nil?

  self._list << OpenStruct.new({
    name:         name.to_sym,
    klass:        klass,
    description:  description ? description.strip.gsub("\n", '') : nil
  })

  self._list.last
end

#to_jsonString

Return the list of site templates in JSON

Returns:

  • (String)

    JSON output



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/locomotive/wagon/generators/site.rb', line 100

def to_json
  self._list.map do |template|
    # puts template.klass.class_options.inspect
    # puts class_options_to_json
    path = template.klass.source_root ? File.expand_path(template.klass.source_root) : nil
    icon = path ? File.join(path, 'icon.png') : nil

    {
      name:         template.name,
      description:  template.description,
      path:         path,
      icon:         icon && File.exist?(icon) ? icon : nil,
      options:      class_options_to_json(template)
    }
  end.to_json
end