class Diecut::PluginDescription

Constants

NO_VALUE

Attributes

context_defaults[R]
default_activated[R]
issue_handler[W]
name[R]
options[R]
resolve_block[R]
source_path[R]

Public Class Methods

new(name, source_path) click to toggle source
# File lib/diecut/plugin-description.rb, line 21
def initialize(name, source_path)
  @name = name
  @source_path = source_path
  @default_activated = true
  @context_defaults = []
  @options = []
  @resolve_block = nil
  @kind_stems = {}
end

Public Instance Methods

apply_resolve(ui, context) click to toggle source
# File lib/diecut/plugin-description.rb, line 63
def apply_resolve(ui, context)
  @resolve_block.call(ui, context)
end
default(context_path, value = NO_VALUE, &block) click to toggle source

Set a default value for a field in the templating context.

@param context_path [String, Array]

Either an array of strings or a dotted string (e.g.
"deeply.nested.value") that describes a path into the templating
context to give a default value to.

@param value

A simple default value, which will be used verbatim (n.b. it will be
cloned if appropriate, so you can use [] for an array).

@yieldreturn

A computed default value. The block will be called when the context is
set up. You cannot use both a simple value and a computed value.

@example

plugin.default("built_at"){ Time.now }
plugin.default("author", "Judson")
# File lib/diecut/plugin-description.rb, line 128
def default(context_path, value = NO_VALUE, &block)
  context_path =
    case context_path
    when Array
      context_path
    when /.+\..+/ # has an embedded .
      context_path.split('.')
    else
      [context_path]
    end
  if value != NO_VALUE and not block.nil?
    issue_handler.invalid_plugin(name, context_path, value)
  end
  @context_defaults << ContextDefault.new(context_path, value, block)
end
default_activated_for(kind) click to toggle source
# File lib/diecut/plugin-description.rb, line 50
def default_activated_for(kind)
  stem = @kind_stems[kind]
  if stem.default_activated.nil?
    default_active?
  else
    stem.default_activated
  end
end
default_active?() click to toggle source
# File lib/diecut/plugin-description.rb, line 59
def default_active?
  @default_activated
end
default_off() click to toggle source

Force this plugin to be enabled to be used. Good for optional features.

# File lib/diecut/plugin-description.rb, line 100
def default_off
  @default_activated = false
end
default_on() click to toggle source

Make this plugin part of the generation process by default. The is the default behavior anyway, provided for consistency.

# File lib/diecut/plugin-description.rb, line 106
def default_on
  @default_activated = true
end
for_kind(kind, templates = nil, stem = nil) { |kind_stem| ... } click to toggle source

Attaches this plugin to a particular kind of diecut generator. Can be called multiple times in order to reuse the plugin.

@param kind [String, Symbol]

The kind of generator to register the plugin to

@param templates [String]

The directory of templates that the plugin adds to the generation
process. Relative paths are resolved from the directory the plugin is
being defined in. If omitted (or nil) defaults to "templates"

@param stem [Array(String), String]

A prefix for the templates directory when it's used for this kind of
generator. By default, this will be [kind], which is what you'll
probably want in a gem plugin. For local plugins, you probably want to
have directories per kind, and set this to []

For instance, you might set up a plugin for Rails that also works in Xing projects that use Rails for a backend

@example Install for Rails and Xing

plugin.for_kind(:rails)
plugin.for_kind(:xing, nil, "xing/backend")
# File lib/diecut/plugin-description.rb, line 90
def for_kind(kind, templates = nil, stem = nil)
  templates ||= "diecut_templates"
  templates = File.expand_path(templates, File.dirname(caller_locations(1..1).first.absolute_path))
  kind_stem = KindStem.new(kind, stem, templates, nil)
  @kind_stems[kind] = kind_stem
  yield kind_stem if block_given?
  return kind_stem
end
has_kind?(kind) click to toggle source
# File lib/diecut/plugin-description.rb, line 46
def has_kind?(kind)
  @kind_stems.key?(kind)
end
issue_handler() click to toggle source
# File lib/diecut/plugin-description.rb, line 33
def issue_handler
  @issue_handler ||= Diecut.issue_handler
end
kinds() click to toggle source
# File lib/diecut/plugin-description.rb, line 38
def kinds
  @kind_stems.keys
end
option(name) { |option| ... } click to toggle source

Define an option to provide to the user interface. @param name [String,Symbol]

The name for the option, as it'll be provided to the user.

@yieldparam option [Option]

The option description object
# File lib/diecut/plugin-description.rb, line 149
def option(name)
  name = name.to_sym
  option = Option.new(name)
  yield option
  @options << option
  return option
end
resolve(&block) click to toggle source

The resolve block provides the loophole to allow complete configuration of the rendering context. The last thing that happens before files are generated is that all the plugin resolves are run, so that e.g. values can be calculated from other values. It’s very difficult to analyze resolve blocks, however: use them as sparingly as possible.

@yeildparam ui [UIContext]

the values supplied by the user to satisfy options

@yeildparam context [Configurable]

the configured rendering context
# File lib/diecut/plugin-description.rb, line 167
def resolve(&block)
  @resolve_block = block
end
stem_for(kind) click to toggle source
# File lib/diecut/plugin-description.rb, line 42
def stem_for(kind)
  @kind_stems.fetch(kind)
end