Class: Prigner::Template

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Template

Initialize a template using a path.

Example:

template = Prigner::Template.new "path/to/template"

The template initialization will search the specfile in path passed as argument (path/to/template/specfile) for Spec attributes.



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_options
  initialize_directories
  initialize_models
rescue Errno::ENOENT => error
  raise RuntimeError, error.message
end

Instance Attribute Details

#directoriesObject (readonly)

List of directories that will created in project tree.



30
31
32
# File 'lib/prigner/template.rb', line 30

def directories
  @directories
end

#modelsObject (readonly)

List of models.



27
28
29
# File 'lib/prigner/template.rb', line 27

def models
  @models
end

#nameObject (readonly)

Name of template.



24
25
26
# File 'lib/prigner/template.rb', line 24

def name
  @name
end

#namespaceObject (readonly)

Namespace of template.



21
22
23
# File 'lib/prigner/template.rb', line 21

def namespace
  @namespace
end

#optionsObject (readonly)

List of options (see Spec#options).



33
34
35
# File 'lib/prigner/template.rb', line 33

def options
  @options
end

#pathObject (readonly)

Path to template.



36
37
38
# File 'lib/prigner/template.rb', line 36

def path
  @path
end

#specObject (readonly)

Specifications



39
40
41
# File 'lib/prigner/template.rb', line 39

def spec
  @spec
end

Class Method Details

.allObject

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_pathsObject

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

#maskObject

Mask for presentation of template.



62
63
64
# File 'lib/prigner/template.rb', line 62

def mask
  @name == "default" ? @namespace : "#{@namespace}:#{@name}"
end