Class: Howzit::Directive

Inherits:
Object
  • Object
show all
Defined in:
lib/howzit/directive.rb

Overview

Directive class Represents a parsed directive from topic content (tasks, conditionals, etc.)

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • type (Symbol)

    :task, :if, :unless, :elsif, :else, :end, :log_level

  • content (String) (defaults to: nil)

    The directive content (action, block, etc.)

  • condition (String, nil) (defaults to: nil)

    Condition string for conditionals

  • directive_type (String, nil) (defaults to: nil)

    ‘if’, ‘unless’, ‘elsif’, ‘else’ for conditionals

  • optional (Boolean) (defaults to: false)

    Whether task requires confirmation

  • default (Boolean) (defaults to: true)

    Default response for confirmation

  • line_number (Integer) (defaults to: nil)

    Line number in original content

  • conditional_path (Array) (defaults to: [])

    Array of conditional indices this directive is nested in

  • log_level_value (String, nil) (defaults to: nil)

    Log level value for @log_level directives

  • var_name (String, nil) (defaults to: nil)

    Variable name for @set_var directives

  • var_value (String, nil) (defaults to: nil)

    Variable value for @set_var directives



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

#conditionObject (readonly)

Returns the value of attribute condition.



9
10
11
# File 'lib/howzit/directive.rb', line 9

def condition
  @condition
end

#conditional_pathObject (readonly)

Returns the value of attribute conditional_path.



9
10
11
# File 'lib/howzit/directive.rb', line 9

def conditional_path
  @conditional_path
end

#contentObject (readonly)

Returns the value of attribute content.



9
10
11
# File 'lib/howzit/directive.rb', line 9

def content
  @content
end

#defaultObject (readonly)

Returns the value of attribute default.



9
10
11
# File 'lib/howzit/directive.rb', line 9

def default
  @default
end

#directive_typeObject (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_numberObject (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_valueObject (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

#optionalObject (readonly)

Returns the value of attribute optional.



9
10
11
# File 'lib/howzit/directive.rb', line 9

def optional
  @optional
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/howzit/directive.rb', line 9

def type
  @type
end

#var_nameObject (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_valueObject (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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


66
67
68
# File 'lib/howzit/directive.rb', line 66

def set_var?
  @type == :set_var
end

#task?Boolean

Is this a task directive?

Returns:

  • (Boolean)


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)

Parameters:

  • parent (Topic)

    The parent topic

  • current_log_level (String, nil) (defaults to: nil)

    Current log level to apply to task

Returns:

  • (Task)

    Task object



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