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.



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

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.



64
65
66
67
68
69
70
71
72
# File 'lib/active_nomad.rb', line 64

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

.from_serialized_attributes(serialized_attributes) ⇒ Object

Recreate an object from the serialized attributes returned by #to_serialized_attributes.



40
41
42
43
44
45
46
# File 'lib/active_nomad.rb', line 40

def self.from_serialized_attributes(serialized_attributes)
  serialized_attributes = serialized_attributes.dup
  columns_hash.each do |name, column|
    serialized_attributes[name] ||= column.default
  end
  instantiate(serialized_attributes)
end

.inspectObject



180
181
182
# File 'lib/active_nomad.rb', line 180

def inspect
  (n = name).blank? ? '(anonymous)' : n
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.

ActiveRecord::Rollback exceptions are also swallowed, as ActiveRecord raises these internally if the save returns false. If you need to provide custom rollback behavior, this is the place to implement it.



175
176
177
178
# File 'lib/active_nomad.rb', line 175

def transaction
  yield
rescue ActiveRecord::Rollback
end

Instance Method Details

#destroyObject

Destroy the object.

The default is to call the block registered with #to_destroy. Override if you don’t want to use #to_destroy.



103
104
105
106
107
108
# File 'lib/active_nomad.rb', line 103

def destroy
  if @destroy_proc
    @destroy_proc.call(self)
  end
  self
end

#to_destroy(&proc) ⇒ Object

Tell this record how to destroy itself.



16
17
18
# File 'lib/active_nomad.rb', line 16

def to_destroy(&proc)
  @destroy_proc = proc
end

#to_ordered_jsonObject

Serialize this record as a JSON string.

Attributes are sorted by name.



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

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.



53
54
55
56
57
58
# File 'lib/active_nomad.rb', line 53

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.



9
10
11
# File 'lib/active_nomad.rb', line 9

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.



26
27
28
29
30
31
32
33
34
# File 'lib/active_nomad.rb', line 26

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