Class: ActiveNomad::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/active_nomad.rb

Defined Under Namespace

Classes: FakeAdapter

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute(name, type, options = {}) ⇒ Object

Declare a column.

Works like #add_column in a migration:

column :name, :string, :limit => 1, :null => false, :default => 'Joe'


79
80
81
82
83
# File 'lib/active_nomad.rb', line 79

def attribute(name, type, options={})
  sql_type = FAKE_ADAPTER.type_to_sql(type, options[:limit], options[:precision], options[:scale])
  columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, options[:default], sql_type, options[:null] != false)
  reset_column_information
end

.columnsObject

:nodoc:



85
86
87
# File 'lib/active_nomad.rb', line 85

def columns # :nodoc:
  @columns ||= superclass.columns.dup
end

.deserialize(string) ⇒ Object

Recreate an object from a serialized string.



33
34
35
36
37
38
39
40
41
42
# File 'lib/active_nomad.rb', line 33

def self.deserialize(string)
  params = string ? CGI.parse(string.strip) : {}
  instance = new
  columns.map do |column|
    next if !params.key?(column.name)
    value = params[column.name].first
    instance.send "#{column.name}=", deserialize_value(value, column.type)
  end
  instance
end

.reset_column_informationObject

:nodoc:



89
90
91
92
93
94
# File 'lib/active_nomad.rb', line 89

def reset_column_information # :nodoc:
  # Reset everything, except the column information.
  columns = @columns
  super
  @columns = columns
end

.transactionObject

Override to provide custom transaction semantics.

The default #transaction simply yields to the given block.



101
102
103
# File 'lib/active_nomad.rb', line 101

def transaction
  yield
end

Instance Method Details

#serializeObject

Return the attributes of this object serialized as a valid query string.

Attributes are sorted by name.



21
22
23
24
25
26
27
28
# File 'lib/active_nomad.rb', line 21

def serialize
  self.class.columns.map do |column|
    name = column.name
    value = serialize_value(send(name), column.type) or
      next
    "#{CGI.escape(name)}=#{value}"
  end.compact.sort.join('&')
end

#to_save(&proc) ⇒ Object

Tell this record how to save itself.



11
12
13
# File 'lib/active_nomad.rb', line 11

def to_save(&proc)
  @save_proc = proc
end