Class: Prigner::Template
- Inherits:
-
Object
- Object
- Prigner::Template
- Defined in:
- lib/prigner/template.rb
Overview
Project Template
This class implements several methods for load a template for a new project. Basically, a template is based in an YAML file for specfile
and a directory containing all files to be parsed.
The template tree is based in following structure:
namespace
`-- template
|-- models
`-- specfile
See Spec class for more information about mandatory attributes for draw your project using specfile
.
Defined Under Namespace
Classes: Option
Instance Attribute Summary collapse
-
#directories ⇒ Object
readonly
List of directories that will created in project tree.
-
#models ⇒ Object
readonly
List of models.
-
#name ⇒ Object
readonly
Name of template.
-
#namespace ⇒ Object
readonly
Namespace of template.
-
#options ⇒ Object
readonly
List of options (see Spec#options).
-
#path ⇒ Object
readonly
Path to template.
-
#spec ⇒ Object
readonly
Specifications.
Class Method Summary collapse
-
.all ⇒ Object
All templates grouped by namespace.
-
.all_template_paths ⇒ Object
Return all template paths placed in shared user or in project base directory.
-
.load(namespace, template = :default) ⇒ Object
Load template from shared directories.
Instance Method Summary collapse
-
#initialize(path) ⇒ Template
constructor
Initialize a template using a path.
-
#initialize_models_for_option(optname) ⇒ Object
If the option has list of files, then initialize all models.
-
#mask ⇒ Object
Mask for presentation of template.
Constructor Details
#initialize(path) ⇒ Template
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/prigner/template.rb', line 49 def initialize(path) @path = Pathname.new(path).tap{ |check| check.stat } @namespace = @path.parent.basename.to_s @name = @path.basename.to_s initialize_specfile initialize_directories initialize_models rescue Errno::ENOENT => error raise RuntimeError, error. end |
Instance Attribute Details
#directories ⇒ Object (readonly)
List of directories that will created in project tree.
30 31 32 |
# File 'lib/prigner/template.rb', line 30 def directories @directories end |
#models ⇒ Object (readonly)
List of models.
27 28 29 |
# File 'lib/prigner/template.rb', line 27 def models @models end |
#name ⇒ Object (readonly)
Name of template.
24 25 26 |
# File 'lib/prigner/template.rb', line 24 def name @name end |
#namespace ⇒ Object (readonly)
Namespace of template.
21 22 23 |
# File 'lib/prigner/template.rb', line 21 def namespace @namespace end |
#options ⇒ Object (readonly)
List of options (see Spec#options).
33 34 35 |
# File 'lib/prigner/template.rb', line 33 def @options end |
#path ⇒ Object (readonly)
Path to template.
36 37 38 |
# File 'lib/prigner/template.rb', line 36 def path @path end |
#spec ⇒ Object (readonly)
Specifications
39 40 41 |
# File 'lib/prigner/template.rb', line 39 def spec @spec end |
Class Method Details
.all ⇒ Object
All templates grouped by namespace.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/prigner/template.rb', line 85 def self.all all_template_paths.map do |path| new(path) end.inject({}) do |group, template| customized = template.path.to_s =~ %r/#{Prigner.user_home_basedir}/ group[template.namespace] ||= [] group[template.namespace] << [ template, customized ] group end end |
.all_template_paths ⇒ Object
Return all template paths placed in shared user or in project base directory.
78 79 80 81 82 |
# File 'lib/prigner/template.rb', line 78 def self.all_template_paths Prigner.shared_path.map do |source| Dir.glob("#{source}/*/*") end.flatten.compact end |
.load(namespace, template = :default) ⇒ Object
Load template from shared directories. The shared path set the home user directory and Prigner::Template shared files.
68 69 70 71 72 73 74 |
# File 'lib/prigner/template.rb', line 68 def self.load(namespace, template = :default) Prigner.shared_path.map do |source| path = "#{source}/#{namespace}/#{template}" return new(path) if File.exist? path end nil end |
Instance Method Details
#initialize_models_for_option(optname) ⇒ Object
If the option has list of files, then initialize all models.
97 98 99 100 101 102 |
# File 'lib/prigner/template.rb', line 97 def initialize_models_for_option(optname) option = optname.to_sym unless @options[option].files.empty? @models[option] = parse_models(@options[option].files) end end |
#mask ⇒ Object
Mask for presentation of template.
62 63 64 |
# File 'lib/prigner/template.rb', line 62 def mask @name == "default" ? @namespace : "#{@namespace}:#{@name}" end |