Class: EacRubyUtils::Envs::Executable

Inherits:
Object
  • Object
show all
Includes:
Listable, SimpleCache
Defined in:
lib/eac_ruby_utils/envs/executable.rb

Defined Under Namespace

Classes: ProgramNotFoundError

Constant Summary collapse

DEFAULT_AUTO_VALIDATE =
true

Constants included from SimpleCache

SimpleCache::UNCACHED_METHOD_NAME_SUFFIX, SimpleCache::UNCACHED_METHOD_PATTERN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SimpleCache

#method_missing, #reset_cache, #respond_to_missing?, #sanitize_cache_key, uncached_method_name

Constructor Details

#initialize(env, name, *check_args) ⇒ Executable

Returns a new instance of Executable.



18
19
20
21
22
23
24
# File 'lib/eac_ruby_utils/envs/executable.rb', line 18

def initialize(env, name, *check_args)
  @env = env
  @name = name
  self.options = self.class.lists.option.hash_keys_validate!(check_args.extract_options!)
  options[OPTION_CHECK_ARGS] = check_args unless options.key?(OPTION_CHECK_ARGS)
  options.freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class EacRubyUtils::SimpleCache

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



16
17
18
# File 'lib/eac_ruby_utils/envs/executable.rb', line 16

def env
  @env
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/eac_ruby_utils/envs/executable.rb', line 16

def name
  @name
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/eac_ruby_utils/envs/executable.rb', line 16

def options
  @options
end

Instance Method Details

#auto_validate?Boolean

Returns:



26
27
28
# File 'lib/eac_ruby_utils/envs/executable.rb', line 26

def auto_validate?
  options.key?(OPTION_AUTO_VALIDATE) ? options[OPTION_AUTO_VALIDATE] : DEFAULT_AUTO_VALIDATE
end

#check_argsObject



30
31
32
# File 'lib/eac_ruby_utils/envs/executable.rb', line 30

def check_args
  options[OPTION_CHECK_ARGS]
end

#command(*command_args) ⇒ Object



50
51
52
53
# File 'lib/eac_ruby_utils/envs/executable.rb', line 50

def command(*command_args)
  validate! if auto_validate?
  env.command(*executable_args, *command_args)
end

#executable_argsObject



55
56
57
# File 'lib/eac_ruby_utils/envs/executable.rb', line 55

def executable_args
  executable_args_from_envvar || executable_args_from_options || executable_args_from_name
end

#executable_args_envvarObject



59
60
61
# File 'lib/eac_ruby_utils/envs/executable.rb', line 59

def executable_args_envvar
  "#{name}_command".variableize.upcase
end

#executable_args_from_envvarObject



63
64
65
# File 'lib/eac_ruby_utils/envs/executable.rb', line 63

def executable_args_from_envvar
  ENV[executable_args_envvar].if_present { |v| ::Shellwords.split(v) }
end

#executable_args_from_nameEnumerable<String>

Returns:



68
69
70
# File 'lib/eac_ruby_utils/envs/executable.rb', line 68

def executable_args_from_name
  ::Shellwords.split(name)
end

#executable_args_from_optionsEnumerable<String>

Returns:



73
74
75
76
77
# File 'lib/eac_ruby_utils/envs/executable.rb', line 73

def executable_args_from_options
  options[OPTION_EXEC_ARGS].if_present do |v|
    v.is_a?(::Enumerable) ? v.map(&:to_s) : ::Shellwords.split(v.to_s)
  end
end

#exist?Boolean

Returns:



34
35
36
# File 'lib/eac_ruby_utils/envs/executable.rb', line 34

def exist?
  exist
end

#validateObject



38
39
40
41
42
# File 'lib/eac_ruby_utils/envs/executable.rb', line 38

def validate
  return nil if exist?

  "Program \"#{::Shellwords.join(executable_args)}\" not found in environment #{env}"
end

#validate!Object



44
45
46
47
48
# File 'lib/eac_ruby_utils/envs/executable.rb', line 44

def validate!
  message = validate

  raise ProgramNotFoundError, message if message
end