Class: String

Inherits:
Object show all
Includes:
S3String
Defined in:
lib/poolparty/lite.rb,
lib/poolparty/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

#/(o) ⇒ String

Returns The original path concatenated with o.

Examples:

"merb"/"core_ext" #=> "merb/core_ext"

Parameters:

  • o (String)

    The path component to join with the string.

Returns:

  • (String)

    The original path concatenated with o.



195
196
197
# File 'lib/poolparty/core/string.rb', line 195

def /(o)
  File.join(self, o.to_s)
end

#^(h = {}) ⇒ Object



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

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

#arrayableObject



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

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

#camelcaseObject



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

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

#camelizeObject



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

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/poolparty/core/string.rb', line 63

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



112
113
114
# File 'lib/poolparty/core/string.rb', line 112

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

#collect_each_line_with_index(&block) ⇒ Object



157
158
159
160
161
# File 'lib/poolparty/core/string.rb', line 157

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


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

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.match(/-(\d+-\d+-\d+-\d+)\./) ? $1.gsub(/-/, '.') : self
end

#dasherizeObject

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



108
109
110
# File 'lib/poolparty/core/string.rb', line 108

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

#dir_safeObject



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

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

#grab_filename_from_caller_traceObject



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

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

#hasherize(format = []) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/poolparty/core/string.rb', line 6

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

#json_parseObject

Parse json string to a ruby object



200
201
202
203
204
205
206
# File 'lib/poolparty/core/string.rb', line 200

def json_parse
  return nil if self == "null" || self.empty?
  result = JSON.parse self
  if result.respond_to? :keys
    result.symbolize_keys!
  end
end

#keyerizeObject



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

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



184
185
186
# File 'lib/poolparty/core/string.rb', line 184

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

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



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

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



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/poolparty/core/string.rb', line 78

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



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

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

#path_quoteObject

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



54
55
56
# File 'lib/poolparty/core/string.rb', line 54

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

#pluralize(count = 2) ⇒ Object

dumb and ugly pluralize



209
210
211
# File 'lib/poolparty/core/string.rb', line 209

def pluralize(count=2)
  count > 1 ? self + "s" : self 
end

#preserved_class_constant(append = "") ⇒ Object



140
141
142
143
# File 'lib/poolparty/core/string.rb', line 140

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



151
152
153
154
155
156
# File 'lib/poolparty/core/string.rb', line 151

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



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

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

#safe_quoteObject



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

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

#sanitizeObject



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

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

#snake_caseObject

“FooBar”.snake_case #=> “foo_bar”



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

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"}


169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/poolparty/core/string.rb', line 169

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



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

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

#underscoreObject



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

def underscore
  snake_case
end