Sha256: 7dd784d7768df2f004f677cd7191608a4dcf346c039bde07fb68d21dea0bf46b

Contents?: true

Size: 949 Bytes

Versions: 23

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

module Praxis
  class FileGroup
    attr_reader :groups, :base

    def initialize(base, &block)
      if base.nil?
        raise ArgumentError, 'base must not be nil.' \
          'Are you missing a call Praxis::Application.instance.setup?'
      end

      @groups = {}
      @base = Pathname.new(base)

      instance_eval(&block) if block_given?
    end

    def layout(&block)
      instance_eval(&block)
    end

    def map(name, pattern, &block)
      return unless base.exist?

      if block_given?
        @groups[name] = FileGroup.new(base + pattern, &block)
      else
        @groups[name] ||= []
        files = Pathname.glob(base + pattern).select(&:file?)
        files.sort_by! { |file| [file.to_s.split('/').size, file.to_s] }
        files.each { |file| @groups[name] << file }
      end
    end

    def [](*names)
      names.inject(@groups) { |group, name| group[name] || [] }
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
praxis-2.0.0 lib/praxis/file_group.rb
praxis-2.0.pre.40 lib/praxis/file_group.rb
praxis-2.0.pre.39 lib/praxis/file_group.rb
praxis-2.0.pre.38 lib/praxis/file_group.rb
praxis-2.0.pre.37 lib/praxis/file_group.rb
praxis-2.0.pre.36 lib/praxis/file_group.rb
praxis-2.0.pre.35 lib/praxis/file_group.rb
praxis-2.0.pre.34 lib/praxis/file_group.rb
praxis-2.0.pre.33 lib/praxis/file_group.rb
praxis-2.0.pre.32 lib/praxis/file_group.rb
praxis-2.0.pre.31 lib/praxis/file_group.rb
praxis-2.0.pre.30 lib/praxis/file_group.rb
praxis-2.0.pre.29 lib/praxis/file_group.rb
praxis-2.0.pre.28 lib/praxis/file_group.rb
praxis-2.0.pre.27 lib/praxis/file_group.rb
praxis-2.0.pre.26 lib/praxis/file_group.rb
praxis-2.0.pre.25 lib/praxis/file_group.rb
praxis-2.0.pre.24 lib/praxis/file_group.rb
praxis-2.0.pre.23 lib/praxis/file_group.rb
praxis-2.0.pre.22 lib/praxis/file_group.rb