Class: PoolParty::Base

Inherits:
Object show all
Includes:
Callbacks, Delayed, Dslify, Parenting, SearchablePaths
Defined in:
lib/poolparty/base.rb

Direct Known Subclasses

DslBase, Resource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delayed

included

Methods included from Callbacks

included

Methods included from SearchablePaths

included

Constructor Details

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

Returns a new instance of Base.



21
22
23
24
25
26
27
# File 'lib/poolparty/base.rb', line 21

def initialize(opts={}, extra_opts={}, &block)
  @init_block = block
  @init_opts = compile_opts(opts, extra_opts)
  
  run_with_callbacks(init_opts, &block)
  @base_name = self.name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

If the method is missing from ourself, check the Default class for the corresponding method. If the Default class has the method, inquire from the Default class, otherwise, call super



212
213
214
215
216
217
218
# File 'lib/poolparty/base.rb', line 212

def method_missing(m,*a,&block)
  if Default.respond_to?(m)
    Default.send(m,*a,&block)
  else
    super
  end
end

Instance Attribute Details

#base_nameObject (readonly)

Returns the value of attribute base_name.



12
13
14
# File 'lib/poolparty/base.rb', line 12

def base_name
  @base_name
end

#init_optsObject (readonly)

Returns the value of attribute init_opts.



12
13
14
# File 'lib/poolparty/base.rb', line 12

def init_opts
  @init_opts
end

Class Method Details

.clouds_dot_rb_dir(n = nil) ⇒ Object



206
# File 'lib/poolparty/base.rb', line 206

def self.clouds_dot_rb_dir(n=nil); Pool.clouds_dot_rb_dir(n);end

.clouds_dot_rb_file(n = nil) ⇒ Object



204
# File 'lib/poolparty/base.rb', line 204

def self.clouds_dot_rb_file(n=nil);Pool.clouds_dot_rb_file(n);end

Instance Method Details

#add_ordered_resources_to_result(resources, result) ⇒ Object

Add all the resources as edges of each other



141
142
143
144
145
146
147
148
# File 'lib/poolparty/base.rb', line 141

def add_ordered_resources_to_result(resources, result)
  arr_of_resources = resources.zip_offset(1)
  
  arr_of_resources.each do |first, second|
    result.add_edge!(first, second) unless result.edge?(first, second) or result.edge?(second, first)
    add_ordered_resources_to_result(first.resources, result)
  end
end

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

after_loaded This is run immediately after the block given on init is yielded



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

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

#all_resourcesObject



180
181
182
183
184
# File 'lib/poolparty/base.rb', line 180

def all_resources
  resources.map do |res|
    [res, res.all_resources ]
  end.flatten
end

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

Base callbacks for run_with_callbacks before_load This is called before the block is yielded



63
64
# File 'lib/poolparty/base.rb', line 63

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

#clouds_dot_rb_dir(n = nil) ⇒ Object



205
# File 'lib/poolparty/base.rb', line 205

def clouds_dot_rb_dir(n=nil); self.class.clouds_dot_rb_dir(n);end

#clouds_dot_rb_file(n = nil) ⇒ Object

The clouds.rb file



203
# File 'lib/poolparty/base.rb', line 203

def clouds_dot_rb_file(n=nil); self.class.clouds_dot_rb_file(n); end

#compile_opts(o = {}, extra = {}) ⇒ Object

Try to extract the name from the options Either the first parameter is a string or it is a hash if it is a hash, just merge the two hashes If it is a string, then merge it in as the name of the instance on the hash



77
78
79
80
81
82
83
84
85
86
# File 'lib/poolparty/base.rb', line 77

def compile_opts(o={}, extra={})
  case o
  when Symbol, String
    extra.merge(:name => o.to_s)
  when NilClass
    extra
  else
    extra.merge(o)
  end
end

#create_graph(resources, on, result) ⇒ Object

Create the graph of resources. Blow up if a resource isn’t found that is required. If it is found, add it as an edge to the dependency graph



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/poolparty/base.rb', line 153

def create_graph(resources, on, result)
  resources.each_with_index do |resource, idx|
    
    resource.dependencies.each do |dep_type, deps_array|
      deps_array.each do |dep_name|
        dep = get_resource(dep_type, dep_name)
        raise PoolPartyError.create("ResourceNotFound", "A resource required for #{resource.has_method_name}(#{resource.name}) was not found: #{dep_type}(#{dep_name}). Please make sure you've specified this in your configuration.") unless dep
        result.add_edge!(dep, resource, dep.name) unless result.edge?(dep, resource) or result.edge?(resource, dep)
      end
    end
    
    if on
      result.add_edge!(resource, on, resource.name) unless result.edge?(resource, on) or result.edge?(on, resource)
    else
      result.add_vertex!(resource)
    end
    
    create_graph(resource.resources, resource, result)
  end
