Class: Chef::ResourceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/resource_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type: nil, name: nil, created_at: nil, params: nil, run_context: nil, cookbook_name: nil, recipe_name: nil, enclosing_provider: nil, new_resource: nil) ⇒ ResourceBuilder

FIXME (ruby-2.1 syntax): most of these are mandatory



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/chef/resource_builder.rb', line 35

def initialize(type: nil, name: nil, created_at: nil, params: nil, run_context: nil, cookbook_name: nil, recipe_name: nil, enclosing_provider: nil, new_resource: nil)
  @type               = type
  @name               = name
  @created_at         = created_at
  @params             = params
  @run_context        = run_context
  @cookbook_name      = cookbook_name
  @recipe_name        = recipe_name
  @enclosing_provider = enclosing_provider
  @new_resource       = new_resource
end

Instance Attribute Details

#cookbook_nameObject (readonly)

Returns the value of attribute cookbook_name.



28
29
30
# File 'lib/chef/resource_builder.rb', line 28

def cookbook_name
  @cookbook_name
end

#created_atObject (readonly)

Returns the value of attribute created_at.



25
26
27
# File 'lib/chef/resource_builder.rb', line 25

def created_at
  @created_at
end

#enclosing_providerObject (readonly)

Returns the value of attribute enclosing_provider.



30
31
32
# File 'lib/chef/resource_builder.rb', line 30

def enclosing_provider
  @enclosing_provider
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/chef/resource_builder.rb', line 24

def name
  @name
end

#new_resourceObject (readonly)

Returns the value of attribute new_resource.



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

def new_resource
  @new_resource
end

#paramsObject (readonly)

Returns the value of attribute params.



26
27
28
# File 'lib/chef/resource_builder.rb', line 26

def params
  @params
end

#recipe_nameObject (readonly)

Returns the value of attribute recipe_name.



29
30
31
# File 'lib/chef/resource_builder.rb', line 29

def recipe_name
  @recipe_name
end

#resourceObject (readonly)

Returns the value of attribute resource.



31
32
33
# File 'lib/chef/resource_builder.rb', line 31

def resource
  @resource
end

#run_contextObject (readonly)

Returns the value of attribute run_context.



27
28
29
# File 'lib/chef/resource_builder.rb', line 27

def run_context
  @run_context
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/chef/resource_builder.rb', line 23

def type
  @type
end

Instance Method Details

#build(&block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chef/resource_builder.rb', line 47

def build(&block)
  @resource = resource_class.new(name, run_context)
  if resource.resource_name.nil?
    raise Chef::Exceptions::InvalidResourceSpecification, "#{resource}.resource_name is `nil`!  Did you forget to put `provides :blah` or `resource_name :blah` in your resource class?"
  end

  resource.source_line = created_at
  resource.declared_type = type

  resource.cookbook_name = cookbook_name
  resource.recipe_name = recipe_name
  # Determine whether this resource is being created in the context of an enclosing Provider
  resource.enclosing_provider = enclosing_provider

  # XXX: this is required for definition params inside of the scope of a
  # subresource to work correctly.
  resource.params = params

  # Evaluate resource attribute DSL
  if block_given?
    resource.resource_initializing = true
    begin
      if new_resource.nil?
        resource.instance_exec(&block)
      else
        resource.instance_exec(new_resource, &block)
      end
    ensure
      resource.resource_initializing = false
    end
  end

  # Run optional resource hook
  resource.after_created

  # Force to compile_time execution if the flag is set
  if resource.compile_time
    Array(resource.action).each do |action|
      resource.run_action(action)
    end
    resource.action :nothing
  end

  resource
end