Class: ActiveResource::Coder
- Inherits:
-
Object
- Object
- ActiveResource::Coder
- Defined in:
- lib/active_resource/coder.rb
Overview
Integrates with Active Record’s serialize method as the :coder
option.
Encodes Active Resource instances into a value to be stored in the database. Decodes values read from the database into Active Resource instances.
class User < ActiveRecord::Base
serialize :person, coder: ActiveResource::Coder.new(Person)
end
class Person < ActiveResource::Base
schema do
attribute :name, :string
end
end
user = User.new
user.person = Person.new name: "Matz"
user.person.name # => "Matz"
Values are loaded as persisted when decoded from data containing a primary key value, and new records when missing a primary key value:
user.person = Person.new
user.person.persisted? # => true
user.person = Person.find(1)
user.person.persisted? # => true
By default, #dump
serializes the instance to a string value by calling Base#encode:
user.person_before_type_cast # => "{\"name\":\"Matz\"}"
To customize serialization, pass the method name or a block as the second argument:
person = Person.new name: "Matz"
coder = ActiveResource::Coder.new(Person, :serializable_hash)
coder.dump(person) # => { "name" => "Matz" }
coder = ActiveResource::Coder.new(Person) { |person| person.serializable_hash }
coder.dump(person) # => { "name" => "Matz" }
Instance Attribute Summary collapse
-
#encoder ⇒ Object
Returns the value of attribute encoder.
-
#resource_class ⇒ Object
Returns the value of attribute resource_class.
Instance Method Summary collapse
-
#dump(value) ⇒ Object
Serializes a resource value to a value that will be stored in the database.
-
#initialize(resource_class, encoder_method = :encode, &block) ⇒ Coder
constructor
A new instance of Coder.
-
#load(value) ⇒ Object
Deserializes a value from the database to a resource instance.
Constructor Details
#initialize(resource_class, encoder_method = :encode, &block) ⇒ Coder
Returns a new instance of Coder.
53 54 55 56 |
# File 'lib/active_resource/coder.rb', line 53 def initialize(resource_class, encoder_method = :encode, &block) @resource_class = resource_class @encoder = block || encoder_method end |
Instance Attribute Details
#encoder ⇒ Object
Returns the value of attribute encoder.
51 52 53 |
# File 'lib/active_resource/coder.rb', line 51 def encoder @encoder end |
#resource_class ⇒ Object
Returns the value of attribute resource_class.
51 52 53 |
# File 'lib/active_resource/coder.rb', line 51 def resource_class @resource_class end |
Instance Method Details
#dump(value) ⇒ Object
Serializes a resource value to a value that will be stored in the database. Returns nil when passed nil
60 61 62 63 64 65 |
# File 'lib/active_resource/coder.rb', line 60 def dump(value) return if value.nil? raise ArgumentError.new("expected value to be #{resource_class}, but was #{value.class}") unless value.is_a?(resource_class) value.yield_self(&encoder) end |
#load(value) ⇒ Object
Deserializes a value from the database to a resource instance. Returns nil when passed nil
69 70 71 72 73 74 |
# File 'lib/active_resource/coder.rb', line 69 def load(value) return if value.nil? value = resource_class.format.decode(value) if value.is_a?(String) raise ArgumentError.new("expected value to be Hash, but was #{value.class}") unless value.is_a?(Hash) resource_class.new(value, value[resource_class.primary_key]) end |