Class: Object

Inherits:
BasicObject
Includes:
PoolParty, PoolParty::Cloud, PoolParty::Console, PoolParty::DefinableResource, PoolParty::Pool
Defined in:
lib/poolparty/core/object.rb,
lib/poolparty/core/metaid.rb,
lib/poolparty/helpers/console.rb,
lib/poolparty/net/remote_bases.rb,
lib/poolparty/spec/spec/dynamic_matchers.rb,
lib/poolparty.rb

Overview

Basic, add an alias_method to the object class

Add returning to the object

Instance Method Summary collapse

Methods included from PoolParty::DefinableResource

#define_resource, #virtual_resource

Methods included from PoolParty::Cloud

#cloud, #clouds, #with_cloud

Methods included from PoolParty::Pool

#pool, #pool_specfile, #pools, #remove_pool, #reset!, #set_pool_specfile, #with_pool

Methods included from PoolParty

#case_of, #context_stack, #extract_cloud_from_options, #extract_pool_from_options, load_cloud_from_json, #load_pool, #log, #pack_user_directory, #pool_specfile, #print_with_nice_printer, require_directory, #require_user_directory, #reset!, #working_conditional

Methods included from PoolParty::Remote

#_nodes, #are_any_nodes_exceeding_minimum_runtime?, #are_too_few_instances_running?, #are_too_many_instances_running?, #commands, #execute!, #is_master_running?, #list_of_instances, #list_of_nodes_exceeding_minimum_runtime, #master, #netssh, #nodes, #remote_rsync_command, #remote_ssh_array, #remote_ssh_string, #rsync, #rsync_command, #rsync_storage_files_to, #rsync_storage_files_to_command, #rsync_to, #rsync_to_command, #run_command_on, #run_command_on_command, #run_command_on_instance_number, #run_local, #run_remote, #scp_array, #scp_to_command, #simplest_run_remote, #ssh_array, #ssh_command, #ssh_into, #ssh_into_instance_number, #ssh_options, #ssh_string, #target_host

Methods included from PoolParty::Pinger

included

Methods included from PoolParty::FileWriter

#cleanup_storage_directory, #clear_base_directory, #copy_directory_into_storage_directory, #copy_directory_into_template_storage_directory, #copy_file_to_storage_directory, #copy_template_to_storage_directory, #make_base_directory, #make_base_path, #make_directory_in_storage_directory, #make_template_directory, #write_to_file, #write_to_file_in_storage_directory, #write_to_temp_file

Methods included from PoolParty::Console

#help, #reload!

Methods included from PoolParty::Display

#header, #pool_describe, #subheader

Instance Method Details

#alias_method(new_id, original_id) ⇒ Object



15
16
17
18
# File 'lib/poolparty/core/object.rb', line 15

def alias_method(new_id, original_id)
  original = self.method(original_id).to_proc
  define_method(new_id){|*args| original.call(*args)}
end

#block_instance_eval(*args, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/poolparty/core/object.rb', line 50

def block_instance_eval(*args, &block)
  return instance_eval(*args,&block) unless block && !block.arity.zero?
  old_method = (self.class.instance_method(:__) rescue nil)
  self.class.send(:define_method, :__, &block)
  block_method = self.class.instance_method(:__)
  if old_method
    self.class.send(:define_method, :__, old_method)
  else
    self.class.send(:remove_method, :__)
  end
  block_method.bind(self).call(*args)
end

#class_def(name, &blk) ⇒ Object

Defines an instance method within a class



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

def class_def name, &blk
  class_eval { define_method name, &blk }
end

#debugging(bool = nil) ⇒ Object Also known as: debug



93
94
95
# File 'lib/poolparty/core/object.rb', line 93

def debugging(bool=nil)
  bool.nil? ? $DEBUGGING : $DEBUGGING = bool
end

#debugging?(o = self) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/poolparty/core/object.rb', line 90

def debugging?(o=self)
  o.respond_to?(:debug) ? o.debug : ($DEBUGGING ||= false)
end

#dprint(m = "", o = self) ⇒ Object



84
85
86
# File 'lib/poolparty/core/object.rb', line 84

def dprint(m="", o=self)
  print "#{m}" if debugging?(o) || verbose?(o) rescue ""
end

#dputs(m = "", o = self) ⇒ Object



81
82
83
# File 'lib/poolparty/core/object.rb', line 81

def dputs(m="", o=self)
  puts "[DEBUG] -- #{m.inspect}" if debugging?(o) rescue ""
end

#dynamic_matcher_for(ty, &block) ⇒ Object

Create a dynamic matcher Create a matcher for spec based in the context called from Usage example:

dynamic_matcher_for(:virtualhost) do
  set_description "should have virtualhost"
  it "should have the directory /var/www" do        
    have_directory("/var/www")
  end
end

This creates a method by the name of the dynamic matcher so in the above example for virtualhost, the method have_virtualhost(name) &block is created in the spec context and can be called from within the describe/context in a spec



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/poolparty/spec/spec/dynamic_matchers.rb', line 32

def dynamic_matcher_for(ty, &block)
  name = "#{ty}".camelcase
  
  begin
    const = Object.const_set(name, Spec::Example::SharedExampleGroup.register(name.to_sym, &block))
  rescue NameError => e
    raise NameError.new(e.message + "\nThe first argument to share_as must be a legal name for a constant\n")
  end
  
  described_type = "#{ty.to_s.underscore.downcase}"    
  
  ::Spec::Example::ExampleGroupMethods.module_eval <<-EOM
    def have_#{ty.to_s.underscore.downcase}(*args)        
      include #{const}
      self.module_eval "@@described_type = args;def #{described_type}; @@described_type; end"
    end
  EOM
end

