Class: Ready::ZshScript

Inherits:
Object
  • Object
show all
Defined in:
lib/ready/zsh_script.rb

Overview

Assembles a zsh script by rendering one ZshFunction per name, building the functions concurrently.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(names: [], environment: {}) ⇒ ZshScript

Returns a new instance of ZshScript.



11
12
13
14
15
16
# File 'lib/ready/zsh_script.rb', line 11

def initialize(names: [], environment: {})
  @names = names
  @environment = environment
  @mutex = Mutex.new
  @script = StringIO.new
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



9
10
11
# File 'lib/ready/zsh_script.rb', line 9

def environment
  @environment
end

#namesObject

Returns the value of attribute names.



8
9
10
# File 'lib/ready/zsh_script.rb', line 8

def names
  @names
end

Instance Method Details

#add_eager_loading!Object



35
36
37
# File 'lib/ready/zsh_script.rb', line 35

def add_eager_loading!
  #    @source.puts 'by -e "ActiveSupport.eager_load!"'
end

#multithread_process(names) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ready/zsh_script.rb', line 18

def multithread_process(names)
  threads = names.map do |name|
    Thread.new do
      zsh_body = ZshFunction.new(name, environment: environment).to_s
      @mutex.synchronize do
        @script.puts zsh_body
      end
    end
  end

  threads.each(&:join)
end

#set_env(key, value) ⇒ Object



31
32
33
# File 'lib/ready/zsh_script.rb', line 31

def set_env(key, value)
  @environment[key] = value
end

#to_sObject



39
40
41
42
43
# File 'lib/ready/zsh_script.rb', line 39

def to_s
  add_eager_loading!
  multithread_process(@names)
  @script.string
end