Sha256: 8f8ba75cf28f3c990d56731435c72e8a223d7294b4c5fedb20e88f2e97f931d5

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

require 'tap/support/lazydoc'

module Tap
  module Support
    
    # LazyAttributes adds methods to declare class-level accessors
    # for Lazydoc attributes.  The source_file for the class must
    # be set manually.
    #
    #   # ConstName::key value
    #   class ConstName
    #     extend LazyAttributes
    #
    #     self.source_file = __FILE__
    #     lazy_attr :key
    #   end
    #
    #   ConstName::key.subject           # => 'value'
    # 
    module LazyAttributes
      
      # The source_file for self.  Must be set independently.
      attr_accessor :source_file
      
      # Returns the lazydoc for source_file
      def lazydoc(resolve=true)
        lazydoc = Lazydoc[source_file]
        lazydoc.resolve if resolve
        lazydoc
      end
      
      # Creates a lazy attribute reader for the specified attribute.
      def lazy_attr(key, attribute=key)
        instance_eval %Q{def #{key}; @#{key} ||= get_lazy_attr('#{attribute}'); end}
      end
      
      private
      
      def get_lazy_attr(attribute) # :nodoc:
        lazydoc[self.to_s][attribute] ||= Lazydoc::Comment.new
      end
      
    end 
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
bahuvrihi-tap-0.10.8 lib/tap/support/lazy_attributes.rb
bahuvrihi-tap-0.11.0 lib/tap/support/lazy_attributes.rb
tap-0.11.0 lib/tap/support/lazy_attributes.rb