Sha256: 6b986d79c3a311c7682124009b73bc727908e5e8a8688e8f239bec73d6f07443

Contents?: true

Size: 1.8 KB

Versions: 21

Compression:

Stored size: 1.8 KB

Contents

# Encoding: utf-8

module ChemistryKit
  module CLI
    module Helpers
      # this loader returns the formulas found in a path according to these rules:
      # - directories loaded in alpha order
      # - children directories loaded before parents
      # - files loaded in alpha order
      # - lib directories are loaded before any other
      # - rules stack
      class FormulaLoader

        def initialize
          @formulas = []
        end

        def get_formulas(path)
          gather(path)
          @formulas
        end

        private

          def gather(path)
            # get all the directories for path
            directories = get_directories path
            if directories.empty?
              # if there are no directories is empty just get all the files and add it to the instance variable
              @formulas.concat Dir.glob(File.join(path, '*.rb')).sort
            else
              # otherwise for each of them recurse into it
              directories.sort!
              move_lib_to_top_of_stack directories
              directories.each do |directory|
                gather(File.join(path, directory))
              end
              # and then after add the files in the original parent
              @formulas.concat Dir.glob(File.join(path, '*.rb')).sort
            end
          end

          def get_directories(path)
            Dir.entries(path).select do |entry|
              (File.directory? File.join(path, entry)) && !(entry == '.' || entry == '..')
            end
          end

          def move_lib_to_top_of_stack(directories)
            if directories.include? 'lib'
              # if there is a lib directory, put that at the front
              directories.delete('lib')
              directories.unshift('lib')
            end
          end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
chemistrykit-3.10.1 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.10.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.9.1 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.9.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.9.0.rc3 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.9.0.rc2 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.9.0.rc1 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.8.1 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.8.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.7.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.6.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.5.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.4.2 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.4.1 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.4.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.3.1 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.3.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.2.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.1.0 lib/chemistrykit/cli/helpers/formula_loader.rb
chemistrykit-3.0.1 lib/chemistrykit/cli/helpers/formula_loader.rb