Class: Chef::Provisioning::AWSDriver::SuperLWRP

Inherits:
Resource::LWRPBase
  • Object
show all
Defined in:
lib/chef/provisioning/aws_driver/super_lwrp.rb

Direct Known Subclasses

AWSResource, Resource::AwsRoute53RecordSet

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(attr_name, validation_opts = {}) ⇒ Object

in Chef 12.5+, properties replace attributes and these respond to coerce and default with a lazy block - no need for overwriting!



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chef/provisioning/aws_driver/super_lwrp.rb', line 14

def self.attribute(attr_name, validation_opts={})
  if validation_opts[:default].is_a?(Chef::DelayedEvaluator)
    lazy_default = validation_opts.delete(:default)
  end
  coerce = validation_opts.delete(:coerce)
  if lazy_default || coerce
    define_method(attr_name) do |arg=nil|
      arg = instance_exec(arg, &coerce) if coerce && !arg.nil?

      result = set_or_return(attr_name.to_sym, arg, validation_opts)

      if result.nil? && arg.nil?
        result = instance_eval(&lazy_default) if lazy_default
      end

      result
    end
    define_method(:"#{attr_name}=") do |arg|
      if arg.nil?
        remove_instance_variable(:"@#{arg}")
      else
        set_or_return(attr_name.to_sym, arg, validation_opts)
      end
    end
  else
    super
  end
end

.lazy(&block) ⇒ Object

Below chef 12.5 you cannot do ‘default lazy: { … }` - this adds that



44
45
46
# File 'lib/chef/provisioning/aws_driver/super_lwrp.rb', line 44

def self.lazy(&block)
  Chef::DelayedEvaluator.new(&block)
end

Instance Method Details

#_pv_is(opts, key, to_be, raise_error: true) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/provisioning/aws_driver/super_lwrp.rb', line 51

def _pv_is(opts, key, to_be, raise_error: true)
  return true if !opts.has_key?(key.to_s) && !opts.has_key?(key.to_sym)
  value = _pv_opts_lookup(opts, key)
  to_be = [ to_be ].flatten(1)
  to_be.each do |tb|
    case tb
    when Proc
      return true if instance_exec(value, &tb)
    when Property
      validate(opts, { key => tb.validation_options })
      return true
    else
      return true if tb === value
    end
  end

  if raise_error
    raise ::Chef::Exceptions::ValidationFailed, "Option #{key} must be one of: #{to_be.join(", ")}!  You passed #{value.inspect}."
  else
    false
  end
end