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'


147
148
149
150
151
# File 'lib/active_nomad.rb', line 147

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:



153
154
155
# File 'lib/active_nomad.rb', line 153

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

.from_json(string) ⇒ Object

Deserialize this record from a JSON string returned by #to_ordered_json.



90
91
92
93
94
95
96
97
98
# File 'lib/active_nomad.rb', line 90

def self.from_json(string)
  return new if string.blank?
  begin
    serialized_attributes = JSON.parse(string)
  rescue JSON::ParserError
    serialized_attributes = {}
  end
  from_serialized_attributes(serialized_attributes)
end

.from_query_string(string) ⇒ Object

Deserialize this record from a query string returned by #to_ordered_query_string.



68
69
70
71
72
73
74
75
# File 'lib/active_nomad.rb', line 68

def self.from_query_string(string)
  return new if string.blank?
  serialized_attributes = string.strip.split(/&/).map do |pair|
    name, value = pair.split(/=/, 2)
    [CGI.unescape(name), CGI.unescape(value)]
  end
  from_serialized_attributes(serialized_attributes)
end

.from_serialized_attributes(deserialized_attributes) ⇒ Object

Recreate an object from a serialized string.



42
43
44
45
46
47
48
49
50
# File 'lib/active_nomad.rb', line 42

def self.from_serialized_attributes(deserialized_attributes)
  instance = new
  deserialized_attributes.each do |name, serialized_value|
    column = columns_hash[name.to_s] or
      next
    instance.send "#{column.name}=", deserialize_value(serialized_value, column.type)
  end
  instance
end

.reset_column_informationObject

:nodoc:



157
158
159
160
161
162
# File 'lib/active_nomad.rb', line 157

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.



169
170
171
# File 'lib/active_nomad.rb', line 169

def transaction
  yield
end

Instance Method Details

#to_destroy(&proc) ⇒ Object

Tell this record how to destroy itself.



18
19
20
# File 'lib/active_nomad.rb', line 18

def to_destroy(&proc)
  @destroy_proc = proc
end

#to_ordered_jsonObject

Serialize this record as a JSON string.

Attributes are sorted by name.



82
83
84
# File 'lib/active_nomad.rb', line 82

def to_ordered_json
  to_serialized_attributes.to_json
end

#to_ordered_query_stringObject

Serialize this record as a query string.

Attributes are sorted by name.



57
58
59
60
61
62
# File 'lib/active_nomad.rb', line 57

def to_ordered_query_string
  to_serialized_attributes.map do |name, value|
    next nil if value.nil?
    "#{CGI.escape(name)}=#{CGI.escape(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

#to_serialized_attributesObject

Return an ActiveSupport::OrderedHash of serialized attributes.

Attributes are sorted by name. Each value is either a string or nil.



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

def to_serialized_attributes
  attributes = ActiveSupport::OrderedHash.new
  columns = self.class.columns_hash
  self.class.column_names.sort.each do |name|
    column = columns[name] or
      next
    attributes[name] = serialize_value(send(name), column.type)
  end
  attributes
end