Module: Functions

Defined in:
lib/functions.rb

Overview

this is Functions

Instance Method Summary collapse

Instance Method Details

#allObject



31
32
33
# File 'lib/functions.rb', line 31

def all
  exe("select * from #{find_table_name}")
end

#exe(query) ⇒ Object



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

def exe(query)
  Rubylite.new.exec_this(@database, query)
end

#find_by(arg1, arg2) ⇒ Object



26
27
28
29
# File 'lib/functions.rb', line 26

def find_by(arg1, arg2)
  exe("select * from #{find_table_name} where
     (#{find_table_name}.#{arg1} = '#{arg2}') limit 1")
end

#find_table_nameObject



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

def find_table_name
  (to_s + 's').downcase
end

#first(arg = 1) ⇒ Object



12
13
14
15
# File 'lib/functions.rb', line 12

def first(arg = 1)
  exe("select * from #{find_table_name} order by
     #{find_table_name}.id ASC limit #{arg}")
end

#last(arg = 1) ⇒ Object



21
22
23
24
# File 'lib/functions.rb', line 21

def last(arg = 1)
  exe("select * from #{find_table_name} order by
     #{find_table_name}.id DESC limit #{arg}")
end

#making_query_from_hash(hash) ⇒ Object



35
36
37
# File 'lib/functions.rb', line 35

def making_query_from_hash(hash)
  hash.inject('') { |s, (k, v)| s + "#{k} = '#{v}', " }
end

#take(arg = 1) ⇒ Object



17
18
19
# File 'lib/functions.rb', line 17

def take(arg = 1)
  exe("select * from #{find_table_name} limit #{arg}")
end

#update(hash, condn_hash) ⇒ Object



39
40
41
42
43
# File 'lib/functions.rb', line 39

def update(hash, condn_hash)
  str = making_query_from_hash(hash)
  condn = making_query_from_hash(condn_hash)
  exe("update #{to_s.downcase} set #{str[0..-3]} where #{condn[0..-3]}")
end