Class: Hx::Sort

Inherits:
Object
  • Object
show all
Includes:
Filter
Defined in:
lib/hx.rb

Instance Method Summary collapse

Methods included from Filter

#each_entry_path

Constructor Details

#initialize(input, options) ⇒ Sort

Returns a new instance of Sort.



314
315
316
317
318
# File 'lib/hx.rb', line 314

def initialize(input, options)
  @input = Cache.new(input)
  @key_fields = Array(options[:sort_by] || []).map { |f| f.to_s }
  @reverse = !!options[:reverse]
end

Instance Method Details

#each_entry(selector) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/hx.rb', line 325

def each_entry(selector)
  entries = []
  @input.each_entry(selector) do |path, entry|
    entries << [path, entry]
  end
  unless @key_fields.empty?
    entries = entries.sort_by do |path, entry|
      @key_fields.map { |f| entry[f] }
    end
  end
  entries.reverse! if @reverse
  entries.each do |path, entry|
    yield path, entry
  end
  self
end

#edit_entry(path, prototype = nil) ⇒ Object



320
321
322
323
# File 'lib/hx.rb', line 320

def edit_entry(path, prototype=nil)
  @input.edit_entry(path, prototype) { |text| yield text }
  self
end

#get_entry(path) ⇒ Object



342
343
344
# File 'lib/hx.rb', line 342

def get_entry(path)
  @input.get_entry(path)
end