Sha256: f6bd7db999b175b89f62db1d1d854b4bbcf9b3b5331278c9d49e6f9b97a40a69

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module DecoLite
  # Provides methods to manage fields that must be defined from
  # the dynamically loaded data.
  module FieldRequireable
    # Returns field names that will be used to validate the presence of
    # dynamically created fields from loaded objects.
    #
    # You must override this method if you want to return field names that
    # are required to be present. You may simply return a static array, but
    # if you want to dynamically manipulate this field, return a variable.
    def required_fields
      # @required_fields ||= []
      []
    end

    # Validator for field names. This validator simply checks to make
    # sure that the field was created, which can only occur if:
    # A) The field was defined on the model explicitly (e.g. attr_accessor :field).
    # B) The field was created as a result of loading data dynamically.
    def validate_required_fields
      required_fields.each do |field_name|
        next if required_field_exist? field_name: field_name

        errors.add(field_name, 'field is missing', type: :missing_required_field)
      end
    end

    # :reek:ManualDispatch - method added dynamically; this is the best way to check.
    def required_field_exist?(field_name:)
      respond_to?(field_name) && respond_to?("#{field_name}=".to_sym)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
deco_lite-0.3.0 lib/deco_lite/field_requireable.rb
deco_lite-0.2.5 lib/deco_lite/field_requireable.rb
deco_lite-0.2.4 lib/deco_lite/field_requireable.rb
deco_lite-0.2.3 lib/deco_lite/field_requireable.rb
deco_lite-0.2.2 lib/deco_lite/field_requireable.rb
deco_lite-0.1.1 lib/deco_lite/field_requireable.rb
deco_lite-0.1.0 lib/deco_lite/field_requireable.rb