Module: Croesus::Utils::ClassMethods

Included in:
Croesus, WebRequest
Defined in:
lib/croesus/utils.rb

Instance Method Summary collapse

Instance Method Details

#callable(call_her) ⇒ Object



30
31
32
# File 'lib/croesus/utils.rb', line 30

def callable(call_her)
  call_her.respond_to?(:call) ? call_her : lambda { call_her }
end

#caller_nameObject



46
47
48
# File 'lib/croesus/utils.rb', line 46

def caller_name
  caller_locations(2, 1).first.label
end

#camelize(underscored_word) ⇒ Object



34
35
36
# File 'lib/croesus/utils.rb', line 34

def camelize(underscored_word)
  underscored_word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
end

#class_nameObject



42
43
44
# File 'lib/croesus/utils.rb', line 42

def class_name
  demodulize(self.class)
end

#classify(table_name) ⇒ Object



38
39
40
# File 'lib/croesus/utils.rb', line 38

def classify(table_name)
  camelize singularize(table_name.to_s.sub(/.*\./, ''))
end

#demodulize(class_name_in_module) ⇒ Object



50
51
52
# File 'lib/croesus/utils.rb', line 50

def demodulize(class_name_in_module)
  class_name_in_module.to_s.sub(/^.*::/, '')
end

#pluralize(word) ⇒ Object



54
55
56
# File 'lib/croesus/utils.rb', line 54

def pluralize(word)
  word.to_s.sub(/([^s])$/, '\1s')
end

#request_idObject



79
80
81
# File 'lib/croesus/utils.rb', line 79

def request_id
  SecureRandom.uuid
end

#singularize(word) ⇒ Object



58
59
60
# File 'lib/croesus/utils.rb', line 58

def singularize(word)
  word.to_s.sub(/s$/, '').sub(/ie$/, 'y')
end

#twenty_four_hours_agoObject



83
84
85
# File 'lib/croesus/utils.rb', line 83

def twenty_four_hours_ago
  Time.now - ( 60 * 60 * 24)
end

#underscore(camel_cased_word) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/croesus/utils.rb', line 62

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_httpdateDate, Time

Return the date and time in “HTTP-date” format as defined by RFC 7231.

Returns:

  • (Date, Time)

    in “HTTP-date” format



75
76
77
# File 'lib/croesus/utils.rb', line 75

def utc_httpdate
  Time.now.utc.httpdate
end

#verify_options(accepted, actual) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/croesus/utils.rb', line 87

def verify_options(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