Module: Namo::Enumerable

Includes:
Enumerable
Included in:
Namo
Defined in:
lib/Namo/Enumerable.rb

Instance Method Summary collapse

Instance Method Details

#drop(n) ⇒ Object



47
48
49
# File 'lib/Namo/Enumerable.rb', line 47

def drop(n)
  self.class.new(@data.drop(n), formulae: @formulae.dup)
end

#drop_while(&block) ⇒ Object



55
56
57
# File 'lib/Namo/Enumerable.rb', line 55

def drop_while(&block)
  self.class.new(@data.drop_while{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#each(&block) ⇒ Object



8
9
10
11
# File 'lib/Namo/Enumerable.rb', line 8

def each(&block)
  return enum_for(:each) unless block_given?
  @data.each{|row_data| block.call(Row.new(row_data, @formulae))}
end

#first(n = nil) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/Namo/Enumerable.rb', line 27

def first(n = nil)
  if n
    self.class.new(@data.first(n), formulae: @formulae.dup)
  else
    @data.first ? Row.new(@data.first, @formulae) : nil
  end
end

#last(n = nil) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/Namo/Enumerable.rb', line 35

def last(n = nil)
  if n
    self.class.new(@data.last(n), formulae: @formulae.dup)
  else
    @data.last ? Row.new(@data.last, @formulae) : nil
  end
end

#partition(&block) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/Namo/Enumerable.rb', line 64

def partition(&block)
  matches, non_matches = @data.partition{|row| block.call(Row.new(row, @formulae))}
  [
    self.class.new(matches, formulae: @formulae.dup),
    self.class.new(non_matches, formulae: @formulae.dup),
  ]
end

#reject(&block) ⇒ Object



19
20
21
# File 'lib/Namo/Enumerable.rb', line 19

def reject(&block)
  self.class.new(@data.reject{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#select(&block) ⇒ Object Also known as: filter, find_all



13
14
15
# File 'lib/Namo/Enumerable.rb', line 13

def select(&block)
  self.class.new(@data.select{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#sort_by(&block) ⇒ Object



23
24
25
# File 'lib/Namo/Enumerable.rb', line 23

def sort_by(&block)
  self.class.new(@data.sort_by{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#take(n) ⇒ Object



43
44
45
# File 'lib/Namo/Enumerable.rb', line 43

def take(n)
  self.class.new(@data.take(n), formulae: @formulae.dup)
end

#take_while(&block) ⇒ Object



51
52
53
# File 'lib/Namo/Enumerable.rb', line 51

def take_while(&block)
  self.class.new(@data.take_while{|row| block.call(Row.new(row, @formulae))}, formulae: @formulae.dup)
end

#uniq(&block) ⇒ Object



59
60
61
62
# File 'lib/Namo/Enumerable.rb', line 59

def uniq(&block)
  rows = block ? @data.uniq{|row| block.call(Row.new(row, @formulae))} : @data.uniq
  self.class.new(rows, formulae: @formulae.dup)
end