在 ORM 层上添加装饰器。
Decorators on top of your ORM layer.
Disposable is the missing API of ActiveRecord*. The mission:
sync.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.
The public twin API is unbelievably simple.
Twin::new creates and populates the twin.Twin#"reader" returns the value or nested twin of the property.Twin#"writer"=(v) writes the value to the twin, not the model.Twin#sync writes all values to the model.Twin#save writes all values to the model and calls save on configured models.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.
class AlbumTwin "Nice Try"
twin.playable? #=> trueYou can also override property values in the constructor:
twin = AlbumTwin.new(album, title: "Plasticash")
twin.title #=> "Plasticash"Writers change values on the twin and are not propagated to the model.
twin.title = "Skamobile"
twin.title #=> "Skamobile"
album.title #=> "Nice Try"Writers on nested twins will "twin" the value.
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.
Given the above state change on the twin, here is what happens after calling #sync.
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.
twin.sync do |hash|
hash #=> {
# "title" => "Skamobile",
# "playable?" => true,
# "songs" => [{"name"=>"Adondo"...}..]
# }
endInvoking sync with block will not write anything to the models.
Needs to be included explicitly (Sync).
Calling #save will do sync plus calling save on all nested models. This implies that the models need to implement #save.
twin.save
#=> album.save
#=> .songs[0].save
Needs to be included explicitly (Save).
Nested objects can be declared with an inline twin.
property :artist do
property :full_name
endThe setter will automatically "twin" the model.
twin.artist = Artist.new
twin.artist #=> You can also specify nested objects with an explicit class.
property :artist, twin: TwinArtistYou can simply include feature modules into twins. If you want a feature to be included into all inline twins of your schema, use ::feature.
class AlbumTwin 1Again, coercion only happens in the setter.
Default values can be set via :default.
class AlbumTwin { "Object-#{id}" }
end
endDefault 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 can be defined analogue to property. The exposed API is the Array API.
twin.songs = [..] will override the existing value and "twin" every item.
Assignments from the constructor are _not_ tracked as changes.
```ruby
twin = AlbumTwin.new(album)
twin.changed? #=> falseThe Persisted module will track the persisted? field of the model, implying that your model exposes this field.
twin.persisted? #=> false
twin.save
twin.persisted? #=> trueThe 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.
twin = AlbumTwin.new(Album.create) # assuming we were using ActiveRecord.
twin.created? #=> false
twin.save
twin.created? #=> falseThis will only return true when the persisted? field has flipped.
The Expose module allows renaming properties.
class AlbumTwin "Run For Cover"Compositions of objects can be mapped, too.
class AlbumTwin {admin: {read: true, write: true}, user: {destroy: false}}Map that using the Struct module.
class AlbumTwin trueNote that you do not have to use Struct everywhere.
twin.permissions.user #=> {destroy: false}Of course, this works for writing, too.
twin.permissions.admin.read = :MAYBEAfter syncing, you will find a hash in the model.
album.permissions #=> {admin: {read: :MAYBE, write: true}, user: {destroy: false}}they indirect data, the twin's attributes get assigned without writing to the persistence layer, yet.
You can override getters for presentation.
class AlbumTwin By using the Parent feature you can access the parent twin of a nested one.
class AlbumTwin twinNote that this will internally add a parent property.
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,或尚未同步最近议题。