module Agnostic::Duplicate::ClassMethods
Methods added to classes including Duplicable module
Attributes
Public Instance Methods
Adds a new duplicable changeset for the class.
By default created changesets apply a deep copy strategy over the attributes specified. If you want to set a shallow copy strategy then you can add the option ‘strategy: :shallow_copy`
@param *args [Array<Symbol>] a list of attribute names @param options [Hash] options specific for the changeset
# File lib/agnostic/duplicate.rb, line 267 def attr_duplicable(*args) @changeset_options = {} @changeset_options = args.pop if args.last.is_a? Hash duplicable_changesets << changeset_class.new(args) end
Sets global options for applying changesets
@param opts [Hash] The options for duplicable configuration @option opts [Boolean] :new_instance If ‘true` the duplicated instance
is created calling in first place `new` method over the class. if `false` the duplicated instance is created calling to `dup` method over the instance object.
# File lib/agnostic/duplicate.rb, line 280 def duplicable_config(opts) if opts.is_a? Hash @duplicable_options.merge! opts keep_valid_options else fail ArgumentError, 'Invalid options configuration' end end
@param option [Symbol] global option for duplication @return [Boolean] the boolean value expressing if the option is activated
# File lib/agnostic/duplicate.rb, line 292 def duplicable_option?(option) @duplicable_options ||= {} @duplicable_options[option] end
Private Instance Methods
@return [ChangeSet::Object] based on the strategy of duplication to be applied over the attributes
# File lib/agnostic/duplicate.rb, line 306 def changeset_class strategy = @changeset_options[:strategy] || :deep_copy class_name = strategy.to_s.split('_').map(&:capitalize).join Duplicate.const_get('ChangeSet').const_get("#{class_name}") end
Remove unknown options for applying changesets
# File lib/agnostic/duplicate.rb, line 300 def keep_valid_options @duplicable_options.keep_if { |key, _| [:new_instance].include? key } end