Class: Baza::Table

Inherits:
Object
  • Object
show all
Includes:
DatabaseModelFunctionality
Defined in:
lib/baza/table.rb

Direct Known Subclasses

Driver::Mysql::Table, Driver::Sqlite3::Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DatabaseModelFunctionality

#model_name, #to_model

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



4
5
6
# File 'lib/baza/table.rb', line 4

def db
  @db
end

Instance Method Details

#inspectObject



10
11
12
# File 'lib/baza/table.rb', line 10

def inspect
  to_s
end

#row(id) ⇒ Object

Raises:

  • (Baza::Errors::RowNotFound)


26
27
28
29
30
31
32
33
34
35
# File 'lib/baza/table.rb', line 26

def row(id)
  data = rows({id: id}, limit: 1).fetch
  raise Baza::Errors::RowNotFound unless data

  Baza::Row.new(
    db: db,
    table: name,
    data: data
  )
end

#rows(*args) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/baza/table.rb', line 14

def rows(*args)
  ArrayEnumerator.new do |yielder|
    db.select(name, *args) do |data|
      yielder << Baza::Row.new(
        db: db,
        table: name,
        data: data
      )
    end
  end
end

#to_paramObject



37
38
39
# File 'lib/baza/table.rb', line 37

def to_param
  name
end

#to_sObject



6
7
8
# File 'lib/baza/table.rb', line 6

def to_s
  "#<#{self.class.name} name=\"#{name}\">"
end