Sha256: b817eb03a99bc1ca8b21c431f0e2204c55960606177abde153154b2de02d287c

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

module Tadpole
  module SectionProviders
    class << self
      def providers
        @providers ||= []
      end

      def register_provider(*provs)
        providers.push(*provs)
      end
    end
    
    class SectionProvider
      attr_accessor :owner, :content, :full_path
      
      EXTENSIONS = []
      
      # You don't need to override this method unless
      # you need custom functionality beyond checking
      # if a file exists under one of the possible file 
      # extensions.
      # 
      # @return [String, nil] The full pathname
      def self.provides?(object, basename) 
        self.const_get("EXTENSIONS").any? do |ext|
          path = basename + ext
          return path if path_suitable?(path) 
        end
        nil
      end
      
      # Override this if you want to provide another
      # mechanism to detect that the path is suitable
      # for use. Default just checks File.file?(full_path)
      def self.path_suitable?(full_path)
        File.file?(full_path)
      end
      
      # @abstract
      def render(locals = {}, &block) 
        raise NotImplementedError, "abstract class"
      end
        
      def initialize(full_path, owner = nil)
        self.full_path = full_path
        self.owner = owner
        self.content = File.read(full_path)
      end
      
      def inspect; "#<%s:0x%s>" % [self.class.to_s.split('::').last, object_id.to_s(16)] end
      
      def method_missing(meth, *args, &block)
        if owner.options.respond_to?(meth)
          owner.options[meth]
        elsif owner.respond_to?(meth)
          owner.send(meth, *args, &block)
        else
          super
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tadpole-0.1.7 lib/tadpole/providers/section_provider.rb
tadpole-0.1.4 lib/tadpole/providers/section_provider.rb
tadpole-0.1.5 lib/tadpole/providers/section_provider.rb
tadpole-0.1.6 lib/tadpole/providers/section_provider.rb