Sha256: d544224548edb7aade30d83eae28ebfcecfe8316246db0ac342460604ae954ee

Contents?: true

Size: 1.96 KB

Versions: 5

Compression:

Stored size: 1.96 KB

Contents

module Cardio
  module Mod
    # Shared code for the three different load strategies: Eval, TmpFiles and BindingMagic
    class LoadStrategy
      class << self
        attr_accessor :tmp_files, :current

        def klass symbol
          case symbol
          when :tmp_files     then TmpFiles
          when :binding_magic then BindingMagic
          else                     Eval
          end
        end

        def tmp_files?
          Cardio.config.load_strategy == :tmp_files
        end
      end

      def initialize mod_dirs, loader
        LoadStrategy.current = self.class
        @mod_dirs = mod_dirs
        @loader = loader
      end

      def clean_comments?
        false
      end

      private

      def module_type
        @loader.class.module_type
      end

      def module_template
        @loader.class.module_class_template
      end

      def each_file &block
        if module_type == :set
          each_set_file(&block)
        else
          each_mod_dir module_type do |base_dir|
            each_file_in_dir base_dir, &block
          end
        end
      end

      def each_set_file &block
        each_mod_dir :set do |base_dir|
          @loader.patterns.each do |pattern|
            each_file_in_dir base_dir, pattern.to_s, &block
          end
        end
      end

      def each_mod_dir module_type
        @mod_dirs.each module_type do |base_dir|
          yield base_dir
        end
      end

      def each_file_in_dir base_dir, subdir=nil
        pattern = File.join(*[base_dir, subdir, "**/*.rb"].compact)
        Dir.glob(pattern).sort.each do |abs_path|
          rel_path = abs_path.sub("#{base_dir}/", "")
          const_parts = parts_from_path rel_path
          yield abs_path, const_parts
        end
      end

      def parts_from_path path
        # remove file extension and number prefixes
        parts = path.gsub(/\.rb/, "").gsub(%r{(?<=\A|/)\d+_}, "").split(File::SEPARATOR)
        parts.map(&:camelize)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
card-1.101.4 lib/cardio/mod/load_strategy.rb
card-1.101.3 lib/cardio/mod/load_strategy.rb
card-1.101.2 lib/cardio/mod/load_strategy.rb
card-1.101.1 lib/cardio/mod/load_strategy.rb
card-1.101.0 lib/cardio/mod/load_strategy.rb