Class: Coque::Rb

Inherits:
Cmd
  • Object
show all
Defined in:
lib/coque/rb.rb

Constant Summary collapse

NOOP =
Proc.new { }

Instance Attribute Summary collapse

Attributes inherited from Cmd

#context

Attributes included from Redirectable

#stderr, #stdin, #stdout

Instance Method Summary collapse

Methods inherited from Cmd

#get_default_fds, #pipe, #|

Methods included from Runnable

#log_start, #run, #run!, #success?, #to_a, #to_a!

Methods included from Redirectable

#<, #>, #>=, #err, #getio, #in, #out

Constructor Details

#initialize(context = Context.new, &block) ⇒ Rb

Returns a new instance of Rb.



5
6
7
8
9
10
11
12
13
14
# File 'lib/coque/rb.rb', line 5

def initialize(context = Context.new, &block)
  if block_given?
    @block = block
  else
    @block = NOOP
  end
  @pre_block = nil
  @post_block = nil
  @context = context
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



4
5
6
# File 'lib/coque/rb.rb', line 4

def block
  @block
end

#post_blockObject (readonly)

Returns the value of attribute post_block.



4
5
6
# File 'lib/coque/rb.rb', line 4

def post_block
  @post_block
end

#pre_blockObject (readonly)

Returns the value of attribute pre_block.



4
5
6
# File 'lib/coque/rb.rb', line 4

def pre_block
  @pre_block
end

Instance Method Details

#cloneObject



16
17
18
# File 'lib/coque/rb.rb', line 16

def clone
  self.class.new(context, &block).pre(&pre_block).post(&post_block)
end

#get_resultObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/coque/rb.rb', line 34

def get_result
  stdin, stdoutr, stdoutw = get_default_fds

  pid = fork do
    STDOUT.reopen(stdoutw)
    Dir.chdir(context.dir)
    if context.disinherits_env?
      ENV.clear
    end
    context.env.each do |k,v|
      ENV[k] = v
    end
    @pre_block.call if @pre_block
    stdin.each_line(&@block)
    @post_block.call if @post_block
  end
  stdoutw.close
  Result.new(pid, stdoutr)
end

#post(&block) ⇒ Object



27
28
29
30
31
32
# File 'lib/coque/rb.rb', line 27

def post(&block)
  if block_given?
    @post_block = block
  end
  self
end

#pre(&block) ⇒ Object



20
21
22
23
24
25
# File 'lib/coque/rb.rb', line 20

def pre(&block)
  if block_given?
    @pre_block = block
  end
  self
end