Sha256: f5b4846c3e7edf4d712713fd72b2ca6d49bee3a09f523c6fc3d42330fba6c403

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

# -*- ruby -*-
#encoding: utf-8

require 'arborist/loader' unless defined?( Arborist::Loader )


# A loader for Arborist that knows how to load stuff from files on disk.
class Arborist::Loader::File < Arborist::Loader

	##
	# The glob pattern to use for searching for files
	FILE_PATTERN = '**/*.rb'


	### Create a new loader that will read nodes from the specified +directory+.
	def initialize( directory )
		Arborist.load_all
		@directory = directory
	end


	# The directory to load files from
	attr_reader :directory


	### Return an Enumerator
	def paths
		path = Pathname( self.directory )
		if path.directory?
			return Pathname.glob( directory + FILE_PATTERN ).each
		else
			return [ path ].each
		end
	end


	### Return an Enumerator that yields Arborist::Nodes loaded from the target
	### directory.
	def nodes
		return self.enumerator_for( Arborist::Node )
	end


	### Return an Enumerator that yields Arborist::Monitors loaded from the target
	### directory.
	def monitors
		return self.enumerator_for( Arborist::Monitor )
	end


	### Return an Enumerator that yields Arborist::Observers loaded from the target
	### directory.
	def observers
		return self.enumerator_for( Arborist::Observer )
	end


	### Return an Enumerator that will instantiate and yield instances of the specified
	### +arborist_class+ for each file path in the loader's directory.
	def enumerator_for( arborist_class )
		return Enumerator.new do |yielder|
			self.paths.each do |file|
				objects = arborist_class.load( file )
				objects.each do |object|
					object.source = "file://%s" % [ file.expand_path ]
					yielder.yield( object )
				end
			end
		end
	end

end # class Arborist::Loader::File

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arborist-0.0.1.pre20160606141735 lib/arborist/loader/file.rb
arborist-0.0.1.pre20160128152542 lib/arborist/loader/file.rb