Module: Croesus::Utils
- Included in:
- Croesus, ModFactory, WebRequest, WebResponse
- Defined in:
- lib/croesus/utils.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#callable(call_her) ⇒ Object
module ClassMethods.
- #caller_name ⇒ Object
- #camelize(underscored_word) ⇒ Object
- #class_name ⇒ Object
- #classify(table_name) ⇒ Object
-
#command_in_path?(command) ⇒ Boolean
Checks in PATH returns true if the command is found.
- #demodulize(class_name_in_module) ⇒ Object
- #pluralize(word) ⇒ Object
- #request_id ⇒ Object
-
#retrier(opts = {}, &block) ⇒ Block
Runs a code block, and retries it when an exception occurs.
- #singularize(word) ⇒ Object
-
#terminal_dimensions ⇒ Integer
Returns the columns and lines of the current tty.
- #twenty_four_hours_ago ⇒ Object
- #underscore(camel_cased_word) ⇒ Object
-
#utc_httpdate ⇒ Date, Time
Return the date and time in “HTTP-date” format as defined by RFC 7231.
- #verify_options(accepted, actual) ⇒ Object
Instance Method Details
#callable(call_her) ⇒ Object
module ClassMethods
98 99 100 |
# File 'lib/croesus/utils.rb', line 98 def callable(call_her) call_her.respond_to?(:call) ? call_her : lambda { call_her } end |
#caller_name ⇒ Object
114 115 116 |
# File 'lib/croesus/utils.rb', line 114 def caller_name caller_locations(2, 1).first.label end |
#camelize(underscored_word) ⇒ Object
102 103 104 |
# File 'lib/croesus/utils.rb', line 102 def camelize(underscored_word) underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase } end |
#class_name ⇒ Object
110 111 112 |
# File 'lib/croesus/utils.rb', line 110 def class_name demodulize(self.class) end |
#classify(table_name) ⇒ Object
106 107 108 |
# File 'lib/croesus/utils.rb', line 106 def classify(table_name) camelize singularize(table_name.to_s.sub(/.*\./, '')) end |
#command_in_path?(command) ⇒ Boolean
Checks in PATH returns true if the command is found
189 190 191 192 193 194 |
# File 'lib/croesus/utils.rb', line 189 def command_in_path?(command) found = ENV['PATH'].split(File::PATH_SEPARATOR).map do |p| File.exist?(File.join(p, command)) end found.include?(true) end |
#demodulize(class_name_in_module) ⇒ Object
118 119 120 |
# File 'lib/croesus/utils.rb', line 118 def demodulize(class_name_in_module) class_name_in_module.to_s.sub(/^.*::/, '') end |
#pluralize(word) ⇒ Object
122 123 124 |
# File 'lib/croesus/utils.rb', line 122 def pluralize(word) word.to_s.sub(/([^s])$/, '\1s') end |
#request_id ⇒ Object
147 148 149 |
# File 'lib/croesus/utils.rb', line 147 def request_id SecureRandom.uuid end |
#retrier(opts = {}, &block) ⇒ Block
Runs a code block, and retries it when an exception occurs. Should the number of retries be reached without success, the last exception will be raised.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/croesus/utils.rb', line 216 def retrier(opts = {}, &block) defaults = { tries: 2, sleep: 1, on: StandardError, matching: /.*/, :ensure => Proc.new {} } (opts, defaults) defaults.merge!(opts) return if defaults[:tries] == 0 on_exception, tries = [defaults[:on]].flatten, defaults[:tries] retries = 0 retry_exception = nil begin yield retries, retry_exception rescue *on_exception => exception raise unless exception. =~ defaults[:matching] raise if retries+1 >= defaults[:tries] # Interrupt Exception could be raised while sleeping begin sleep defaults[:sleep].respond_to?(:call) ? defaults[:sleep].call(retries) : defaults[:sleep] rescue *on_exception end retries += 1 retry_exception = exception retry ensure defaults[:ensure].call(retries) end end |
#singularize(word) ⇒ Object
126 127 128 |
# File 'lib/croesus/utils.rb', line 126 def singularize(word) word.to_s.sub(/s$/, '').sub(/ie$/, 'y') end |
#terminal_dimensions ⇒ Integer
Returns the columns and lines of the current tty.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/croesus/utils.rb', line 171 def terminal_dimensions [0, 0] unless STDOUT.tty? [80, 40] if OS.windows? if ENV['COLUMNS'] && ENV['LINES'] [ENV['COLUMNS'].to_i, ENV['LINES'].to_i] elsif ENV['TERM'] && command_in_path?('tput') [`tput cols`.to_i, `tput lines`.to_i] elsif command_in_path?('stty') `stty size`.scan(/\d+/).map {|s| s.to_i } else [0, 0] end rescue [0, 0] end |
#twenty_four_hours_ago ⇒ Object
151 152 153 |
# File 'lib/croesus/utils.rb', line 151 def twenty_four_hours_ago Time.now - ( 60 * 60 * 24) end |
#underscore(camel_cased_word) ⇒ Object
130 131 132 133 134 135 136 137 138 |
# File 'lib/croesus/utils.rb', line 130 def underscore(camel_cased_word) word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') word.gsub!(/([a-z\d])([A-Z])/, '\1_\2') word.tr! '-', '_' word.downcase! word end |
#utc_httpdate ⇒ Date, Time
Return the date and time in “HTTP-date” format as defined by RFC 7231.
143 144 145 |
# File 'lib/croesus/utils.rb', line 143 def utc_httpdate Time.now.utc.httpdate end |
#verify_options(accepted, actual) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/croesus/utils.rb', line 155 def (accepted, actual) # @private return unless debug || $DEBUG unless (act=Set[*actual.keys]).subset?(acc=Set[*accepted]) raise Croesus::Errors::UnknownOption, "\nDetected unknown option(s): #{(act - acc).to_a.inspect}\n" << "Accepted options are: #{accepted.inspect}" end yield if block_given? end |