Module: General::GOperations

Defined in:
lib/goperations.rb

Overview

Created: 6 - 3 - 2016

Constant Summary collapse

MONEY_TYPES =

The money types used by the money operation

{
	"USD" => "$",
	"EUR" => ""
}
DEFAULT_TIME =

The default time format

"@I:@MM:@SS @A"

Class Method Summary collapse

Class Method Details

.capitalize(string = nil, what = 'first') ⇒ Object

Capitalizes every word in the string

Parameter: string - the string being capitalized

Return: the capitalized string



65
66
67
68
69
70
71
72
73
# File 'lib/goperations.rb', line 65

def self.capitalize string=nil, what='first'
	assert 'capitalize', string, String

	case what
	when 'all' then string.split(' ').collect(&:capitalize).join(' ')
	when 'first' then string.capitalize
	else raise GOperationError.new "Undefined second argument for operation capitalize: #{what}"
	end
end

.lowercase(string = nil) ⇒ Object

Converts every letter in the string to lowercase

Parameter: string - the string being lowercased

Return: the lowercased string



90
91
92
93
# File 'lib/goperations.rb', line 90

def self.lowercase string=nil
	assert 'lowercase', string, String
	return string.downcase
end

.money(integer = nil, type = "USD") ⇒ Object

Returns the integer monetary value formatted to the given money type

Parameter: integer - the monetary amount being formatted Parameter: type - the type of money (defaults to USD)

Return: the formatted money amount



103
104
105
106
107
108
109
110
111
# File 'lib/goperations.rb', line 103

def self.money integer=nil, type="USD"
	assert 'money', integer, Integer

	if MONEY_TYPES[type]
		return (integer < 0 ? "-" : "") + MONEY_TYPES[type] + (integer * 0.01).abs.to_s
	else
		raise GOperationError.new "Money type: #{type} is not supported!"
	end
end

.split(string = nil, delimeter = "\r?\n") ⇒ Object

Splits the string by the given delimeter (or by newline if no delimeter is given)

Parameter: string - the string to split Parameter: delimeter - the delimeter to split by (defaults to newline)

Return: the array containing hashes representing the split string chunks



132
133
134
135
# File 'lib/goperations.rb', line 132

def self.split string=nil, delimeter="\r?\n"
	assert 'split', string, String
	return string.split(Regexp.new(delimeter))
end

.splitwords(string = nil, words = 10) ⇒ Object

Splits a sequence by a set number of words (defaults to 10)

Parameter: string - the string to split Parameter: words - the number of words to split by (defaults to 10)

Return: an array containing hashes representing the chunks of the string split by words



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/goperations.rb', line 143

def self.splitwords string=nil, words=10
	assert 'splitwords', string, String

	# Convert words to integer
	words = words.to_i

	# Regex to match words
	matcher = /\G[\w\',\.\?!\(\)\-\:\;\"\"]+\s*/

	# Buffers
	to_return = []
	buffer    = ""

	# Initialize loop
	matched = matcher.match string
	index   = 0

	# While matched data exists
	while matched
		# Push line to array and reset if number of words is passed
		if index % words == 0
			to_return << buffer.sub(/\s+$/, "")
			buffer = ""
		end

		# Append word to buffer
		buffer += matched.to_s

		# Trim word from string
		string = string[matched.end(0)..-1]

		# Iterate
		matched = matcher.match string
		index += 1
	end

	# Push final line to array and return
	to_return << buffer
	return to_return[1..-1] # Getting rid of the first blank line
end

.time(integer = nil, format = DEFAULT_TIME) ⇒ Object

Returns the integer time value (in seconds) formatted with the given formatter

Parameter: integer - the integer being formatted (representing the time in seconds) Parameter: format - the format being used (defaults to DEFAULT_TIME)

Return: the time formatted with the given formatter



119
120
121
122
# File 'lib/goperations.rb', line 119

def self.time integer=nil, format=DEFAULT_TIME
	assert 'time', integer, Integer
	return General::GTimeFormat.new(format).apply(integer)
end

.uppercase(string = nil) ⇒ Object

Converts every letter in the string to uppercase

Parameter: string - the string being uppercased

Return: the uppercased string



80
81
82
83
# File 'lib/goperations.rb', line 80

def self.uppercase string=nil
	assert 'uppercase', string, String
	return string.upcase
end