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.
-
#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 |
#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
41 42 43 |
# File 'lib/diecut/plugin-description.rb', line 41 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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/diecut/plugin-description.rb', line 104 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? 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
37 38 39 |
# File 'lib/diecut/plugin-description.rb', line 37 def default_active? @default_activated end |
#default_off ⇒ Object
Force this plugin to be enabled to be used. Good for optional features.
76 77 78 |
# File 'lib/diecut/plugin-description.rb', line 76 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.
82 83 84 |
# File 'lib/diecut/plugin-description.rb', line 82 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
68 69 70 71 72 73 |
# File 'lib/diecut/plugin-description.rb', line 68 def for_kind(kind, templates = nil, stem = nil) stem ||= [kind] templates ||= "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
33 34 35 |
# File 'lib/diecut/plugin-description.rb', line 33 def has_kind?(kind) @kind_stems.key?(kind) end |
#kinds ⇒ Object
25 26 27 |
# File 'lib/diecut/plugin-description.rb', line 25 def kinds @kind_stems.keys end |
#option(name) {|option| ... } ⇒ Object
Define an option to provide to the user interface.
125 126 127 128 129 130 131 |
# File 'lib/diecut/plugin-description.rb', line 125 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.
143 144 145 |
# File 'lib/diecut/plugin-description.rb', line 143 def resolve(&block) @resolve_block = block end |
#stem_for(kind) ⇒ Object
29 30 31 |
# File 'lib/diecut/plugin-description.rb', line 29 def stem_for(kind) @kind_stems.fetch(kind) end |