Sha256: 995c1f5bd83034af08515533fa7e6b704a0f3dffb801ced4039ab77c24408e39

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

require 'tap/support/constant_utils'
class String # :nodoc:
  include Tap::Support::ConstantUtils
end

module Tap
  module Support
    class Constant
      
      # The camelized name for self.
      attr_reader :name
      
      # The path to load to initialize the constant name.
      attr_reader :require_path
  
      def initialize(name, require_path=nil)
        @name = name
        @require_path = require_path
      end
      
      # Returns the underscored name.
      def path
        @path ||= name.underscore
      end
      
      # Returns the basename of path.
      def basename
        @basename ||= File.basename(path)
      end
      
      # Returns the path, minus the basename of path. 
      def dirname
        @dirname ||= (dirname = File.dirname(path)) == "." ? "" : dirname
      end
      
      # Returns the name of the constant, minus nesting.
      def const_name
        @const_name ||= (name =~ /.*::(.*)$/ ? $1 : name)
      end
      
      # Returns an array of the nesting constants of name.
      def nesting
        @nesting ||= (name =~ /(.*)::.*$/ ? $1 : '')
      end
      
      # Returns the number of constants in nesting.
      def nesting_depth
        @nesting_depth ||= nesting.split(/::/).length
      end
  
      # Returns the document for require_path, if set, or nil otherwise.
      def document
        require_path ? Support::Lazydoc[require_path] : nil 
      end
      
      def ==(another)
        another.kind_of?(Constant) && 
        another.name == self.name &&
        another.require_path == self.require_path
      end
  
      def constantize
        name.try_constantize do |const_name|
          require require_path
          name.constantize
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
bahuvrihi-tap-0.10.0 lib/tap/support/constant.rb
bahuvrihi-tap-0.10.1 lib/tap/support/constant.rb
bahuvrihi-tap-0.10.2 lib/tap/support/constant.rb
bahuvrihi-tap-0.10.3 lib/tap/support/constant.rb
bahuvrihi-tap-0.10.4 lib/tap/support/constant.rb
tap-0.10.0 lib/tap/support/constant.rb
tap-0.10.1 lib/tap/support/constant.rb