Class: Fleakr::Support::Utility

Inherits:
Object
  • Object
show all
Defined in:
lib/fleakr/support/utility.rb

Overview

Utility

Helpful utility methods.

Class Method Summary collapse

Class Method Details

.blank?(object) ⇒ Boolean

Determine if the passed value is blank. Blank values include nil, the empty string, and a string with only whitespace.

Returns:

  • (Boolean)


42
43
44
# File 'lib/fleakr/support/utility.rb', line 42

def self.blank?(object)
  object.to_s.sub(/\s+/, '') == ''
end

.class_name_for(module_name, name) ⇒ Object

Given a module name and an underscored name, generate the fully namespaced class name. For example:

>> Utility.class_name_for('Fleakr::Api', 'method_request')
=> "Fleakr::Api::MethodRequest"


16
17
18
19
20
21
22
# File 'lib/fleakr/support/utility.rb', line 16

def self.class_name_for(module_name, name)
  class_name = name.to_s.sub(/^(\w)/) {|m| m.upcase }
  class_name = class_name.sub(/(_(\w))/) {|m| $2.upcase }
  class_name = class_name.sub(/s$/, '')

  "#{module_name}::#{class_name}"
end

.extract_options(array_with_possible_options) ⇒ Object

Extract the options from an array if present and return the new array and any available options. For example:

>> Utility.extract_options([:sets, {:using => :key}])
=> [[:sets], {:using => :key}]

Note that this method does not modify the supplied parameter.



54
55
56
57
58
59
60
61
# File 'lib/fleakr/support/utility.rb', line 54

def self.extract_options(array_with_possible_options)
  array = array_with_possible_options.dup

  options = array.pop if array.last.is_a?(Hash)
  options ||= {}

  [array, options]
end

.id_attribute_for(class_name) ⇒ Object

Given a class name as a string with an optional namespace, generate an attribute ID parameter suitable for retrieving the ID of an associated object. For example:

>> Utility.id_attribute_for('Fleakr::Objects::Set')
=> "set_id"


31
32
33
34
35
36
37
# File 'lib/fleakr/support/utility.rb', line 31

def self.id_attribute_for(class_name)
  class_name = class_name.match(/([^:]+)$/)[1]
  class_name.gsub!(/([A-Z])([A-Z][a-z])/, '\1_\2')
  class_name.gsub!(/([a-z])([A-Z])/, '\1_\2')

  "#{class_name.downcase}_id"
end