Class: AIA::Directives

Inherits:
Object
  • Object
show all
Defined in:
lib/aia/directives.rb

Instance Method Summary collapse

Instance Method Details

#box(what) ⇒ Object



38
39
40
41
42
# File 'lib/aia/directives.rb', line 38

def box(what)
  f   = what[0]
  bar = "#{f}"*what.size
  puts "#{bar}\n#{what}\n#{bar}"
end

#config(what) ⇒ Object

Allows a prompt to change its configuration environment



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/aia/directives.rb', line 46

def config(what)
  parts = what.split(' ')
  item  = parts.shift
  parts.shift if %w[:= =].include? parts[0]

  if '<<' == parts[0]
    parts.shift
    value = parts.join
    if AIA.config(item).is_a?(Array)
      AIA.config[item] << value
    else
      AIA.config[item] = [ value ]
    end
  else
    value = parts.join
    if item.end_with?('?')
      AIA.config[item] = %w[1 y yea yes t true].include?(value.downcase)
    elsif item.end_with?('_file')
      if "STDOUT" == value.upcase
        AIA.config[item] = STDOUT
      elsif "STDERR" == value.upcase
        AIA.config[item] = STDERR
      else
        AIA.config[item] = value.start_with?('/') ? 
          Pathname.new(value) :
          Pathname.pwd + value
      end
    elsif %w[next pipeline].include? item.downcase
      pipeline(value)
    else
      AIA.config[item] = value
    end
  end

  nil
end

#execute_my_directivesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aia/directives.rb', line 14

def execute_my_directives
  return if AIA.config.directives.nil? || AIA.config.directives.empty?
  
  result    = ""
  not_mine  = []

  AIA.config.directives.each do |entry|
    directive   = entry[0].to_sym
    parameters  = entry[1]

    if respond_to? directive
      output  = send(directive, parameters)
      result << "#{output}\n" unless output.nil?
    else
      not_mine << entry
    end
  end

  AIA.config.directives = not_mine

  result.empty? ? nil : result
end

#include(path_to_file) ⇒ Object

when path_to_file is relative it will be relative to the PWD.

TODO: Consider an AIA_INCLUDE_DIR –include_dir option to be used for all relative include paths



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/aia/directives.rb', line 117

def include(path_to_file)
  path = Pathname.new path_to_file
  if path.exist? && path.readable?
    content = path.readlines.reject do |a_line|
      a_line.strip.start_with?(AIA::Prompt::COMMENT_SIGNAL) ||
      a_line.strip.start_with?(AIA::Prompt::DIRECTIVE_SIGNAL)
    end.join("\n")
  else
    abort "ERROR: could not include #{path_to_file}"
  end

  content
end

#pipeline(what) ⇒ Object Also known as: next

//next id //pipeline id1,id2, id3 , id4



104
105
106
107
108
# File 'lib/aia/directives.rb', line 104

def pipeline(what)
  return if what.empty?
  AIA.config.pipeline << what.split(',').map(&:strip)
  AIA.config.pipeline.flatten!
end

#ruby(code) ⇒ Object



137
138
139
140
141
# File 'lib/aia/directives.rb', line 137

def ruby(code)
  output = eval(code)

  output.is_a?(String) ? output : nil
end

#shell(command) ⇒ Object



132
133
134
# File 'lib/aia/directives.rb', line 132

def shell(command)
  `#{command}`
end