Module: Diary::ModelQuery::ClassMethods

Defined in:
lib/diary-ruby/model.rb

Instance Method Summary collapse

Instance Method Details

#column_namesObject



17
18
19
# File 'lib/diary-ruby/model.rb', line 17

def column_names
  @column_names ||= columns.map {|col_info| col_info[1]}
end

#columnsObject



13
14
15
# File 'lib/diary-ruby/model.rb', line 13

def columns
  @columns ||= connection.execute("PRAGMA table_info(#{table_name})")
end

#find(attrs) ⇒ Object



37
38
39
# File 'lib/diary-ruby/model.rb', line 37

def find(attrs)
  where(attrs).first
end

#materialize(array_of_rows) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/diary-ruby/model.rb', line 27

def materialize(array_of_rows)
  results_to_hashes(array_of_rows).map do |record_hash|
    if respond_to?(:from_hash)
      from_hash(record_hash)
    else
      record_hash
    end
  end
end

#new_select_relationObject



41
42
43
# File 'lib/diary-ruby/model.rb', line 41

def new_select_relation
  Diary::Query::Select.new(table_name, self)
end

#results_to_hashes(array_of_rows) ⇒ Object



21
22
23
24
25
# File 'lib/diary-ruby/model.rb', line 21

def results_to_hashes(array_of_rows)
  array_of_rows.map do |row|
    Hash[ column_names.zip(row) ]
  end
end