Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

disposable

> 编程语言
Open source

Decorators on top of your ORM layer.

171 stars0 likes0 views
WebsiteGitHub

About

Decorators on top of your ORM layer.

Disposable

Decorators on top of your ORM layer.

Introduction

Disposable is the missing API of ActiveRecord*. The mission:

  • Maintain a manipulatable object graph that is a copy/map of a persistent structure.
  • Prevent any write to the persistence layer until you say sync.
  • Help designing your domain layer without being restricted to database layouts (renaming, compositions, hash fields).
  • Provide additional behavior like change tracking, imperative callbacks and collection semantics.

Disposable gives you "Twins": non-persistent domain objects. That is reflected in the name of the gem. They can read from and write values to a persistent object and abstract the persistence layer until data is synced to the model.

API

The public twin API is unbelievably simple.

  1. Twin::new creates and populates the twin.
  2. Twin#"reader" returns the value or nested twin of the property.
  3. Twin#"writer"=(v) writes the value to the twin, not the model.
  4. Twin#sync writes all values to the model.
  5. Twin#save writes all values to the model and calls save on configured models.

Twin

Twins are only # FIXME % slower than AR alone.

Twins implement light-weight decorators objects with a unified interface. They map objects, hashes, and compositions of objects, along with optional hashes to inject additional options.

Every twin is based on a defined schema.

ruby
class AlbumTwin  "Nice Try"
twin.playable? #=> true

You can also override property values in the constructor:

ruby
twin = AlbumTwin.new(album, title: "Plasticash")
twin.title #=> "Plasticash"

Writers

Writers change values on the twin and are not propagated to the model.

ruby
twin.title = "Skamobile"
twin.title  #=> "Skamobile"
album.title #=> "Nice Try"

Writers on nested twins will "twin" the value.

ruby
twin.songs #=> []
twin.songs  [>]
album.songs #=> []

The added twin is not passed to the model. Note that the nested song is a twin, not the model itself.

Sync

Given the above state change on the twin, here is what happens after calling #sync.

ruby
album.title  #=> "Nice Try"
album.songs #=> []

twin.sync

album.title  #=> "Skamobile"
album.songs #=> []

#sync writes all configured attributes back to the models using public setters as album.name= or album.songs=. This is recursive and will sync the entire object graph.

Note that sync might already trigger saving the model as persistence layers like ActiveRecord can't deal with collection= [] and instantly persist that.

You may implement your syncing manually by passing a block to sync.

ruby
twin.sync do |hash|
  hash #=> {
  #  "title"     => "Skamobile",
  #  "playable?" => true,
  #  "songs"     => [{"name"=>"Adondo"...}..]
  # }
end

Invoking sync with block will not write anything to the models.

Needs to be included explicitly (Sync).

Save

Calling #save will do sync plus calling save on all nested models. This implies that the models need to implement #save.

ruby
twin.save
#=> album.save
#=>      .songs[0].save

Needs to be included explicitly (Save).

Nested Twin

Nested objects can be declared with an inline twin.

ruby
property :artist do
  property :full_name
end

The setter will automatically "twin" the model.

ruby
twin.artist = Artist.new
twin.artist #=> 

You can also specify nested objects with an explicit class.

ruby
property :artist, twin: TwinArtist

Unnest

todo: document

Features

You can simply include feature modules into twins. If you want a feature to be included into all inline twins of your schema, use ::feature.

ruby
class AlbumTwin  1

Again, coercion only happens in the setter.

Defaults

Default values can be set via :default.

ruby
class AlbumTwin  { "Object-#{id}" }
  end
end

Default value is applied when the model's getter returns nil when initializing the twin.

Note that :default also works with :virtual and readable: false. :default can also be a lambda which is then executed in twin context.

Collections

Collections can be defined analogue to property. The exposed API is the Array API.

  • twin.songs = [..] will override the existing value and "twin" every item.
  • `twin.songs true twin.changed?(:name) #=> true twin.changed?(:playable?) #=> false twin.songs.changed? #=> true twin.songs[0].changed? #=> false twin.songs[1].changed? #=> true

Assignments from the constructor are _not_ tracked as changes.

```ruby
twin = AlbumTwin.new(album)
twin.changed? #=> false

Persistance Tracking

The Persisted module will track the persisted? field of the model, implying that your model exposes this field.

ruby
twin.persisted? #=> false
twin.save
twin.persisted? #=> true

The persisted? field is a copy of the model's persisted? flag.

You can also use created? to find out whether a twin's model was already persisted or just got created in this session.

ruby
twin = AlbumTwin.new(Album.create) # assuming we were using ActiveRecord.
twin.created? #=> false
twin.save
twin.created? #=> false

This will only return true when the persisted? field has flipped.

Renaming

The Expose module allows renaming properties.

ruby
class AlbumTwin  "Run For Cover"

Composition

Compositions of objects can be mapped, too.

ruby
class AlbumTwin  {admin: {read: true, write: true}, user: {destroy: false}}

Map that using the Struct module.

ruby
class AlbumTwin  true

Note that you do not have to use Struct everywhere.

ruby
twin.permissions.user #=> {destroy: false}

Of course, this works for writing, too.

ruby
twin.permissions.admin.read = :MAYBE

After syncing, you will find a hash in the model.

ruby
album.permissions #=> {admin: {read: :MAYBE, write: true}, user: {destroy: false}}

With Representers

they indirect data, the twin's attributes get assigned without writing to the persistence layer, yet.

With Contracts

Overriding Getter for Presentation

You can override getters for presentation.

ruby
class AlbumTwin  

Parent

By using the Parent feature you can access the parent twin of a nested one.

ruby
class AlbumTwin  twin

Note that this will internally add a parent property.

Builders

Used In

  • Reform forms are based on twins and add a little bit of form decoration on top. Every nested form is a twin.
  • Trailblazer uses twins as decorators and callbacks in operations to structure business logic.

Development

  • rake test runs all tests without builder_test.rb. For the latter, run BUNDLE_GEMFILE=Gemfile_builder_test.rb bundle exec rake test_builder

Issues· 32 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyobjectmapper

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言