Class: ActiveTsv::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/active_tsv/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(table, conditions) ⇒ Relation

Returns a new instance of Relation.



5
6
7
8
# File 'lib/active_tsv/relation.rb', line 5

def initialize(table, conditions)
  @table = table
  @conditions = conditions
end

Instance Method Details

#exist?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/active_tsv/relation.rb', line 14

def exist?
  !first.nil?
end

#firstObject



18
19
20
21
22
23
24
# File 'lib/active_tsv/relation.rb', line 18

def first
  @table.find do |i|
    @conditions.all? do |k, v|
      i[k] == v.to_s
    end
  end
end

#lastObject



26
27
28
# File 'lib/active_tsv/relation.rb', line 26

def last
  to_a.last
end

#to_aObject



30
31
32
33
34
35
36
# File 'lib/active_tsv/relation.rb', line 30

def to_a
  @table.select do |i|
    @conditions.all? do |k, v|
      i[k] == v.to_s
    end
  end
end

#where(conditions) ⇒ Object



10
11
12
# File 'lib/active_tsv/relation.rb', line 10

def where(conditions)
  self.class.new(@table, @conditions.merge(conditions))
end