Module: DecoLite::FieldConflictable

Includes:
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

Instance Method Details

#attr_accessor_exist?(field_name:) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

This method returns true

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/deco_lite/field_conflictable.rb', line 22

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.
  if field_names_include?(field_name: field_name)
    return options.strict?
  end

  # 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: field_name)
end

#field_names_include?(field_name:) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/deco_lite/field_conflictable.rb', line 36

def field_names_include?(field_name:)
  field_names.include? field_name
end

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



11
12
13
14
15
16
17
18
19
# File 'lib/deco_lite/field_conflictable.rb', line 11

def validate_field_conflicts!(field_name:, options:)
  return unless field_conflict?(field_name: field_name, options: 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.).'
end