module StateGate::Builder::ConflictDetectionMethods

Description

Multiple private methods providing error handling functionality for StateGate::Builder.

Private Instance Methods

_dangerous_method_names() click to toggle source

returns an array of dagerous methods names found in ActiveRecord::AttributeMethods::RESTRICTED_CLASS_METHODS (which is called BLACKLISTED_CLASS_METHODS in 5.0 and 5.1)

# File lib/state_gate/builder/conflict_detection_methods.rb, line 128
def _dangerous_method_names
  %w[private public protected allocate new name parent superclass]
end
detect_class_method_conflict!(method_name) click to toggle source

Check if a class method is already defined. Checks are made: 1 –> is it an ActiveRecord dangerous method? 2 –> is it an ActiveRecord defined class method? 3 –> is it a singleton method of the klass? 4 –> is it defined within any of the klass ancestors?

If found, raise an error giving a guide to where the method has been defined

# File lib/state_gate/builder/conflict_detection_methods.rb, line 25
def detect_class_method_conflict!(method_name)
  defining_klass = _active_record_protected_method?(method_name) ||
                   _klass_singleton_method?(method_name) ||
                   _klass_ancestor_singleton_method?(method_name)

  return unless defining_klass

  raise_conflict_error method_name, type: 'a class', source: defining_klass
end
detect_instance_method_conflict!(method_name) click to toggle source

Check an instance method is already defined. Checks are made: 1 –> is it an ActiveRecord dangerous method? 2 –> is it an ActiveRecord defined instance method? 3 –> is it an instance method of the klass? 4 –> is it defined within any of the klass ancestors?

If found, raise an error giving a guide to where the method has been defined

# File lib/state_gate/builder/conflict_detection_methods.rb, line 45
def detect_instance_method_conflict!(method_name)
  defining_klass = _active_record_protected_method?(method_name) ||
                   _klass_instance_method?(method_name) ||
                   _klass_ancestor_instance_method?(method_name)

  return unless defining_klass

  raise_conflict_error method_name, source: defining_klass
end
raise_conflict_error(method_name, type: 'an instance', source: 'ActiveRecord') click to toggle source

Raise a StateGate::ConflictError with a details message of the problem

Message

StateGate for Klass#attribute will generate a class
method 'statuses', which is already defined by ActiveRecord.
# File lib/state_gate/builder/conflict_detection_methods.rb, line 64
def raise_conflict_error(method_name, type: 'an instance', source: 'ActiveRecord')
  fail StateGate::ConflictError, I18n.t('state_gate.builder.conflict_err',
                                        klass:       @klass,
                                        attribute:   @attribute,
                                        type:        type,
                                        method_name: method_name,
                                        source:      source)
end