Class: RemoteRuby::AdapterBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_ruby/adapter_builder.rb

Overview

Builds connection adapter based on the provided parameters. Can wrap the adapter in caching and text mode adapters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter_klass: nil, use_cache: false, save_cache: false, **params) ⇒ AdapterBuilder

Returns a new instance of AdapterBuilder.



9
10
11
12
13
14
15
16
# File 'lib/remote_ruby/adapter_builder.rb', line 9

def initialize(adapter_klass: nil, use_cache: false, save_cache: false, **params)
  @adapter_klass = adapter_klass
  @use_cache = use_cache
  @save_cache = save_cache
  @adapter_params = params

  RemoteRuby.ensure_cache_dir if save_cache
end

Instance Attribute Details

#adapter_paramsObject (readonly)

Returns the value of attribute adapter_params.



7
8
9
# File 'lib/remote_ruby/adapter_builder.rb', line 7

def adapter_params
  @adapter_params
end

#save_cacheObject (readonly)

Returns the value of attribute save_cache.



7
8
9
# File 'lib/remote_ruby/adapter_builder.rb', line 7

def save_cache
  @save_cache
end

#text_modeObject (readonly)

Returns the value of attribute text_mode.



7
8
9
# File 'lib/remote_ruby/adapter_builder.rb', line 7

def text_mode
  @text_mode
end

#use_cacheObject (readonly)

Returns the value of attribute use_cache.



7
8
9
# File 'lib/remote_ruby/adapter_builder.rb', line 7

def use_cache
  @use_cache
end

Instance Method Details

#adapter_klassObject



18
19
20
21
22
23
24
25
26
# File 'lib/remote_ruby/adapter_builder.rb', line 18

def adapter_klass
  return @adapter_klass if @adapter_klass

  if adapter_params[:host]
    ::RemoteRuby::SSHAdapter
  else
    ::RemoteRuby::TmpFileAdapter
  end
end

#build(code_hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/remote_ruby/adapter_builder.rb', line 28

def build(code_hash)
  res = adapter_klass.new(**adapter_params)

  cache_mode = use_cache && cache_exists?(code_hash)

  if cache_mode
    cache_adapter(code_hash, res.connection_name)
  elsif save_cache
    caching_adapter(res, code_hash)
  else
    res
  end
end

#cache_adapter(code_hash, connection_name) ⇒ Object



42
43
44
45
46
47
# File 'lib/remote_ruby/adapter_builder.rb', line 42

def cache_adapter(code_hash, connection_name)
  ::RemoteRuby::CacheAdapter.new(
    cache_path: cache_path(code_hash),
    connection_name: connection_name
  )
end

#cache_exists?(code_hash) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/remote_ruby/adapter_builder.rb', line 70

def cache_exists?(code_hash)
  hsh = cache_path(code_hash)
  File.exist?("#{hsh}.stdout") || File.exist?("#{hsh}.stderr") || File.exist?("#{hsh}.result")
end

#cache_path(code_hash) ⇒ Object



65
66
67
68
# File 'lib/remote_ruby/adapter_builder.rb', line 65

def cache_path(code_hash)
  hsh = context_hash(code_hash)
  File.join(RemoteRuby.cache_dir, hsh)
end

#caching_adapter(adapter, code_hash) ⇒ Object



49
50
51
52
53
54
# File 'lib/remote_ruby/adapter_builder.rb', line 49

def caching_adapter(adapter, code_hash)
  ::RemoteRuby::CachingAdapter.new(
    adapter: adapter,
    cache_path: cache_path(code_hash)
  )
end

#context_hash(code_hash) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/remote_ruby/adapter_builder.rb', line 56

def context_hash(code_hash)
  Digest::MD5.hexdigest(
    self.class.name +
    adapter_klass.name.to_s +
    adapter_params.to_s +
    code_hash
  )
end