Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with Active Record's .serialize #420

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

seanpdoyle
Copy link

Define ActiveResource::Base.dump and ActiveResource::Base.load to support passing classes directly to serialize as the :coder option:

Writing to String columns

Encodes Active Resource instances into a string to be stored in the database. Decodes strings read from the database into Active Resource instances.

class User < ActiveRecord::Base
  serialize :person, coder: Person
end

class Person < ActiveResource::Base
  schema do
    attribute :name, :string
  end
end

user = User.new
user.person = Person.new name: "Matz"
user.person_before_type_cast # => "{\"name\":\"Matz\"}"

Writing string values incorporates the Base.format:

Person.format = :xml

user.person = Person.new name: "Matz"
user.person_before_type_cast # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><person><name>Matz</name></person>"

Instances 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? # => false

user.person = Person.find(1)
user.person.persisted? # => true

Writing to JSON and JSONB columns

class User < ActiveRecord::Base
  serialize :person, coder: ActiveResource::Coder.new(Person, :serializable_hash)
end

class Person < ActiveResource::Base
  schema do
    attribute :name, :string
  end
end

user = User.new
user.person = Person.new name: "Matz"
user.person_before_type_cast # => {"name"=>"Matz"}

user.person.name # => "Matz"

The ActiveResource::Coder class

By default, #dump serializes the instance to a string value by calling ActiveResource::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"}

Define `ActiveResource::Base.dump` and `ActiveResource::Base.load` to
support passing classes directly to [serialize][] as the `:coder`
option:

Writing to String columns
---

Encodes Active Resource instances into a string to be stored in the
database. Decodes strings read from the database into Active Resource
instances.

```ruby
class User < ActiveRecord::Base
  serialize :person, coder: Person
end

class Person < ActiveResource::Base
  schema do
    attribute :name, :string
  end
end

user = User.new
user.person = Person.new name: "Matz"
user.person_before_type_cast # => "{\"name\":\"Matz\"}"
```

Writing string values incorporates the Base.format:

```ruby
Person.format = :xml

user.person = Person.new name: "Matz"
user.person_before_type_cast # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?><person><name>Matz</name></person>"
```

Instances are loaded as persisted when decoded from data containing a
primary key value, and new records when missing a primary key value:

```ruby
user.person = Person.new
user.person.persisted? # => false

user.person = Person.find(1)
user.person.persisted? # => true
```

Writing to JSON and JSONB columns
---

```ruby
class User < ActiveRecord::Base
  serialize :person, coder: ActiveResource::Coder.new(Person, :serializable_hash)
end

class Person < ActiveResource::Base
  schema do
    attribute :name, :string
  end
end

user = User.new
user.person = Person.new name: "Matz"
user.person_before_type_cast # => {"name"=>"Matz"}

user.person.name # => "Matz"
```

The `ActiveResource::Coder` class
===

By default, `#dump` serializes the instance to a string value by
calling `ActiveResource::Base#encode`:

```ruby
user.person_before_type_cast # => "{\"name\":\"Matz\"}"
```

To customize serialization, pass the method name or a block as the second
argument:

```ruby
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"}
```

[serialize]: https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html#method-i-serialize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant