Class: Rmap::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/rmap/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, table_name, id) ⇒ Row

Returns a new instance of Row.



9
10
11
12
13
# File 'lib/rmap/row.rb', line 9

def initialize(database, table_name, id)
  @database = database
  @table_name = table_name
  @id = id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rmap/row.rb', line 27

def method_missing name, *args
  if @database.table? name
    Table.new(@database, name).join(Table.new(@database, @table_name).eq(:id, @id), *args)
  elsif name.match(/\A.*=\Z/) && @database.method_missing(@table_name).column?(name.to_s.sub(/=\Z/, ""))
    update(name[/\A(.*)=\Z/, 1] => args[0])
  elsif @database.method_missing(@table_name).column? name
    fetch(name).first
  else
    super
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/rmap/row.rb', line 7

def id
  @id
end

Instance Method Details

#deleteObject



23
24
25
# File 'lib/rmap/row.rb', line 23

def delete
  @database.client.query("delete from `#{@table_name}` where id = '#{id}'")
end

#fetch(*args) ⇒ Object



15
16
17
# File 'lib/rmap/row.rb', line 15

def fetch(*args)
  @database.client.query("select #{(args.map { |field| "`#{@database.client.escape(field.to_s)}`"}).join(', ')} from `#{@table_name}` where id = '#{id}'", :as => :array).first
end

#to_hashObject



39
40
41
# File 'lib/rmap/row.rb', line 39

def to_hash
  @database.client.query("select * from `#{@table_name}` where id = '#{id}'", :as => :hash).first
end

#to_sObject



43
44
45
# File 'lib/rmap/row.rb', line 43

def to_s
  to_hash.to_json
end

#update(hash) ⇒ Object



19
20
21
# File 'lib/rmap/row.rb', line 19

def update(hash)
  @database.client.query("update `#{@table_name}` set #{(hash.map{|k,v| "`#{k}`='#{@database.client.escape(v.to_s)}'"}).join(', ')} where id = '#{id}'")
end