end

#dependenciesObject

All the dependencies that are required by this resource This is a hash of the dependencies required by the resource



176
177
178
# File 'lib/poolparty/base.rb', line 176

def dependencies
  @dependencies ||= {}
end

#get_resource(ty, nm) ⇒ Object

Get a resource, based on it’s type Used for INTERNAL use



116
117
118
119
# File 'lib/poolparty/base.rb', line 116

def get_resource(ty, nm)
  o = self.send "#{ty}s".to_sym
  o.detect {|r| r.name == nm }
end

#has_searchable_paths(opts = {}) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/poolparty/base.rb', line 220

def has_searchable_paths(opts={})
  default_searchable_paths = [
    Dir.pwd,
    PoolParty::Default.poolparty_home_path,
    PoolParty::Default.poolparty_src_path,
    PoolParty::Default.poolparty_src_path/:lib/:poolparty,
    PoolParty::Default.base_config_directory,
    PoolParty::Default.remote_storage_path
  ]
  super(opts.merge(:paths => (opts.delete(:paths) || default_searchable_paths)))
end

#ordered_resourcesObject

Order the resources_graph using a top-sort iterator



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

def ordered_resources
  resources_graph.topsort.to_a
end

#output_resources_graph(fmt = 'png', dotfile = name, params = {}) ⇒ Object

Write the cloud dependency graph



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/poolparty/base.rb', line 187

def output_resources_graph(fmt='png', dotfile=name, params={})
    p = {
      'bgcolor' => 'white',
      'label'   => "#{name}",
      'pad'     => '0.5',
      'rankdir' => 'LR',
      'overlap' => 'false',
      'node_params' => {
        'color' => '"#111111"',
        'style' => "rounded"
      }
    }.merge(params)
    resources_graph.write_to_graphic_file(fmt, dotfile, p)
end

#resourcesObject

Resources are all the resources attached to this resource



105
106
107
# File 'lib/poolparty/base.rb', line 105

def resources
  @resources ||= []
end

#resources_graph(force = false) ⇒ Object

Create a directed adjacency graph of each of the dependencies Add all the resources in order and their resources as well and then create the graph from the rest of the resources to create edges on the graph



129
130
131
132
133
134
135
136
137
138
# File 'lib/poolparty/base.rb', line 129

def resources_graph(force=false)
  return @resources_graph if @resources_graph && !force
  result = Digraph.new      
  
  create_graph(resources, nil, result)
  
  add_ordered_resources_to_result(resources, result)
  
  @resources_graph = result
end

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

Overloading the parent run_in_context First, push ourself to the stack then set all the variables given with the init and then eval the block and pop ourselves off the stack During runtime, the stack looks like

  • self

    • instance_eval block

  • stack



38
39
40
41
42
43
44
45
# File 'lib/poolparty/base.rb', line 38

def run_in_context(o={}, &block)
  proc = Proc.new do
    set_vars_from_options(:name => o[:name]) if o.has_key?(:name) # Name MUST be set first
    set_vars_from_options(o)
    instance_eval &block if block
  end
  super(&proc)
end

#run_with_callbacks(o, &block) ⇒ Object

Run the block in the context of self and call the block after calling

before_load

and afterwards calling

after_loaded


52
53
54
55
56
57
58
# File 'lib/poolparty/base.rb', line 52

def run_with_callbacks(o, &block)
  run_in_context(o) do
    callback :before_load, o, &block
    instance_eval &block if block
    callback :after_loaded, o, &block
  end
end

#to_sObject



121
122
123
# File 'lib/poolparty/base.rb', line 121

def to_s
  "#{self.respond_to?(:has_method_name) ? self.has_method_name : self.class.to_s.top_level_class}:#{name}"
end

#valid?Boolean

Validation checks if all of the validations pass, the object is considered valid the validations are responsible for raising a PoolPartyError (StandardError)

Returns:

  • (Boolean)


91
92
93
# File 'lib/poolparty/base.rb', line 91

def valid?
  validations.each {|validation| self.send(validation.to_sym) }
end

#validationsObject

The array of validations that the instances must pass to be considered valid. These must be methods available on the instance



97
98
99
# File 'lib/poolparty/base.rb', line 97

def validations
  []
end