Class: ActiveNomad::Base
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ActiveNomad::Base
- Defined in:
- lib/active_nomad.rb
Defined Under Namespace
Classes: FakeAdapter
Class Method Summary collapse
-
.attribute(name, type, options = {}) ⇒ Object
Declare a column.
-
.columns ⇒ Object
:nodoc:.
-
.from_json(string) ⇒ Object
Deserialize this record from a JSON string returned by #to_ordered_json.
-
.from_query_string(string) ⇒ Object
Deserialize this record from a query string returned by #to_ordered_query_string.
-
.from_serialized_attributes(deserialized_attributes) ⇒ Object
Recreate an object from a serialized string.
-
.reset_column_information ⇒ Object
:nodoc:.
-
.transaction ⇒ Object
Override to provide custom transaction semantics.
Instance Method Summary collapse
-
#destroy ⇒ Object
Destroy the object.
-
#to_destroy(&proc) ⇒ Object
Tell this record how to destroy itself.
-
#to_ordered_json ⇒ Object
Serialize this record as a JSON string.
-
#to_ordered_query_string ⇒ Object
Serialize this record as a query string.
-
#to_save(&proc) ⇒ Object
Tell this record how to save itself.
-
#to_serialized_attributes ⇒ Object
Return an ActiveSupport::OrderedHash of serialized attributes.
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'
148 149 150 151 152 |
# File 'lib/active_nomad.rb', line 148 def attribute(name, type, ={}) sql_type = FAKE_ADAPTER.type_to_sql(type, [:limit], [:precision], [:scale]) columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, [:default], sql_type, [:null] != false) reset_column_information end |
.columns ⇒ Object
:nodoc:
154 155 156 |
# File 'lib/active_nomad.rb', line 154 def columns # :nodoc: @columns ||= superclass.columns.dup end |
.from_json(string) ⇒ Object
Deserialize this record from a JSON string returned by #to_ordered_json.
88 89 90 91 92 93 94 95 96 |
# File 'lib/active_nomad.rb', line 88 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.
66 67 68 69 70 71 72 73 |
# File 'lib/active_nomad.rb', line 66 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.
40 41 42 43 44 45 46 47 48 |
# File 'lib/active_nomad.rb', line 40 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_information ⇒ Object
:nodoc:
158 159 160 161 162 163 |
# File 'lib/active_nomad.rb', line 158 def reset_column_information # :nodoc: # Reset everything, except the column information. columns = @columns super @columns = columns end |
.transaction ⇒ Object
Override to provide custom transaction semantics.
The default #transaction simply yields to the given block.
170 171 172 |
# File 'lib/active_nomad.rb', line 170 def transaction yield end |
Instance Method Details
#destroy ⇒ Object
Destroy the object.
The default is to call the block registered with #to_destroy. Override if you don’t want to use #to_destroy.
104 105 106 107 108 109 |
# File 'lib/active_nomad.rb', line 104 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_json ⇒ Object
Serialize this record as a JSON string.
Attributes are sorted by name.
80 81 82 |
# File 'lib/active_nomad.rb', line 80 def to_ordered_json to_serialized_attributes.to_json end |
#to_ordered_query_string ⇒ Object
Serialize this record as a query string.
Attributes are sorted by name.
55 56 57 58 59 60 |
# File 'lib/active_nomad.rb', line 55 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_attributes ⇒ Object
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 35 |
# File 'lib/active_nomad.rb', line 26 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 |