Class: Praxis::FileGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/praxis/file_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, &block) ⇒ FileGroup

Returns a new instance of FileGroup.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/praxis/file_group.rb', line 7

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

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/praxis/file_group.rb', line 5

def base
  @base
end

#groupsObject (readonly)

Returns the value of attribute groups.



5
6
7
# File 'lib/praxis/file_group.rb', line 5

def groups
  @groups
end

Instance Method Details

#[](*names) ⇒ Object



36
37
38
# File 'lib/praxis/file_group.rb', line 36

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

#layout(&block) ⇒ Object



19
20
21
# File 'lib/praxis/file_group.rb', line 19

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

#map(name, pattern, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/praxis/file_group.rb', line 23

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