Sha256: 76ccad9be1dadaff532973d1ef0c6666260f34c8d8f195d5cd41bb5de0106d4f

Contents?: true

Size: 1.48 KB

Versions: 22

Compression:

Stored size: 1.48 KB

Contents

module Vedeu

  # A module for common methods used throughout Vedeu.
  #
  module Common

    # Returns a boolean indicating whether a variable is nil or empty.
    #
    # @param variable [String|Symbol|Array|Fixnum] The variable to
    #   check.
    # @return [Boolean]
    def absent?(variable)
      !present?(variable)
    end

    # Removes the module part from the expression in the string.
    #
    # @example
    #   demodulize('Vedeu::SomeModule::SomeClass') # => "SomeClass"
    #
    # @param klass [Class|String]
    # @return [String]
    def demodulize(klass)
      klass = klass.to_s

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

    # Returns a boolean indicating whether a variable has a useful
    # value.
    #
    # @param variable [String|Symbol|Array|Fixnum] The variable to
    #   check.
    # @return [Boolean]
    def present?(variable)
      return true if variable.is_a?(Fixnum)
      return true unless variable.nil? || variable.empty?

      false
    end

    # Converts a class name to a lowercase snake case string.
    #
    # @example
    #   snake_case(MyClassName) # => "my_class_name"
    #   snake_case(NameSpaced::ClassName)
    #   # => "name_spaced/class_name"
    #
    # @param name [String]
    # @return [String]
    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

  end # Common

end # Vedeu

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
vedeu-0.6.50 lib/vedeu/common.rb
vedeu-0.6.49 lib/vedeu/common.rb
vedeu-0.6.48 lib/vedeu/common.rb
vedeu-0.6.47 lib/vedeu/common.rb
vedeu-0.6.46 lib/vedeu/common.rb
vedeu-0.6.45 lib/vedeu/common.rb
vedeu-0.6.44 lib/vedeu/common.rb
vedeu-0.6.43 lib/vedeu/common.rb
vedeu-0.6.42 lib/vedeu/common.rb
vedeu-0.6.41 lib/vedeu/common.rb
vedeu-0.6.40 lib/vedeu/common.rb
vedeu-0.6.39 lib/vedeu/common.rb
vedeu-0.6.38 lib/vedeu/common.rb
vedeu-0.6.37 lib/vedeu/common.rb
vedeu-0.6.36 lib/vedeu/common.rb
vedeu-0.6.35 lib/vedeu/common.rb
vedeu-0.6.34 lib/vedeu/common.rb
vedeu-0.6.33 lib/vedeu/common.rb
vedeu-0.6.32 lib/vedeu/common.rb
vedeu-0.6.31 lib/vedeu/common.rb