Class: Momomoto::Row

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

Overview

Base class for all Rows.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = []) ⇒ Row

Creates a new Row instance.



68
69
70
71
72
# File 'lib/momomoto/row.rb', line 68

def initialize( data = [] )
  @data = data
  @new_record = false
  clean_dirty
end

Class Method Details

.column_orderObject

Getter for the order of columns. The order is built from columns.keys.



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

def self.column_order
  @column_order
end

.columnsObject

Getter for the columns of this Row’s table.



13
14
15
# File 'lib/momomoto/row.rb', line 13

def self.columns
  @columns
end

.tableObject

Getter for the table this Row is using.



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

def self.table
  @table
end

Instance Method Details

#==(other) ⇒ Object

Compares @data value of the row with other.



42
43
44
# File 'lib/momomoto/row.rb', line 42

def ==( other )
  @data == other.instance_variable_get( :@data )
end

#[](fieldname) ⇒ Object

Gets the value of a given fieldname.

feed = Feeds.select( {:url => 'https://www.c3d2.de/news-atom.xml' )[0]
feed[:url] == 'https://www.c3d2.de/news-atom.xml'
  => true


29
30
31
# File 'lib/momomoto/row.rb', line 29

def []( fieldname )
  get_column( fieldname )
end

#[]=(fieldname, value) ⇒ Object

Sets fieldname to value.

feed = Feeds.select( {:url => 'http://www.c3d2.de/news-atom.xml' )[0]
feed[:url_host] = 'https://www.c3d2.de/'


37
38
39
# File 'lib/momomoto/row.rb', line 37

def []=( fieldname, value )
  set_column( fieldname, value )
end

#clean_dirtyObject

Removes all fields from dirty.



63
64
65
# File 'lib/momomoto/row.rb', line 63

def clean_dirty
  @dirty = []
end

#deleteObject

delete the row



91
92
93
# File 'lib/momomoto/row.rb', line 91

def delete
  self.class.table.delete( self )
end

#dirtyObject

Getter for @dirty which holds all changed fields of a row.



47
48
49
# File 'lib/momomoto/row.rb', line 47

def dirty
  @dirty
end

#dirty?Boolean

Returns true if there are fields in @dirty.

Returns:

  • (Boolean)


52
53
54
# File 'lib/momomoto/row.rb', line 52

def dirty?
  @dirty.length > 0
end

#get_column(column) ⇒ Object

generic getter for column values



133
134
135
136
137
138
139
140
141
142
# File 'lib/momomoto/row.rb', line 133

def get_column( column )
  raise "Unknown column #{column}" if not self.class.column_order.member?( column.to_sym )
  table = self.class.table
  index = self.class.column_order.index( column.to_sym )
  if table.columns[column.to_sym].respond_to?( :filter_get )
    table.columns[column.to_sym].filter_get( @data[index] )
  else
    @data[index]
  end
end

#mark_dirty(field) ⇒ Object

Marks a field as dirty.



57
58
59
60
# File 'lib/momomoto/row.rb', line 57

def mark_dirty( field )
  field = field.to_sym
  @dirty.push( field ) if not @dirty.member?( field )
end

#new_record=(value) ⇒ Object

Sets @new_record to true or false.



81
82
83
# File 'lib/momomoto/row.rb', line 81

def new_record=( value )
  @new_record = !!value
end

#new_record?Boolean

Returns true if the row is newly created.

Returns:

  • (Boolean)


75
76
77
# File 'lib/momomoto/row.rb', line 75

def new_record?
  @new_record
end

#set_column(column, value) ⇒ Object

Generic setter for column values. You should use this method when you are defining your own setter. This is useful for preprocessing value before it is written to database:

class Person < Momomoto::Table
  module Methods
    def nickname=( value )
      set_column( :nickname, value.downcase )
    end
  end
end

This defines a custom setter nickname= which invokes downcase on the given value.



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/momomoto/row.rb', line 118

def set_column( column, value )
  raise "Unknown column #{column}" if not self.class.column_order.member?( column.to_sym )
  table = self.class.table
  if not new_record? and table.primary_keys.member?( column.to_sym )
    raise Error, "Setting primary keys(#{column}) is only allowed for new records"
  end
  value = table.columns[column.to_sym].filter_set( value )
  index = self.class.column_order.index( column.to_sym )
  if !table.columns[column.to_sym].equal( value, @data[index] )
    mark_dirty( column )
    @data[index] = value
  end
end

#to_hashObject

converts row to hash



96
97
98
99
100
101
102
# File 'lib/momomoto/row.rb', line 96

def to_hash
  hash = {}
  self.class.columns.keys.each do | key |
    hash[key] = self[key]
  end
  hash
end

#writeObject

write the row to the database



86
87
88
# File 'lib/momomoto/row.rb', line 86

def write
  self.class.table.write( self )
end