Sha256: 5409f5bf572c4a1569522c8c1fb5fcee05b82bfb1220e69536e3625000be9fb0
Contents?: true
Size: 1.68 KB
Versions: 2
Compression:
Stored size: 1.68 KB
Contents
--- id: customization title: Customization --- Clowne is built with extensibility in mind. You can create your own DSL commands and resolvers. Let's consider an example. Suppose that you want to add the `include_all` declaration to automagically include all associations (for ActiveRecord). First, you should add a custom declaration: ```ruby # Extend from Base declaration class IncludeAll < Clowne::Declarations::Base # :nodoc: all def compile(plan) # Just add all_associations declaration (self) to plan plan.set(:all_associations, self) end end # Register our declrations, i.e. extend DSL Clowne::Declarations.add :include_all, IncludeAll ``` See more on `plan` in [architecture overview](architecture.md). Secondly, register a resolver: ```ruby class AllAssociations # This method is called when all_associations command is applied. # # source – source record # record – target record (our clone) # declaration – declaration object # params – custom params passed to cloner def call(source, record, declaration, params:) source.class.reflections.each_value do |_name, reflection| # Exclude belongs_to associations next if reflection.macro == :belongs_to # Resolve and apply association cloner cloner_class = Clowne::Adapters::ActiveRecord::Associations.cloner_for(reflection) cloner_class.new(reflection, source, declaration, params).call(record) end record end end # Finally, register the resolver Clowne::Adapters::ActiveRecord.register_resolver( :all_associations, AllAssociations ) ``` Now you can use it likes this: ```ruby class UserCloner < Clowne::Cloner adapter :active_record include_all end ```
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
clowne-1.1.0 | docs/customization.md |
clowne-1.0.0 | docs/customization.md |