Class: String

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

Direct Known Subclasses

Aska::Rule

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

#_allowedObject



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

def _allowed
  /[ \$\._\*\-\[\]\n\t\\\/&,\(\)"',|:=\>\<A-Za-z0-9]/
end

#_grab_key_value_for(type, name, key) ⇒ Object



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

def _grab_key_value_for(type, name, key)
  grab_entry_for(type, name).scan(/#{key}[ =>]*(.*)[,?]?$/).flatten.first rescue ""
end

#arrayableObject



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

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

#camelcaseObject



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

def camelcase
  gsub(/(^|_|-)(.)/) { $2.upcase }
end

#camelizeObject



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

def camelize
  camelcase
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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/poolparty/core/string.rb', line 55

def class_constant(superclass=nil, opts={}, &block)
  symc = ((opts && opts[:preserve]) ? ("#{self.camelcase}Class") : "PoolParty#{self.camelcase}Class").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

#classifyObject



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

def classify
  self.sub(/.*\./, '').camelcase
end

#collect_each_line_with_index(&block) ⇒ Object



149
150
151
152
153
# File 'lib/poolparty/core/string.rb', line 149

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

#constantize(mod = Object) ⇒ Object

Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.

Examples

"Module".constantize #=> Module
"Class".constantize #=> Class


122
123
124
125
126
127
128
129
130
# File 'lib/poolparty/core/string.rb', line 122

def constantize(mod=Object)
  camelcased_word = camelcase
  begin
    mod.module_eval(camelcased_word, __FILE__, __LINE__)
  rescue NameError
    puts "#{camelcased_word} is not defined."
    nil
  end
end

#convert_from_ec2_to_ipObject



27
28
29
# File 'lib/poolparty/net/remoter_bases/ec2/ec2.rb', line 27

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

#dasherizeObject

“FooBar”.dasherize #=> “foo-bar”



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

def dasherize
  gsub(/\B[A-Z]+/, '-\&').downcase
end

#dir_safeObject



42
43
44
# File 'lib/poolparty/core/string.rb', line 42

def dir_safe
  self.downcase.gsub(/[ ]/, '_')
end

#grab_entry_for(type, name) ⇒ Object



2
3
4
5
6
7
8
# File 'lib/poolparty/spec/core/string.rb', line 2

def grab_entry_for(type, name)
  begin      
    matches = self.match(/#{type}(.*)\{(.*)"#{name}":(.*)(#{_allowed})*\}/)[0]
  rescue
    ""
  end
end

#grab_filename_from_caller_traceObject



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

def grab_filename_from_caller_trace
  self.gsub(/\.rb(.*)/, '.rb')
end

#grab_key_value_for(ty, name, key) ⇒ Object



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

def grab_key_value_for(ty, name, key)
  _grab_key_value_for(ty,name,key).gsub(/,/, '') rescue ""
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

#keyerizeObject



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

def keyerize
  signed_short = 0x7FFFFFFF
  len = self.sanitize.length
  hash = 0 
  len.times{ |i| 
    hash = self[i] + ( hash << 6 ) + ( hash << 16 ) - hash 
  } 
  hash & signed_short
end

#macifyObject

Take a mac address and split it to map to the arp -a response Just rip off the first 0 if the first char is 0



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

def macify
  split(":").map {|a| a[0].chr == "0" ? a[1].chr : a}.join(":")
end

#module_constant(append = "", &block) ⇒ Object



137
138
139
140
141
142
# File 'lib/poolparty/core/string.rb', line 137

def module_constant(append="", &block)
  symc = "#{self}_Module#{append}".camelcase
  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

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



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/poolparty/core/string.rb', line 70

def new_resource_class(superclass=nil, opts={}, &block)
  symc = "::PoolParty::Resources::#{self.camelcase}"
  kla=<<-EOE
    class #{symc} < ::PoolParty::Resources::Resource
    end
  EOE
  
  Kernel.module_eval kla
  klass = symc.constantize
  klass.module_eval &block if block

  klass
end

#nice_runnable(quite = true) ⇒ Object

self.gsub(//, “\"”)



49
50
51
# File 'lib/poolparty/core/string.rb', line 49

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

#parse_datetimeObject



30
31
32
# File 'lib/poolparty/net/remoter_bases/ec2/ec2.rb', line 30

def parse_datetime
  DateTime.parse( self.chomp ) rescue self
end

#preserved_class_constant(append = "") ⇒ Object



132
133
134
135
# File 'lib/poolparty/core/string.rb', line 132

def preserved_class_constant(append="")
  klass = "#{self}#{append}".classify
  Object.const_defined?(klass.to_sym) ? klass.to_s.constantize : nil
end

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



143
144
145
146
147
148
# File 'lib/poolparty/core/string.rb', line 143

def preserved_module_constant(ext="", from="PoolParty::", &block)
  symc = "#{self}#{ext}".camelcase
  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



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

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

#safe_quoteObject



45
46
47
48
# File 'lib/poolparty/core/string.rb', line 45

def safe_quote
  self.gsub(/['"]/, '\\\"')
  # self.gsub(/["']/, "\\\"")
end

#sanitizeObject



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

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

#snake_caseObject

“FooBar”.snake_case #=> “foo_bar”



92
93
94
# File 'lib/poolparty/core/string.rb', line 92

def snake_case
  gsub(/\B[A-Z]+/, '_\&').downcase
end

#to_hashObject

Read a new-line separated string and turn the string from the form

a = "b"
b = "c"

into a hash

{:a => "b", :b => "c"}


161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/poolparty/core/string.rb', line 161

def to_hash
  split("\n").inject({}) do |sum,line| 
    if line.include?("=")
      l = line.split("=").map {|a| a.strip }
      key = l[0].to_sym
      value = l[1].gsub(/\"/, '')
      sum.merge(key => value)
    else
      sum
    end
  end
end

#top_level_classObject



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

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

#underscoreObject



95
96
97
# File 'lib/poolparty/core/string.rb', line 95

def underscore
  snake_case
end