Top Level Namespace

Defined Under Namespace

Modules: Pattern Classes: Context, DSLCompiler, LanguageProtocol, OutputType, Psql, Python, R, Raka, RubyP, Shell, Token

Instance Method Summary collapse

Instance Method Details

#array_to_hash(array) ⇒ Object



7
8
9
# File 'lib/raka/compile.rb', line 7

def array_to_hash(array)
  array.nil? ? {} : Hash[((0...array.size).map { |i| i.to_s.to_sym }).zip array]
end

#bash(env, cmd) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/raka/lang/psql/impl.rb', line 5

def bash(env, cmd)
  code = remove_common_indent(
    %(set -e
      set -o pipefail

      #{cmd}
    )
  )
  # puts code
  env.send :sh, 'bash ' + create_tmp(code)
end

#create_tmp(content) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/raka/protocol.rb', line 10

def create_tmp(content)
  tmpfile = "/tmp/#{SecureRandom.uuid}"

  File.open(tmpfile, 'w') do |f|
    f.write content
  end

  tmpfile
end

#creator(name, klass, global_defaults = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/raka/protocol.rb', line 117

def creator(name, klass, global_defaults = {})
  global_config = global_defaults
  define_singleton_method name do |*args, **kwargs, &block|
    # pick keyword arguments for klass
    kwargs = global_config.merge kwargs
    impl = klass.new(*args, **pick_kwargs(klass, kwargs))
    proto = LanguageProtocol.new(impl, **pick_kwargs(LanguageProtocol, kwargs))
    if block
      proto.block = block
      [proto]
    else
      proto.define_singleton_method :config do |**config|
        global_config = global_defaults.merge config
      end
      proto # if no block, allow configure or waiting for * to add code text
    end
  end
end

#internal(name) ⇒ Object

methods like xx are preserved for token



22
23
24
# File 'lib/raka/token.rb', line 22

def internal(name)
  name.length > 1 && name.to_s.start_with?('_') && name.to_s.end_with?('_')
end

#pick_kwargs(klass, kwargs) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/raka/protocol.rb', line 108

def pick_kwargs(klass, kwargs)
  param_ref = klass.instance_method(:initialize).parameters
    .select { |arg| arg.size == 2 && (arg[0] == :key || arg[0] == :keyreq) }
    .map { |arg| arg[1] }
  kwargs.select do |key, _value|
    param_ref.include? key
  end
end

#protect_percent_symbol(text) ⇒ Object



11
12
13
14
15
16
# File 'lib/raka/compile.rb', line 11

def protect_percent_symbol(text)
  anchor = '-_-_-'
  safe_text = text.gsub(/%(?=[^\s{]+)/, anchor) # replace % not in shape of %{ to special sign
  safe_text = yield safe_text
  safe_text.gsub(anchor, '%') # replace % not in shape of %{ to special sign
end

#remove_common_indent(code) ⇒ Object



6
7
8
# File 'lib/raka/protocol.rb', line 6

def remove_common_indent(code)
  code.gsub(/^#{code.scan(/^[ \t]+(?=\S)/).min}/, '')
end

#run(&block) ⇒ Object

use run instead of “ruby” to avoid name collision



82
83
84
# File 'lib/raka/protocol.rb', line 82

def run(&block)
  [RubyP.new(&block)]
end

#run_cmd(env, cmd) ⇒ Object

helper functions to implement LanguageImpl



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/raka/protocol.rb', line 87

def run_cmd(env, cmd)
  env.logger.debug(cmd)
  out_r, out_w = IO.pipe
  err_r, err_w = IO.pipe
  if env.logger.level <= 0
    pid = spawn(cmd, out: out_w)
    Thread.new do
      env.logger.debug(out_r.gets) until out_r.eof
    end
  elsif env.logger.level == 1
    pid = spawn(cmd, out: out_w)
  else
    pid = spawn(cmd, out: out_w, err: err_w)
  end

  Process.wait pid
  out_w.close
  err_w.close
  err_r.close
end