项目
在 GitHub 查看
clowne-rb/clowne
关于
A flexible gem for cloning models
网站与应用开发实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文Clowne
A flexible gem for cloning your models. Clowne focuses on ease of use and provides the ability to connect various ORM adapters.
📖 Read Evil Martians Chronicles to learn about possible use cases.
Clowne is built by Evil Martians, an American design and engineering consultancy for developer tools, AI, and cybersecurity startups.
Installation
To install Clowne with RubyGems:
gem install clowne
Or add this line to your application's Gemfile:
gem "clowne"
Quick Start
Assume that you have the following model:
class User < ActiveRecord::Base
# create_table :users do |t|
# t.string :login
# t.string :email
# t.timestamps null: false
# end
has_one :profile
has_many :posts
end
class Profile < ActiveRecord::Base
# create_table :profiles do |t|
# t.string :name
# end
end
class Post < ActiveRecord::Base
# create_table :posts
end
Let's declare our cloners first:
class UserCloner < Clowne::Cloner
adapter :active_record
include_association :profile, clone_with: SpecialProfileCloner
include_association :posts
nullify :login
# params here is an arbitrary Hash passed into cloner
finalize do |_source, record, **params|
record.email = params[:email]
end
end
class SpecialProfileCloner < Clowne::Cloner
adapter :active_record
nullify :name
end
Now you can use UserCloner to clone existing records:
user = User.last
# => <#User id: 1, login: 'clown', email: '[email protected]'>
operation = UserCloner.call(user, email: "[email protected]")
# => <#Clowne::Utils::Operation...>
operation.to_record
# => <#User id: nil, login: nil, email: '[email protected]'>
operation.persist!
# => true
cloned = operation.to_record
# => <#User id: 2, login: nil, email: '[email protected]'>
cloned.login
# => nil
cloned.email
# => "[email protected]"
# associations:
cloned.posts.count == user.posts.count
# => true
cloned.profile.name
# => nil
Take a look at our documentation for more info!
Supported ORM adapters
| Adapter | 1:1 | *:1 | 1:M | M:M |
|---|---|---|---|---|
| Active Record | has_one | belongs_to | has_many | has_and_belongs_to |
| Sequel | one_to_one | - | one_to_many | many_to_many |
Maintainers
License
The gem is available as open source under the terms of the MIT License.
探索相关项目
加载中…