Module: Vedeu::Common

Overview

A module for common methods used throughout Vedeu.

Instance Method Summary collapse

Instance Method Details

#absent?(variable) ⇒ Boolean

Returns a boolean indicating whether a variable is nil or empty.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)


12
13
14
# File 'lib/vedeu/common.rb', line 12

def absent?(variable)
  !present?(variable)
end

#demodulize(klass) ⇒ String

Removes the module part from the expression in the string.

Examples:

demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"

Parameters:

  • klass (Class|String)

Returns:

  • (String)


23
24
25
26
27
# File 'lib/vedeu/common.rb', line 23

def demodulize(klass)
  klass = klass.to_s

  klass[(klass.rindex('::') + 2)..-1]
end

#present?(variable) ⇒ Boolean

Returns a boolean indicating whether a variable has a useful value.

Parameters:

  • variable (String|Symbol|Array|Fixnum)

    The variable to check.

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/vedeu/common.rb', line 35

def present?(variable)
  return true if variable.is_a?(Fixnum)
  return true unless variable.nil? || variable.empty?

  false
end

#snake_case(name) ⇒ String

Converts a class name to a lowercase snake case string.

Examples:

snake_case(MyClassName) # => "my_class_name"
snake_case(NameSpaced::ClassName)
# => "name_spaced/class_name"

Parameters:

  • name (String)

Returns:

  • (String)


51
52
53
54
55
56
57
58
# File 'lib/vedeu/common.rb', line 51

def snake_case(name)
  name.gsub!(/::/, '/')
  name.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  name.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  name.tr!('-', '_')
  name.downcase!
  name
end