# class Chef::Resource::Conditional

### Attributes

block[R]
command[R]
command_opts[R]
positivity[R]

### Public Class Methods

new(positivity, parent_resource, command=nil, command_opts={}, &block) click to toggle source

    # File lib/chef/resource/conditional.rb, line 46
    def initialize(positivity, parent_resource, command=nil, command_opts={}, &block)
      @positivity = positivity
      case command
      when String
        @guard_interpreter = new_guard_interpreter(parent_resource, command, command_opts, &block)
        @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?
        @guard_interpreter = nil
        @command, @command_opts = nil, nil
        @block = block
      else
        raise ArgumentError, "Invalid only_if/not_if command: #{command.inspect} (#{command.class})"
      end
    end

not_if(parent_resource, command=nil, command_opts={}, &block) click to toggle source

    # File lib/chef/resource/conditional.rb, line 33
    def self.not_if(parent_resource, command=nil, command_opts={}, &block)
      new(:not_if, parent_resource, command, command_opts, &block)
    end

only_if(parent_resource, command=nil, command_opts={}, &block) click to toggle source

    # File lib/chef/resource/conditional.rb, line 37
    def self.only_if(parent_resource, command=nil, command_opts={}, &block)
      new(:only_if, parent_resource, command, command_opts, &block)
    end

### Public Instance Methods

continue?() click to toggle source

    # File lib/chef/resource/conditional.rb, line 63
    def continue?
      case @positivity
      when :only_if
        evaluate
      when :not_if
        !evaluate
      else
        raise "Cannot evaluate resource conditional of type #{@positivity}"
      end
    end

description() click to toggle source

    # File lib/chef/resource/conditional.rb, line 93
    def description
      cmd_or_block = @command ? "command `#{@command}`" : "ruby block"
      "#{@positivity} #{cmd_or_block}"
    end

evaluate() click to toggle source

    # File lib/chef/resource/conditional.rb, line 74
    def evaluate
      @guard_interpreter ? evaluate_command : evaluate_block
    end

evaluate_block() click to toggle source

    # File lib/chef/resource/conditional.rb, line 85
    def evaluate_block
      @block.call
    end

evaluate_command() click to toggle source

    # File lib/chef/resource/conditional.rb, line 78
    def evaluate_command
      @guard_interpreter.evaluate
    rescue Chef::Exceptions::CommandTimeout
      Chef::Log.warn "Command '#{@command}' timed out"
      false
    end

short_description() click to toggle source

    # File lib/chef/resource/conditional.rb, line 89
    def short_description
      @positivity
    end

to_text() click to toggle source

    # File lib/chef/resource/conditional.rb, line 98
    def to_text
      if @command
        "#{positivity} \"#{@command}\""
      else
        "#{@positivity} { #code block }"
      end
    end