Class: PoolParty::Resources::Resource

Inherits:
PoolPartyBaseClass show all
Includes:
DependencyResolverResourceExtensions, PoolParty::ResourcingDsl, SearchablePaths
Defined in:
lib/poolparty/poolparty/resource.rb

Instance Attribute Summary

Attributes inherited from PoolPartyBaseClass

#init_opts, #ordered_resources

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SearchablePaths

included

Methods included from PoolParty::ResourcingDsl

#absent, #client_template_exists?, #client_templates_directory_exists?, #ensures, #get_client_or_gem_template, #is_absent, #is_present, #present

Methods included from DependencyResolverResourceExtensions

#to_properties_hash

Methods inherited from PoolPartyBaseClass

add_has_and_does_not_have_methods_for, #add_resource, add_resource_lookup_method, #add_service, #add_to_parent_if_parent_exists_and_is_a_service, #get_local_resource, #get_name_from_options_and_extra_options, #get_resource, #handle_option_values, #in_local_resources?, #inspect, #is_plugin?, #plugin_store, #resource, #resources, #run_in_context, #run_with_callbacks, #search_in_known_locations, #store_in_local_resources, #to_hash, #to_json

Methods included from DependencyResolverCloudExtensions

#to_properties_hash

Constructor Details

#initialize(opts = {}, extra_opts = {}, &block) ⇒ Resource

This is set in order of descending precedence The options are overwritten from the bottom up and the resource will use those as the values Then it takes the value of the block and sets whatever is sent there as the options Finally, it uses the parent’s options as the lowest priority



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/poolparty/poolparty/resource.rb', line 86

def initialize(opts={}, extra_opts={}, &block)
  super(opts, extra_opts, &block)
  
  @resource_name = @base_name
  # dsl_options[:name] = @init_opts[:name] unless dsl_options.has_key?(:name) && dsl_options[:name]
  
  # p [:name, name, dsl_options, init_opts] # methods.sort.join(", ")
  loaded(init_opts, &block)
  
  after_create
end

Class Method Details

.available_resourcesObject

Keep track of the resources that are available. This way we can show some pretty output later and ensure that we are only calling available resources



69
70
71
# File 'lib/poolparty/poolparty/resource.rb', line 69

def self.available_resources
  @available_resources ||= []
end

.inherited(subclass) ⇒ Object

When we subclass Resource, we want to add a few methods to the Resources class This will anable us to call out to these resources in our DSLified manner When we call a method from the subclass, say it’s the File class then we want to be able to have the method file() available. We also want to be able to fetch the resource with a get_file method. This will just call out to get the resource. If the resource isn’t available in a resource store, we expect to return a nil result. Finally, the has_ and does_not_have_ methods are appended. See below for those methods. Then we make sure we add these resources as available_resources onto the class so we know it’s available as a resource



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/poolparty/poolparty/resource.rb', line 39

def self.inherited(subclass)
  original_subclass = subclass
  subclass = subclass.to_s.split("::")[-1] if subclass.to_s.index("::")
  lowercase_class_name = subclass.to_s.underscore.downcase || subclass.downcase
  method_name = "__#{lowercase_class_name}".to_sym
  
  # Add add resource method to the Resources module
  unless PoolParty::PoolPartyBaseClass.respond_to?(method_name)
    method =<<-EOE
      private 
      def #{method_name}(opts={}, &blk)              
        add_resource(:#{lowercase_class_name}, opts, &blk)
      end
      def get_#{lowercase_class_name}(n, opts={}, &block)
        res = get_resource(:#{lowercase_class_name}, n, opts, &block)
        raise PackageException.new("Oops. Check that you specified the #{lowercase_class_name} \#\{n\}.") unless res
        res
      end
      public
    EOE
    PoolParty::PoolPartyBaseClass.module_eval method
    PoolParty::PoolPartyBaseClass.add_has_and_does_not_have_methods_for(lowercase_class_name.to_sym)
    PoolParty::PoolPartyBaseClass.add_resource_lookup_method(lowercase_class_name)
    
    available_resources << original_subclass
  end
end

Instance Method Details

#after_createObject

After create callback



109
110
# File 'lib/poolparty/poolparty/resource.rb', line 109

def after_create
end

#after_load(o = {}, &block) ⇒ Object



113
114
# File 'lib/poolparty/poolparty/resource.rb', line 113

def after_load(o={}, &block)
end

#before_load(o = {}, &block) ⇒ Object



111
112
# File 'lib/poolparty/poolparty/resource.rb', line 111

def before_load(o={}, &block)
end

#class_name_symObject

Private method just for resource retrievling purposes



153
154
155
# File 'lib/poolparty/poolparty/resource.rb', line 153

def class_name_sym
  self.class.to_s.top_level_class.downcase.to_sym
end

#class_type_nameObject

This way we can subclass resources without worry



135
136
137
# File 'lib/poolparty/poolparty/resource.rb', line 135

def class_type_name
  self.class.to_s.top_level_class.underscore.downcase
end

#cloudObject



121
122
123
124
125
126
# File 'lib/poolparty/poolparty/resource.rb', line 121

def cloud
  2.upto(context_stack.size) do |i|
    return ::PoolParty.context_stack[-i] if ::PoolParty.context_stack[-i].is_a?(PoolParty::Cloud::Cloud)
  end
  nil
end

#duplicatable?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/poolparty/poolparty/resource.rb', line 128

def duplicatable?
  false
end

#get_modified_optionsObject

We want to gather the options, but if the option sent is nil then we want to override the option value by sending the key as a method so that we can override this if necessary. Only runs on objects that have options defined, otherwise it returns an empty hash



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/poolparty/poolparty/resource.rb', line 161

def get_modified_options
  unless @modified_options
    if dsl_options
      opts = dsl_options.inject({}) do |sum,h|
        sum.merge!({h[0].to_sym => ((h[1].nil?) ? self.send(h[0].to_sym) : h[1]) })
      end
    else
      opts = {}
    end
    @full_allowed_options ||= allowed_options.reject {|ele| disallowed_options.include?(ele) }
    @modified_options = opts.reject do |k,v|
      !@full_allowed_options.include?(k) || 
        @parent && @parent.respond_to?(:dsl_options) && @parent != self && @parent.dsl_options.has_key?(k) && @parent.dsl_options[k] == dsl_options[k]
    end
  end
  @modified_options
end

#is_a_resource?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/poolparty/poolparty/resource.rb', line 148

def is_a_resource?
  true
end

#is_in_plugin?Boolean

def virtual_resource?

false

end def printable?

true

end

Returns:

  • (Boolean)


144
145
146
# File 'lib/poolparty/poolparty/resource.rb', line 144

def is_in_plugin?
  parent.is_plugin?
end

#loaded(opts = {}, &block) ⇒ Object

Stub, so you can create virtual resources This is called after the resource is initialized with the options given to it in the init-block



101
102
# File 'lib/poolparty/poolparty/resource.rb', line 101

def loaded(opts={}, &block)
end

#resource?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/poolparty/poolparty/resource.rb', line 131

def resource?
  true
end

#resource_nameObject



104
105
106
# File 'lib/poolparty/poolparty/resource.rb', line 104

def resource_name
  @resource_name ||= nil
end

#servicesObject

We don’t want to inherit the services on a resource, as resources are a base and should never have services.



118
119
# File 'lib/poolparty/poolparty/resource.rb', line 118

def services
end