Module: Hoodie::Utils::ClassMethods
- Defined in:
- lib/hoodie/utils.rb
Instance Method Summary collapse
- #callable(call_her) ⇒ Object
- #caller_name(position = 0) ⇒ 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
33 34 35 |
# File 'lib/hoodie/utils.rb', line 33 def callable(call_her) call_her.respond_to?(:call) ? call_her : lambda { call_her } end |
#caller_name(position = 0) ⇒ Object
49 50 51 |
# File 'lib/hoodie/utils.rb', line 49 def caller_name(position = 0) caller[position][/`.*'/][1..-2] end |
#camelize(underscored_word) ⇒ Object
37 38 39 |
# File 'lib/hoodie/utils.rb', line 37 def camelize(underscored_word) underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase } end |
#class_name ⇒ Object
45 46 47 |
# File 'lib/hoodie/utils.rb', line 45 def class_name demodulize(self.class) end |
#classify(table_name) ⇒ Object
41 42 43 |
# File 'lib/hoodie/utils.rb', line 41 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
124 125 126 127 128 129 |
# File 'lib/hoodie/utils.rb', line 124 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
53 54 55 |
# File 'lib/hoodie/utils.rb', line 53 def demodulize(class_name_in_module) class_name_in_module.to_s.sub(/^.*::/, '') end |
#pluralize(word) ⇒ Object
57 58 59 |
# File 'lib/hoodie/utils.rb', line 57 def pluralize(word) word.to_s.sub(/([^s])$/, '\1s') end |
#request_id ⇒ Object
82 83 84 |
# File 'lib/hoodie/utils.rb', line 82 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.
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 183 184 185 186 187 188 |
# File 'lib/hoodie/utils.rb', line 151 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
61 62 63 |
# File 'lib/hoodie/utils.rb', line 61 def singularize(word) word.to_s.sub(/s$/, '').sub(/ie$/, 'y') end |
#terminal_dimensions ⇒ Integer
Returns the columns and lines of the current tty.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/hoodie/utils.rb', line 106 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
86 87 88 |
# File 'lib/hoodie/utils.rb', line 86 def twenty_four_hours_ago Time.now - ( 60 * 60 * 24) end |
#underscore(camel_cased_word) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/hoodie/utils.rb', line 65 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.
78 79 80 |
# File 'lib/hoodie/utils.rb', line 78 def utc_httpdate Time.now.utc.httpdate end |
#verify_options(accepted, actual) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/hoodie/utils.rb', line 90 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 |