Class: Tina4::DatabaseResult

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tina4/database_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records = [], sql: "") ⇒ DatabaseResult

Returns a new instance of DatabaseResult.



10
11
12
13
14
# File 'lib/tina4/database_result.rb', line 10

def initialize(records = [], sql: "")
  @records = records || []
  @sql = sql
  @count = @records.length
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/tina4/database_result.rb', line 8

def count
  @count
end

#recordsObject (readonly)

Returns the value of attribute records.



8
9
10
# File 'lib/tina4/database_result.rb', line 8

def records
  @records
end

#sqlObject (readonly)

Returns the value of attribute sql.



8
9
10
# File 'lib/tina4/database_result.rb', line 8

def sql
  @sql
end

Instance Method Details

#[](index) ⇒ Object



32
33
34
# File 'lib/tina4/database_result.rb', line 32

def [](index)
  @records[index]
end

#each(&block) ⇒ Object



16
17
18
# File 'lib/tina4/database_result.rb', line 16

def each(&block)
  @records.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/tina4/database_result.rb', line 28

def empty?
  @records.empty?
end

#firstObject



20
21
22
# File 'lib/tina4/database_result.rb', line 20

def first
  @records.first
end

#lastObject



24
25
26
# File 'lib/tina4/database_result.rb', line 24

def last
  @records.last
end

#to_arrayObject



36
37
38
39
40
# File 'lib/tina4/database_result.rb', line 36

def to_array
  @records.map do |record|
    record.is_a?(Hash) ? record : record.to_h
  end
end

#to_crud(table_name: "data", primary_key: "id", editable: true) ⇒ Object



73
74
75
76
# File 'lib/tina4/database_result.rb', line 73

def to_crud(table_name: "data", primary_key: "id", editable: true)
  Tina4::Crud.generate_table(@records, table_name: table_name,
                              primary_key: primary_key, editable: editable)
end

#to_csv(separator: ",", headers: true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/tina4/database_result.rb', line 46

def to_csv(separator: ",", headers: true)
  return "" if @records.empty?
  lines = []
  cols = @records.first.keys
  lines << cols.join(separator) if headers
  @records.each do |row|
    lines << cols.map { |c| escape_csv(row[c], separator) }.join(separator)
  end
  lines.join("\n")
end

#to_json(*_args) ⇒ Object



42
43
44
# File 'lib/tina4/database_result.rb', line 42

def to_json(*_args)
  JSON.generate(to_array)
end

#to_paginate(page: 1, per_page: 10) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tina4/database_result.rb', line 57

def to_paginate(page: 1, per_page: 10)
  total = @records.length
  total_pages = (total.to_f / per_page).ceil
  offset = (page - 1) * per_page
  page_records = @records[offset, per_page] || []
  {
    data: page_records,
    page: page,
    per_page: per_page,
    total: total,
    total_pages: total_pages,
    has_next: page < total_pages,
    has_prev: page > 1
  }
end