Sha256: ab745c269e5ffe0846b3fd4bffdfbda6611924815d84e0d461b0e549a3deb62a

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

module Animoto
  module Support
    module ContentType
    
      # When included, includes the InstanceMethods module and extends the
      # ClassMethods module.
      def self.included base
        base.class_eval {
          include Animoto::Support::ContentType::InstanceMethods
          extend Animoto::Support::ContentType::ClassMethods
        }
      end
    
      module InstanceMethods
        # Returns the content type for this class.
        #
        # @return [String] the content type
        def content_type
          self.class.content_type
        end
      end
    
      module ClassMethods
        # @overload content_type(type)
        #   Sets the content type for this class.
        #   @param [String] content_type the type
        #   @return [String] the content type
        # @overload content_type()
        #   Returns the content type for this class.
        #   @return [String] the content type
        def content_type type = nil
          @content_type = type if type
          @content_type || infer_content_type
        end
      
        private
      
        # If no content type is explicitly set, this will infer the name of the content
        # type from the class name by lowercasing and underscoring the base name of the
        # class. For example, Animoto::DirectingJob becomes "directing_job".
        #
        # @return [String] the inferred content type
        def infer_content_type
          name.split('::').last.gsub(/(^)?([A-Z])/) { "#{'_' unless $1}#{$2.downcase}" }
        end      
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
animoto-0.1.1.beta1 ./lib/animoto/support/content_type.rb
animoto-0.1.0.beta1 ./lib/animoto/support/content_type.rb
animoto-0.1.0.beta0 ./lib/animoto/support/content_type.rb