Class: Chef::Sugar::Filters::Injector

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

Overview

A top-level class for manipulation the resource collection.

Instance Method Summary collapse

Constructor Details

#initialize(recipe, identifier, placement) ⇒ Injector

Returns a new instance of Injector.



36
37
38
39
40
41
# File 'lib/chef/sugar/filters.rb', line 36

def initialize(recipe, identifier, placement)
  @recipe              = recipe
  @resource_collection = @recipe.run_context.resource_collection
  @resource            = @resource_collection.lookup(identifier)
  @placement           = placement
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/sugar/filters.rb', line 71

def method_missing(m, *args, &block)
  new_resource = @recipe.send(m, *args, &block)

  case @placement
  when :before
    insert_before(@resource, new_resource)
  when :after
    insert_after(@resource, new_resource)
  else
    super
  end
end

Instance Method Details

#evaluate(&block) ⇒ Object



43
44
45
# File 'lib/chef/sugar/filters.rb', line 43

def evaluate(&block)
  instance_eval(&block)
end

#insert_after(resource, new_resource) ⇒ Object



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

def insert_after(resource, new_resource)
  @resource_collection.instance_eval do
    # Remove the resource because it's automatically created
    @resources.delete_at(@resources_by_name[new_resource.to_s])
    @resources_by_name.delete(new_resource.to_s)

    index = @resources_by_name[resource.to_s] + 2
    @resources.insert(index, new_resource)
    @resources_by_name[new_resource.to_s] = index
  end
end

#insert_before(resource, new_resource) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef/sugar/filters.rb', line 47

def insert_before(resource, new_resource)
  @resource_collection.instance_eval do
    # Remove the resource because it's automatically created
    @resources.delete_at(@resources_by_name[new_resource.to_s])
    @resources_by_name.delete(new_resource.to_s)

    index = @resources_by_name[resource.to_s]
    @resources.insert(index, new_resource)
    @resources_by_name[new_resource.to_s] = index
  end
end