Class: Babushka::DepTemplate

Inherits:
Object
  • Object
show all
Includes:
LogHelpers
Defined in:
lib/babushka/dep_template.rb

Overview

This class represents a template against which deps can be defined. The resulting deps are structured just like regular ones, except for the context against which they were defined.

Standard deps are defined against DepContext, which makes just the basic dep DSL available, i.e. requires/met?/meet, etc. Templated deps are defined against a subclass of TemplatedDepContext as built by build_context.

This means that when a templated dep is defined, the context will be a superset of that of a standard dep – the normal stuff, plus whatever the template adds.

Constant Summary collapse

INVALID_NAMES =
%w[base]
VALID_NAME_START_CHARS =
/[a-z]/
VALID_NAME_CHARS =
/#{VALID_NAME_START_CHARS}[a-z0-9_]*/
VALID_NAME_START =
/^#{VALID_NAME_START_CHARS}/
VALID_NAME =
/\A#{VALID_NAME_CHARS}\z/m
TEMPLATE_NAME_MATCH =
/\A.+\.(#{VALID_NAME_CHARS})\z/m

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Constructor Details

#initialize(name, source, opts, &block) ⇒ DepTemplate

Returns a new instance of DepTemplate.



55
56
57
58
59
60
# File 'lib/babushka/dep_template.rb', line 55

def initialize name, source, opts, &block
  @name, @source, @opts, @block = name, source, opts, block
  debug "Defining #{source.name}:#{name} template"
  @context_class = build_context block
  source.templates.register self
end

Instance Attribute Details

#context_classObject (readonly)

Returns the value of attribute context_class.



51
52
53
# File 'lib/babushka/dep_template.rb', line 51

def context_class
  @context_class
end

#nameObject (readonly)

Returns the value of attribute name.



51
52
53
# File 'lib/babushka/dep_template.rb', line 51

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



51
52
53
# File 'lib/babushka/dep_template.rb', line 51

def opts
  @opts
end

#sourceObject (readonly)

Returns the value of attribute source.



51
52
53
# File 'lib/babushka/dep_template.rb', line 51

def source
  @source
end

Class Method Details

.for(supplied_name, source, opts, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/babushka/dep_template.rb', line 33

def self.for supplied_name, source, opts, &block
  name = supplied_name.to_s.downcase

  if name.empty?
    raise ArgumentError, "You can't define a template with a blank name."
  elsif INVALID_NAMES.include? name
    raise ArgumentError, "You can't use '#{name}' for a template name, because it's reserved."
  elsif name[VALID_NAME_START].nil?
    raise ArgumentError, "You can't use '#{name}' for a template name - it must start with a letter."
  elsif name[VALID_NAME].nil?
    raise ArgumentError, "You can't use '#{name}' for a template name - it can only contain [a-z0-9_]."
  elsif Base.sources.current_load_source.templates.for(name)
    raise ArgumentError, "A template called '#{name}' has already been defined."
  else
    new name, source, opts, &block
  end
end

Instance Method Details

#build_context(block) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/babushka/dep_template.rb', line 75

def build_context block
  Class.new(TemplatedDepContext, &block).tap {|context|
    shadow = self
    context.metaclass.send :define_method, :source_template do
      shadow
    end
  }
end

#contextual_nameObject

Returns this template’s name, including the source name as a prefix if this template is in a cloneable source.

A cloneable source is one that babushka knows how to automatically update; i.e. a source that babushka could have installed itself.

In effect, a cloneable source is one whose deps you prefix when you run them, so this method returns the template’s name in the same form as you would refer to it when using it from another source.



71
72
73
# File 'lib/babushka/dep_template.rb', line 71

def contextual_name
  source.cloneable? ? "#{source.name}:#{name}" : name
end

#descObject



53
# File 'lib/babushka/dep_template.rb', line 53

def desc; context_class.desc end