Class: AppArchetype::TemplateManager

Inherits:
Object
  • Object
show all
Defined in:
lib/app_archetype/template_manager.rb

Overview

Manager looks after a set of archetypes

Constant Summary collapse

DEFAULT_QUERY =

Default filter is a lambda that returns true for all manifests

->(_manifest) { return true }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_dir) ⇒ TemplateManager

Creates a manager

Parameters:



16
17
18
19
# File 'lib/app_archetype/template_manager.rb', line 16

def initialize(template_dir)
  @template_dir = template_dir
  @manifests = []
end

Instance Attribute Details

#manifestsObject (readonly)

Returns the value of attribute manifests.



9
10
11
# File 'lib/app_archetype/template_manager.rb', line 9

def manifests
  @manifests
end

Instance Method Details

#filter(query = DEFAULT_QUERY) ⇒ Array

Filter executes a query function in a select call against each manifest in the manager’s collection.

The given query function should be something that will evaluate to true when the manifest matches - this will hence filter the manifest set to the filtered set.

Examples:

manager = AppArchetype::TemplateManager.new('/path/to/templates')
query = -> (manifest) { manifest.name = "fudge" }

fudge_templates = manager.filter(query)

Parameters:

  • query (Lambda) (defaults to: DEFAULT_QUERY)

Returns:

  • (Array)


56
57
58
59
60
# File 'lib/app_archetype/template_manager.rb', line 56

def filter(query = DEFAULT_QUERY)
  @manifests.select do |template|
    query.call(template)
  end
end

#find_by_name(name, ignore_dupe: false) ⇒ AppArchetype::Template::Manifest

Finds a specific manifest by name and returns it to the caller.

It is possible that a more than one manifest is found when searching by name. If this happens while ignore_dupe is set to false, then a Runtime error is raised. If ignore_dupe is set to false then the first matching manifest is returned.

@example:

manager = AppArchetype::TemplateManager.new('/path/to/templates')
fudge_manifest = manager.find('fudge')

Parameters:

  • name (String)
  • ignore_dupe (Boolean) (defaults to: false)

Returns:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/app_archetype/template_manager.rb', line 98

def find_by_name(name, ignore_dupe: false)
  name_query = lambda do |template|
    template.name == name
  end

  results = filter(name_query)

  if results.count > 1 && ignore_dupe == false
    raise 'more than one manifest matching the'\
    ' given name were found'
  end

  results.first
end

#loadObject

Loads and parses each manifest within the template directory into memory. Any invalid manifests are ignored and a message is printed to STDOUT indicating which manifest is not valid.



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/app_archetype/template_manager.rb', line 26

def load
  Dir.glob(
    File.join(@template_dir, '**', 'manifest.json*')
  ).each do |manifest|
    begin
      @manifests << AppArchetype::Template::Manifest.new_from_file(manifest)
    rescue StandardError
      puts "WARN: `#{manifest}` is invalid, skipping"
      next
    end
  end
end

#search_by_name(name) ⇒ Array

Searches for manifests matching given name and returns it to caller.

@example:

manager = AppArchetype::TemplateManager.new('/path/to/templates')
fudge_manifest = manager.find('fudge')

Parameters:

Returns:

  • (Array)


73
74
75
76
77
78
79
# File 'lib/app_archetype/template_manager.rb', line 73

def search_by_name(name)
  name_query = lambda do |template|
    template.name.include?(name)
  end

  filter(name_query)
end