Class: ActiveTsv::Relation
- Inherits:
-
Object
- Object
- ActiveTsv::Relation
- Defined in:
- lib/active_tsv/relation.rb
Instance Method Summary collapse
- #exist? ⇒ Boolean
- #first ⇒ Object
-
#initialize(table, conditions) ⇒ Relation
constructor
A new instance of Relation.
- #last ⇒ Object
- #to_a ⇒ Object
- #where(conditions) ⇒ Object
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
14 15 16 |
# File 'lib/active_tsv/relation.rb', line 14 def exist? !first.nil? end |
#first ⇒ Object
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 |
#last ⇒ Object
26 27 28 |
# File 'lib/active_tsv/relation.rb', line 26 def last to_a.last end |
#to_a ⇒ Object
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 |