#extended(&block) ⇒ Object



28
29
30
31
# File 'lib/poolparty/core/object.rb', line 28

def extended(&block)
  block.in_context(self).call
  self
end

#make_dynamic_matcher_string_for(ty, matcher = "is_present?") ⇒ Object

Load the have_base file and fill in the variables for the have_base base template



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/poolparty/spec/spec/dynamic_matchers.rb', line 52

def make_dynamic_matcher_string_for(ty, matcher="is_present?")
  @basestr ||= open("#{::File.dirname(__FILE__)}/../templates/have_base.rb").read
  typestring = ty.to_s
  begin
    @basestr ^ {:classname => "Have#{typestring.capitalize}",
                :type => typestring,
                :matches => matcher,
                :includer => "SpecExtensions::Have#{typestring.capitalize}.new(name, extra)"}
  rescue Exception => e
  end
end

#meta_def(name, &blk) ⇒ Object

Adds methods to a metaclass



7
8
9
# File 'lib/poolparty/core/metaid.rb', line 7

def meta_def name, &blk
  meta_eval { define_method name, &blk }
end

#meta_eval(&blk) ⇒ Object



4
# File 'lib/poolparty/core/metaid.rb', line 4

def meta_eval &blk; metaclass.instance_eval &blk; end

#meta_undef(name) ⇒ Object



65
66
67
# File 'lib/poolparty/core/object.rb', line 65

def meta_undef name
  meta_eval { remove_method name }
end

#metaclassObject

The hidden singleton lurks behind everyone



3
# File 'lib/poolparty/core/metaid.rb', line 3

def metaclass; class << self; self; end; end

#my_methodsObject



6
7
8
# File 'lib/poolparty/core/object.rb', line 6

def my_methods
  self.methods.sort - (self.class.methods + self.class.superclass.methods)
end

#register_remote_base(*args) ⇒ Object

Register the remoter base in the remote_bases global store



8
9
10
11
12
13
# File 'lib/poolparty/net/remote_bases.rb', line 8

def register_remote_base(*args)
  args.each do |arg|
    base_name = "#{arg}".downcase.to_sym
    (remote_bases << base_name) unless remote_bases.include?(base_name)
  end
end

#remote_basesObject Also known as: available_bases



4
5
6
# File 'lib/poolparty/net/remote_bases.rb', line 4

def remote_bases
  $remote_bases ||= []
end

#respec_stringObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/poolparty/core/object.rb', line 40

def respec_string
  case self.class
  when String
    self.to_option_string
  when Array
    self.map {|a| "#{a.respec_string}" }.join(" ")
  else
    "'#{self}'"
  end
end

#returning(receiver) {|receiver| ... } ⇒ Object

Yields:

  • (receiver)


24
25
26
27
# File 'lib/poolparty/core/object.rb', line 24

def returning(receiver)
  yield receiver
  receiver
end

#send_if_method(v, *args) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/poolparty/core/object.rb', line 32

def send_if_method(v, *args)
  if (v.nil? || v.to_s.empty? || v.is_a?(Array) || v.is_a?(Integer))# && !v.is_a?(Symbol))#)v.is_a?(String)
    v
  else
    vs = v.to_s.to_sym
    respond_to?(vs) ? self.send(vs, *args) : v rescue v  #NOTE MF: maybe we should not rescue all errors?
  end
end

#testing(bool = $TESTING) ⇒ Object



96
97
98
# File 'lib/poolparty/core/object.rb', line 96

def testing(bool=$TESTING)
  bool.nil? ? $TESTING : $TESTING = bool
end

#thisObject



9
10
11
# File 'lib/poolparty/core/object.rb', line 9

def this
  self
end

#to_osObject



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

def to_os
  self
end

#unix_hide_stringObject



100
101
102
# File 'lib/poolparty/core/object.rb', line 100

def unix_hide_string
  "2>&1 > /dev/null"
end

#verbose?(o = self) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/poolparty/core/object.rb', line 87

def verbose?(o=self)
  o.respond_to?(:verbose) ? o.verbose : ($TESTING ||= false)
end

#vprint(m = "", o = self) ⇒ Object



78
79
80
# File 'lib/poolparty/core/object.rb', line 78

def vprint(m="", o=self)
  print m if o.verbose rescue ""
end

#vputs(m = "", o = self) ⇒ Object

def run_in_context(context=self, &block)

name="temp_#{self.class}_#{respond_to?(:parent) ? parent.to_s : Time.now.to_i}".to_sym
meta_def name, &block
self.send name, context
meta_undef name rescue ""

end



74
75
76
77
# File 'lib/poolparty/core/object.rb', line 74

def vputs(m="", o=self)
  # puts m if o.verbose rescue ""
  puts "[INFO] -- #{m}" if verbose?
end

#with_options(opts = {}, par = nil, &block) ⇒ Object



19
20
21
22
23
# File 'lib/poolparty/core/object.rb', line 19

def with_options(opts={}, par=nil, &block)
  @p = par.clone
  @p.options.merge!(opts)
  @p.instance_eval &block if block
end

#write_dynamic_matchersObject

For all of the available_resources, write the matcher template based on the have_base base and write the method have_ for each of the resources available



8
9
10
11
12
13
14
15
16
17
# File 'lib/poolparty/spec/spec/dynamic_matchers.rb', line 8

def write_dynamic_matchers
  PoolParty::Resources::Resource.available_resources.each do |ty|
    ty.downcase!
    filename = "#{::File.dirname(__FILE__)}/../matchers/have_#{ty}.rb"
    unless ::File.file?(filename)
      str = make_dynamic_matcher_string_for(ty)
      ::File.open(filename, "w+") {|f| f << str}
    end
  end
end