Class: Howzit::Directive
- Inherits:
-
Object
- Object
- Howzit::Directive
- Defined in:
- lib/howzit/directive.rb
Overview
Directive class Represents a parsed directive from topic content (tasks, conditionals, etc.)
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#conditional_path ⇒ Object
readonly
Returns the value of attribute conditional_path.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#directive_type ⇒ Object
readonly
Returns the value of attribute directive_type.
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
-
#log_level_value ⇒ Object
readonly
Returns the value of attribute log_level_value.
-
#optional ⇒ Object
readonly
Returns the value of attribute optional.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#var_name ⇒ Object
readonly
Returns the value of attribute var_name.
-
#var_value ⇒ Object
readonly
Returns the value of attribute var_value.
Instance Method Summary collapse
-
#conditional? ⇒ Boolean
Is this a conditional directive?.
-
#initialize(type:, content: nil, condition: nil, directive_type: nil, optional: false, default: true, line_number: nil, conditional_path: [], log_level_value: nil, var_name: nil, var_value: nil) ⇒ Directive
constructor
Initialize a Directive.
-
#log_level? ⇒ Boolean
Is this a log_level directive?.
-
#set_var? ⇒ Boolean
Is this a set_var directive?.
-
#task? ⇒ Boolean
Is this a task directive?.
-
#to_task(parent, current_log_level: nil) ⇒ Task
Convert directive to a Task object (only works for task directives).
Constructor Details
#initialize(type:, content: nil, condition: nil, directive_type: nil, optional: false, default: true, line_number: nil, conditional_path: [], log_level_value: nil, var_name: nil, var_value: nil) ⇒ Directive
Initialize a Directive
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/howzit/directive.rb', line 27 def initialize(type:, content: nil, condition: nil, directive_type: nil, optional: false, default: true, line_number: nil, conditional_path: [], log_level_value: nil, var_name: nil, var_value: nil) @type = type @content = content @condition = condition @directive_type = directive_type @optional = optional @default = default @line_number = line_number @conditional_path = conditional_path || [] @log_level_value = log_level_value @var_name = var_name @var_value = var_value end |
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def condition @condition end |
#conditional_path ⇒ Object (readonly)
Returns the value of attribute conditional_path.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def conditional_path @conditional_path end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def content @content end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def default @default end |
#directive_type ⇒ Object (readonly)
Returns the value of attribute directive_type.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def directive_type @directive_type end |
#line_number ⇒ Object (readonly)
Returns the value of attribute line_number.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def line_number @line_number end |
#log_level_value ⇒ Object (readonly)
Returns the value of attribute log_level_value.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def log_level_value @log_level_value end |
#optional ⇒ Object (readonly)
Returns the value of attribute optional.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def optional @optional end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def type @type end |
#var_name ⇒ Object (readonly)
Returns the value of attribute var_name.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def var_name @var_name end |
#var_value ⇒ Object (readonly)
Returns the value of attribute var_value.
9 10 11 |
# File 'lib/howzit/directive.rb', line 9 def var_value @var_value end |
Instance Method Details
#conditional? ⇒ Boolean
Is this a conditional directive?
45 46 47 |
# File 'lib/howzit/directive.rb', line 45 def conditional? i[if unless elsif else end].include?(@type) end |
#log_level? ⇒ Boolean
Is this a log_level directive?
59 60 61 |
# File 'lib/howzit/directive.rb', line 59 def log_level? @type == :log_level end |
#set_var? ⇒ Boolean
Is this a set_var directive?
66 67 68 |
# File 'lib/howzit/directive.rb', line 66 def set_var? @type == :set_var end |
#task? ⇒ Boolean
Is this a task directive?
52 53 54 |
# File 'lib/howzit/directive.rb', line 52 def task? @type == :task end |
#to_task(parent, current_log_level: nil) ⇒ Task
Convert directive to a Task object (only works for task directives)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/howzit/directive.rb', line 78 def to_task(parent, current_log_level: nil) return nil unless task? task_data = @content.dup task_type = task_data[:type] # Apply current log level if set and task doesn't have its own task_data[:log_level] = current_log_level if current_log_level && !task_data[:log_level] # Set named_arguments before processing titles for variable substitution # Merge with existing named_arguments to preserve variables set by scripts Howzit.named_arguments ||= {} Howzit.named_arguments.merge!(parent.named_args) if parent.named_args # Pass source_file from parent topic to task task_data[:source_file] = parent.respond_to?(:source_file) ? parent.source_file : nil case task_type when :block # Block tasks need title rendering # Note: Action substitution happens at execution time in Task#run_block # so variables from previous run blocks are available title = task_data[:title] title = title.render_arguments if title && !title.empty? task_data[:title] = title # Don't substitute variables in action here - do it at execution time task_data[:parent] = parent Howzit::Task.new(task_data, optional: @optional, default: @default) when :run # Run tasks need title and action rendering (similar to define_task_args) title = task_data[:title] title = title.render_arguments if title && !title.empty? task_data[:title] = title # Apply variable substitution to action action = task_data[:action] action = action.render_arguments if action && !action.empty? task_data[:action] = action task_data[:parent] = parent Howzit::Task.new(task_data, optional: @optional, default: @default) when :copy # Copy tasks need title rendering and action escaping title = task_data[:title] title = title.render_arguments if title && !title.empty? task_data[:title] = title # Apply variable substitution to action before escaping action = task_data[:action] action = action.render_arguments if action && !action.empty? task_data[:action] = Shellwords.escape(action) task_data[:parent] = parent Howzit::Task.new(task_data, optional: @optional, default: @default) when :open # Open tasks need title rendering title = task_data[:title] title = title.render_arguments if title && !title.empty? task_data[:title] = title task_data[:parent] = parent Howzit::Task.new(task_data, optional: @optional, default: @default) when :include # Include tasks need special handling (title processing, arguments, etc.) title = task_data[:title] if title =~ /\[(.*?)\] *$/ args = Regexp.last_match(1).split(/ *, */).map(&:render_arguments) Howzit.arguments = args parent.arguments title.sub!(/ *\[.*?\] *$/, '') end title = title.render_arguments if title && !title.empty? task_data[:title] = title task_data[:parent] = parent task_data[:arguments] = Howzit.named_arguments Howzit::Task.new(task_data, optional: @optional, default: @default) else task_data[:parent] = parent Howzit::Task.new(task_data, optional: @optional, default: @default) end end |