Class: Diecut::PluginDescription
- Inherits:
-
Object
- Object
- Diecut::PluginDescription
- Includes:
- CallerLocationsPolyfill
- Defined in:
- lib/diecut/plugin-description.rb,
lib/diecut/plugin-description/option.rb,
lib/diecut/plugin-description/context-default.rb
Defined Under Namespace
Classes: ContextDefault, KindStem, Option
Constant Summary collapse
- NO_VALUE =
Object.new.freeze
Constants included from CallerLocationsPolyfill
CallerLocationsPolyfill::LINE_RE
Instance Attribute Summary collapse
-
#context_defaults ⇒ Object
readonly
Returns the value of attribute context_defaults.
-
#default_activated ⇒ Object
readonly
Returns the value of attribute default_activated.
- #issue_handler ⇒ Object
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resolve_block ⇒ Object
readonly
Returns the value of attribute resolve_block.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
Instance Method Summary collapse
- #apply_resolve(ui, context) ⇒ Object
-
#default(context_path, value = NO_VALUE, &block) ⇒ Object
Set a default value for a field in the templating context.
- #default_active? ⇒ Boolean
-
#default_off ⇒ Object
Force this plugin to be enabled to be used.
-
#default_on ⇒ Object
Make this plugin part of the generation process by default.
-
#for_kind(kind, templates = nil, stem = nil) ⇒ Object
Attaches this plugin to a particular kind of diecut generator.
- #has_kind?(kind) ⇒ Boolean
-
#initialize(name, source_path) ⇒ PluginDescription
constructor
A new instance of PluginDescription.
- #kinds ⇒ Object
-
#option(name) {|option| ... } ⇒ Object
Define an option to provide to the user interface.
-
#resolve(&block) ⇒ Object
The resolve block provides the loophole to allow complete configuration of the rendering context.
- #stem_for(kind) ⇒ Object
Methods included from CallerLocationsPolyfill
Constructor Details
#initialize(name, source_path) ⇒ PluginDescription
Returns a new instance of PluginDescription.
13 14 15 16 17 18 19 20 21 |
# File 'lib/diecut/plugin-description.rb', line 13 def initialize(name, source_path) @name = name @source_path = source_path @default_activated = true @context_defaults = [] @options = [] @resolve_block = nil @kind_stems = {} end |
Instance Attribute Details
#context_defaults ⇒ Object (readonly)
Returns the value of attribute context_defaults.
22 23 24 |
# File 'lib/diecut/plugin-description.rb', line 22 def context_defaults @context_defaults end |
#default_activated ⇒ Object (readonly)
Returns the value of attribute default_activated.
22 23 24 |
# File 'lib/diecut/plugin-description.rb', line 22 def default_activated @default_activated end |
#issue_handler ⇒ Object
25 26 27 |
# File 'lib/diecut/plugin-description.rb', line 25 def issue_handler @issue_handler ||= Diecut.issue_handler end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
22 23 24 |
# File 'lib/diecut/plugin-description.rb', line 22 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
22 23 24 |
# File 'lib/diecut/plugin-description.rb', line 22 def @options end |
#resolve_block ⇒ Object (readonly)
Returns the value of attribute resolve_block.
22 23 24 |
# File 'lib/diecut/plugin-description.rb', line 22 def resolve_block @resolve_block end |
#source_path ⇒ Object (readonly)
Returns the value of attribute source_path.
22 23 24 |
# File 'lib/diecut/plugin-description.rb', line 22 def source_path @source_path end |
Instance Method Details
#apply_resolve(ui, context) ⇒ Object
46 47 48 |
# File 'lib/diecut/plugin-description.rb', line 46 def apply_resolve(ui, context) @resolve_block.call(ui, context) end |
#default(context_path, value = NO_VALUE, &block) ⇒ Object
Set a default value for a field in the templating context.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/diecut/plugin-description.rb', line 109 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) raise InvalidPlugin, "Default on #{name.inspect} both has a simple default value (#{value}) and a dynamic block value, which isn't allowed." end @context_defaults << ContextDefault.new(context_path, value, block) end |
#default_active? ⇒ Boolean
42 43 44 |
# File 'lib/diecut/plugin-description.rb', line 42 def default_active? @default_activated end |
#default_off ⇒ Object
Force this plugin to be enabled to be used. Good for optional features.
81 82 83 |
# File 'lib/diecut/plugin-description.rb', line 81 def default_off @default_activated = false end |
#default_on ⇒ Object
Make this plugin part of the generation process by default. The is the default behavior anyway, provided for consistency.
87 88 89 |
# File 'lib/diecut/plugin-description.rb', line 87 def default_on @default_activated = true end |
#for_kind(kind, templates = nil, stem = nil) ⇒ Object
Attaches this plugin to a particular kind of diecut generator. Can be called multiple times in order to reuse the plugin.
For instance, you might set up a plugin for Rails that also works in Xing projects that use Rails for a backend
73 74 75 76 77 78 |
# File 'lib/diecut/plugin-description.rb', line 73 def for_kind(kind, templates = nil, stem = nil) stem ||= [kind] templates ||= "diecut_templates" templates = File.(templates, File.dirname(caller_locations(1..1).first.absolute_path)) @kind_stems[kind] = KindStem.new(kind, stem, templates) end |
#has_kind?(kind) ⇒ Boolean
38 39 40 |
# File 'lib/diecut/plugin-description.rb', line 38 def has_kind?(kind) @kind_stems.key?(kind) end |
#kinds ⇒ Object
30 31 32 |
# File 'lib/diecut/plugin-description.rb', line 30 def kinds @kind_stems.keys end |
#option(name) {|option| ... } ⇒ Object
Define an option to provide to the user interface.
131 132 133 134 135 136 137 |
# File 'lib/diecut/plugin-description.rb', line 131 def option(name) name = name.to_sym option = Option.new(name) yield option @options << option return option end |
#resolve(&block) ⇒ Object
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.
149 150 151 |
# File 'lib/diecut/plugin-description.rb', line 149 def resolve(&block) @resolve_block = block end |
#stem_for(kind) ⇒ Object
34 35 36 |
# File 'lib/diecut/plugin-description.rb', line 34 def stem_for(kind) @kind_stems.fetch(kind) end |