Module: DecoLite::FieldConflictable

Includes:
FieldNameNamespaceable, FieldsOptionable
Included in:
FieldCreatable
Defined in:
lib/deco_lite/field_conflictable.rb

Overview

Defines methods to to manage fields that conflict with existing model attributes.

Constant Summary

Constants included from FieldsOptionable

DecoLite::FieldsOptionable::OPTION_FIELDS, DecoLite::FieldsOptionable::OPTION_FIELDS_DEFAULT, DecoLite::FieldsOptionable::OPTION_FIELDS_MERGE, DecoLite::FieldsOptionable::OPTION_FIELDS_STRICT, DecoLite::FieldsOptionable::OPTION_FIELDS_VALUES

Instance Method Summary collapse

Methods included from FieldNameNamespaceable

#field_name_or_field_name_with_namespace, #field_name_with_namespace

Instance Method Details

#attr_accessor_exist?(field_name:, options:) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
# File 'lib/deco_lite/field_conflictable.rb', line 45

def attr_accessor_exist?(field_name:, options:)
  field_name = field_name_or_field_name_with_namespace(field_name:, options:)

  respond_to?(field_name) || respond_to?(:"#{field_name}=")
end

#field_conflict?(field_name:, options:) ⇒ Boolean

This method returns true

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/deco_lite/field_conflictable.rb', line 27

def field_conflict?(field_name:, options:)
  # If field_name was already added using Model#load, there is only a
  # conflict if options.strict? is true.
  return options.strict? if field_names_include?(field_name:, options:)

  # If we get here, we know that :field_name does not exist as an
  # attribute on the model. If the attribute already exists on the
  # model, this is a conflict because we cannot override an attribute
  # that already exists on the model
  attr_accessor_exist?(field_name:, options:)
end

#field_names_include?(field_name:, options:) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/deco_lite/field_conflictable.rb', line 39

def field_names_include?(field_name:, options:)
  field_name = field_name_or_field_name_with_namespace(field_name:, options:)

  field_names.include? field_name
end

#validate_field_conflicts!(field_name:, options:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/deco_lite/field_conflictable.rb', line 13

def validate_field_conflicts!(field_name:, options:)
  return unless field_conflict?(field_name:, options:)

  field_name = field_name_or_field_name_with_namespace(field_name:, options:)

  raise "Field :#{field_name} conflicts with existing method(s) " \
        ":#{field_name} and/or :#{field_name}=; " \
        'this will raise an error when loading using strict mode ' \
        "(i.e. options: { #{OPTION_FIELDS}: :#{OPTION_FIELDS_STRICT} }) " \
        'or if the method(s) are native to the object (e.g :to_s, :==, etc.). ' \
        "Current options are: options: #{options.to_h}."
end