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.



30
31
32
33
34
35
# File 'lib/chef/sugar/filters.rb', line 30

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/chef/sugar/filters.rb', line 65

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



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

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

#insert_after(resource, new_resource) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chef/sugar/filters.rb', line 53

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



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chef/sugar/filters.rb', line 41

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