Class: Chef::Resource::Conditional

Inherits:
Object
  • Object
show all
Includes:
Mixin::ShellOut
Defined in:
lib/chef/resource/conditional.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::ShellOut

#shell_out, #shell_out!

Constructor Details

#initialize(positivity, command = nil, command_opts = {}, &block) ⇒ Conditional

Returns a new instance of Conditional.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chef/resource/conditional.rb', line 45

def initialize(positivity, command=nil, command_opts={}, &block)
  @positivity = positivity
  case command
  when String
    @command, @command_opts = command, command_opts
    @block = nil
  when nil
    raise ArgumentError, "only_if/not_if requires either a command or a block" unless block_given?
    @command, @command_opts = nil, nil
    @block = block
  else
    raise ArgumentError, "Invalid only_if/not_if command: #{command.inspect} (#{command.class})"
  end
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



43
44
45
# File 'lib/chef/resource/conditional.rb', line 43

def block
  @block
end

#commandObject (readonly)

Returns the value of attribute command.



41
42
43
# File 'lib/chef/resource/conditional.rb', line 41

def command
  @command
end

#command_optsObject (readonly)

Returns the value of attribute command_opts.



42
43
44
# File 'lib/chef/resource/conditional.rb', line 42

def command_opts
  @command_opts
end

#positivityObject (readonly)

Returns the value of attribute positivity.



40
41
42
# File 'lib/chef/resource/conditional.rb', line 40

def positivity
  @positivity
end

Class Method Details

.not_if(command = nil, command_opts = {}, &block) ⇒ Object



32
33
34
# File 'lib/chef/resource/conditional.rb', line 32

def self.not_if(command=nil, command_opts={}, &block)
  new(:not_if, command, command_opts, &block)
end

.only_if(command = nil, command_opts = {}, &block) ⇒ Object



36
37
38
# File 'lib/chef/resource/conditional.rb', line 36

def self.only_if(command=nil, command_opts={}, &block)
  new(:only_if, command, command_opts, &block)
end

Instance Method Details

#continue?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
# File 'lib/chef/resource/conditional.rb', line 60

def continue?
  case @positivity
  when :only_if
    evaluate
  when :not_if
    !evaluate
  else
    raise "Cannot evaluate resource conditional of type #{@positivity}"
  end
end

#descriptionObject



83
84
85
86
# File 'lib/chef/resource/conditional.rb', line 83

def description
  cmd_or_block = @command ? "command `#{@command}`" : "ruby block"
  "#{@positivity} #{cmd_or_block}"
end

#evaluateObject



71
72
73
# File 'lib/chef/resource/conditional.rb', line 71

def evaluate
  @command ? evaluate_command : evaluate_block
end

#evaluate_blockObject



79
80
81
# File 'lib/chef/resource/conditional.rb', line 79

def evaluate_block
  @block.call
end

#evaluate_commandObject



75
76
77
# File 'lib/chef/resource/conditional.rb', line 75

def evaluate_command
  shell_out(@command, @command_opts).status.success?
end