Sha256: a6f08853113395d59cd348e7f4fe2654616c8dd6a7b866ef0117a5c4f149ef29

Contents?: true

Size: 976 Bytes

Versions: 2

Compression:

Stored size: 976 Bytes

Contents

module Praxis

  class FileGroup

    attr_reader :groups, :base

    def initialize(base, &block)
      if base.nil?
        raise ArgumentError, "base must not be nil." \
          "Have you forgot to call 'setup' on the Praxis application instance?"
      end


      @groups = Hash.new
      @base = Pathname.new(base)

      if block_given?
        self.instance_eval(&block)
      end
    end

    def layout(&block)
      self.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| file.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

2 entries across 2 versions & 1 rubygems

Version Path
praxis-0.22.pre.2 lib/praxis/file_group.rb
praxis-0.22.pre.1 lib/praxis/file_group.rb