Class: AppArchetype::TemplateManager
- Inherits:
-
Object
- Object
- AppArchetype::TemplateManager
- 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
-
#manifests ⇒ Object
readonly
Returns the value of attribute manifests.
Instance Method Summary collapse
-
#filter(query = DEFAULT_QUERY) ⇒ Array
Filter executes a query function in a select call against each manifest in the manager’s collection.
-
#find_by_name(name, ignore_dupe: false) ⇒ AppArchetype::Template::Manifest
Finds a specific manifest by name and returns it to the caller.
-
#initialize(template_dir) ⇒ TemplateManager
constructor
Creates a manager.
-
#load ⇒ Object
Loads and parses each manifest within the template directory into memory.
-
#manifest_names ⇒ Array
Returns a list of manifest names from loaded templates.
-
#search_by_name(name) ⇒ Array
Searches for manifests matching given name and returns it to caller.
Constructor Details
#initialize(template_dir) ⇒ TemplateManager
Creates a manager
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
#manifests ⇒ Object (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.
54 55 56 57 58 |
# File 'lib/app_archetype/template_manager.rb', line 54 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')
96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/app_archetype/template_manager.rb', line 96 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 |
#load ⇒ Object
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 |
# File 'lib/app_archetype/template_manager.rb', line 26 def load Dir.glob( File.join(@template_dir, '**', 'manifest.json*') ).each do |manifest| @manifests << AppArchetype::Template::Manifest.new_from_file(manifest) rescue StandardError puts "WARN: `#{manifest}` is invalid, skipping" next end end |
#manifest_names ⇒ Array
Returns a list of manifest names from loaded templates
116 117 118 |
# File 'lib/app_archetype/template_manager.rb', line 116 def manifest_names @manifests.map(&:name) 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')
71 72 73 74 75 76 77 |
# File 'lib/app_archetype/template_manager.rb', line 71 def search_by_name(name) name_query = lambda do |template| template.name.include?(name) end filter(name_query) end |