Class: ROM::Filesystem::Dataset

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/filesystem/dataset.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir, options = {}) ⇒ Dataset

Returns a new instance of Dataset.



4
5
6
7
8
9
# File 'lib/rom/filesystem/dataset.rb', line 4

def initialize(dir, options = {})
  @dir = dir
  @options = options

  @options[:select] ||= ['*']
end

Instance Method Details

#each(&block) ⇒ Object



19
20
21
# File 'lib/rom/filesystem/dataset.rb', line 19

def each(&block)
  to_a.each(&block)
end

#select(*args) ⇒ Object



11
12
13
# File 'lib/rom/filesystem/dataset.rb', line 11

def select(*args)
  self.class.new(@dir, @options.merge(select: args))
end

#sortObject



15
16
17
# File 'lib/rom/filesystem/dataset.rb', line 15

def sort
  self.class.new(@dir, @options.merge(sort: true))
end

#to_aObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rom/filesystem/dataset.rb', line 23

def to_a
  root = Pathname.new(@dir.path)
  paths = @options[:select].map { |filter| "#{root}/#{filter}" }

  matches = Dir[*paths].reject { |f| f == '.' || f == '..' }
  matches = matches.sort if @options[:sort]

  matches.map do |filename|
    path = root.join(filename)

    {
      name: path.basename.to_s,
      path: path
    }
  end
end