Class: String

Inherits:
Object show all
Includes:
S3String
Defined in:
lib/poolparty/core/string.rb,
lib/poolparty/modules/s3_string.rb,
lib/poolparty/net/remote_bases/ec2.rb

Instance Method Summary collapse

Methods included from S3String

#bucket_exists?, #bucket_object, #bucket_object_exists?, #bucket_objects, #delete_bucket, #delete_bucket_value, #store_bucket_value

Instance Method Details

#^(h = {}) ⇒ Object



14
15
16
# File 'lib/poolparty/core/string.rb', line 14

def ^(h={})
  self.gsub(/:([\w]+)/) {h[$1.to_sym] if h.include?($1.to_sym)}
end

#arrayable ⇒ Object



17
18
19
# File 'lib/poolparty/core/string.rb', line 17

def arrayable
  self.strip.split(/\n/)
end

#class_constant(superclass = nil, opts = {}, &block) ⇒ Object

Refactor this guy to get the class if the class is defined, and not always create a new one although, it doesn't really matter as ruby will just reopen the class



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/poolparty/core/string.rb', line 40

def class_constant(superclass=nil, opts={}, &block)
  symc = ((opts && opts[:preserve]) ? ("#{self.classify}Classs") : "PoolParty#{self.classify}Classs").classify
      
  kla=<<-EOE
    class #{symc} #{"< #{superclass}" if superclass}
    end
  EOE
  
  Kernel.module_eval kla
  klass = symc.constantize
  klass.module_eval &block if block
  
  klass
end

#collect_each_line_with_index(&block) ⇒ Object



67
68
69
70
71
# File 'lib/poolparty/core/string.rb', line 67

def collect_each_line_with_index(&block)
  returning [] do |arr|
    arr << self.split(/\n/).collect_with_index(&block)
  end.flatten
end

#convert_from_ec2_to_ip ⇒ Object



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

def convert_from_ec2_to_ip
  self.gsub(/.compute-1.amazonaws.com*/, '').gsub(/ec2-/, '').gsub(/-/, '.')
end

#hasherize(format = []) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/poolparty/core/string.rb', line 2

def hasherize(format=[])
  hash = {}
  i = 0
  self.split(%r{[\n|\t|\s| ]+}).map {|a| a.strip}.each do |f|
    next unless format[i]
    unless f == "" || f.nil?
      hash[format[i].to_sym] = f
      i+=1
    end      
  end
  hash
end

#module_constant(&block) ⇒ Object



55
56
57
58
59
60
# File 'lib/poolparty/core/string.rb', line 55

def module_constant(&block)
  symc = "#{self}_Module".classify
  mod = Object.const_defined?(symc) ? Object.const_get(symc.to_sym) : Module.new(&block)
  Object.const_set(symc, mod) unless Object.const_defined?(symc)
  symc.to_s.constantize
end

#nice_runnable(quite = true) ⇒ Object



30
31
32
# File 'lib/poolparty/core/string.rb', line 30

def nice_runnable(quite=true)
  self.split(/ && /).join("\n")
end

#preserved_module_constant(ext = "", from = "PoolParty::", &block) ⇒ Object



61
62
63
64
65
66
# File 'lib/poolparty/core/string.rb', line 61

def preserved_module_constant(ext="", from="PoolParty::", &block)
  symc = "#{self}#{ext}".classify
  mod = Kernel.const_defined?(symc) ? Kernel.const_get(symc.to_sym) : Module.new(&block)
  Kernel.const_set(symc, mod) unless Kernel.const_defined?(symc)
  symc.to_s.constantize
end

#runnable(quite = true) ⇒ Object



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

def runnable(quite=true)
  # map {|l| l << "#{" >/dev/null 2>/dev/null" if quite}" }.
  self.strip.split(/\n/).join(" && ")
end

#sanitize ⇒ Object



27
28
29
# File 'lib/poolparty/core/string.rb', line 27

def sanitize
  self.gsub(/[\.]*/, '')
end

#to_option_string(ns = []) ⇒ Object



33
34
35
36
37
# File 'lib/poolparty/core/string.rb', line 33

def to_option_string(ns=[])
  a_template = (self =~ /template/) == 0
  a_service = self =~ /^[A-Z][a-zA-Z]*\[[a-zA-Z0-9\-\.\"\'_\$\{\}\/]*\]/
  (a_service || a_template) ? "#{self}" : "'#{self}'"
end

#top_level_class ⇒ Object



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

def top_level_class
  self.split("::")[-1].downcase rescue self
end