Sha256: 260c042fc5a497c43b1d6be2e342d7d4528cd595eca33a96761087c6772f04e0

Contents?: true

Size: 1.92 KB

Versions: 1

Compression:

Stored size: 1.92 KB

Contents

require 'baltix'

module ::Baltix::Source
   attr_reader :rootdir

   TYPES =
      %w(Gem Gemfile Rakefile Fake Base).reduce({}) do |types, name|
         autoload(:"#{name}", File.dirname(__FILE__) + "/source/#{name.downcase}")
         types.merge(name.downcase.to_sym => "Baltix::Source::#{name}")
      end

   class << self
      def search_in dir_in, options = {}
         dir = File.expand_path(dir_in)
         sources_pre =
            TYPES.map do |(name, const)|
               kls = self.const_get(const)
               kls.respond_to?(:search) && kls.search(dir, options) || []
            end.flatten | [ self::Fake.new({ source_file: File.join(dir, '.fake') }.to_os) ]

         sources_pre.group_by do |source|
            source.rootdir
         end.map do |_a, sources_in|
            ina = sources_in.select {|s| TYPES.keys[-1] == s.class.to_s.split("::").last.downcase.to_sym }

            TYPES.keys.reverse[1..-1].reduce(ina) do |res, kind|
               selected =
                  sources_in.select do |s|
                     TYPES.keys[TYPES.keys.index(kind)] == s.class.to_s.split("::").last.downcase.to_sym
                  end

               selected.any? && selected.map {|v| ([v] | res).reduce(&:+) } || res
            end
         end.flatten
      end

      def load_from sources_in
         [ sources_in ].flatten.compact.map do |source_in|
            type_code_in = source_in["type"].to_s.to_sym
            type_code = TYPES.keys.include?(type_code_in) && type_code_in || :fake
            require("baltix/source/#{type_code}")
            TYPES[type_code].constantize.new(source_in)
         end
      end

      def loaders
         @loaders ||=
            TYPES.map do |type, mod|
               if mod.constantize.constants.include?(:LOADERS)
                  mod.constantize.const_get(:LOADERS).values
               else
                  type
               end
            end.flatten.uniq
      end
   end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
baltix-0.1.1 lib/baltix/source.rb