Class: Perpetuity::Postgres::SQLUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/postgres/sql_update.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, id, attributes) ⇒ SQLUpdate

Returns a new instance of SQLUpdate.



9
10
11
12
13
# File 'lib/perpetuity/postgres/sql_update.rb', line 9

def initialize klass, id, attributes
  @class = klass
  @id = id
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/perpetuity/postgres/sql_update.rb', line 7

def attributes
  @attributes
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/perpetuity/postgres/sql_update.rb', line 7

def id
  @id
end

#klassObject (readonly)

Returns the value of attribute klass.



7
8
9
# File 'lib/perpetuity/postgres/sql_update.rb', line 7

def klass
  @klass
end

Instance Method Details

#to_sObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/perpetuity/postgres/sql_update.rb', line 15

def to_s
  sql = "UPDATE #{TableName.new(@class)}"
  if attributes.any?
    sql << " SET "
    sql << attributes.map do |name, value|
      value = SQLValue.new(value) if attributes.is_a? Hash
      "#{name} = #{value}"
    end.join(',')
  end
  sql << " WHERE id = #{SQLValue.new(id)}"
end