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.



302
303
304
305
306
# File 'lib/hx.rb', line 302

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



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/hx.rb', line 313

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



308
309
310
311
# File 'lib/hx.rb', line 308

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

#get_entry(path) ⇒ Object



330
331
332
# File 'lib/hx.rb', line 330

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