Module: Hoodie::Utils::ClassMethods
- Defined in:
- lib/hoodie/utils.rb
Instance Method Summary collapse
- #callable(call_her) ⇒ Object
- #caller_name ⇒ Object
- #camelize(underscored_word) ⇒ Object
- #class_name ⇒ Object
- #classify(table_name) ⇒ Object
- #demodulize(class_name_in_module) ⇒ Object
- #pluralize(word) ⇒ Object
- #request_id ⇒ Object
- #singularize(word) ⇒ Object
- #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
31 32 33 |
# File 'lib/hoodie/utils.rb', line 31 def callable(call_her) call_her.respond_to?(:call) ? call_her : lambda { call_her } end |
#caller_name ⇒ Object
47 48 49 |
# File 'lib/hoodie/utils.rb', line 47 def caller_name caller_locations(2, 1).first.label end |
#camelize(underscored_word) ⇒ Object
35 36 37 |
# File 'lib/hoodie/utils.rb', line 35 def camelize(underscored_word) underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase } end |
#class_name ⇒ Object
43 44 45 |
# File 'lib/hoodie/utils.rb', line 43 def class_name demodulize(self.class) end |
#classify(table_name) ⇒ Object
39 40 41 |
# File 'lib/hoodie/utils.rb', line 39 def classify(table_name) camelize singularize(table_name.to_s.sub(/.*\./, '')) end |
#demodulize(class_name_in_module) ⇒ Object
51 52 53 |
# File 'lib/hoodie/utils.rb', line 51 def demodulize(class_name_in_module) class_name_in_module.to_s.sub(/^.*::/, '') end |
#pluralize(word) ⇒ Object
55 56 57 |
# File 'lib/hoodie/utils.rb', line 55 def pluralize(word) word.to_s.sub(/([^s])$/, '\1s') end |
#request_id ⇒ Object
80 81 82 |
# File 'lib/hoodie/utils.rb', line 80 def request_id SecureRandom.uuid end |
#singularize(word) ⇒ Object
59 60 61 |
# File 'lib/hoodie/utils.rb', line 59 def singularize(word) word.to_s.sub(/s$/, '').sub(/ie$/, 'y') end |
#twenty_four_hours_ago ⇒ Object
84 85 86 |
# File 'lib/hoodie/utils.rb', line 84 def twenty_four_hours_ago Time.now - ( 60 * 60 * 24) end |
#underscore(camel_cased_word) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/hoodie/utils.rb', line 63 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.
76 77 78 |
# File 'lib/hoodie/utils.rb', line 76 def utc_httpdate Time.now.utc.httpdate end |
#verify_options(accepted, actual) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/hoodie/utils.rb', line 88 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 |