class GlobalRegistry::Bindings::Options::EntityOptionsParser

Public Class Methods

new(model_class) click to toggle source
# File lib/global_registry_bindings/options/entity_options_parser.rb, line 7
def initialize(model_class)
  @model_class = model_class
end

Public Instance Methods

defaults() click to toggle source
# File lib/global_registry_bindings/options/entity_options_parser.rb, line 11
def defaults # rubocop:disable Metrics/MethodLength
  {
    binding: :entity,
    id_column: :global_registry_id,
    mdm_id_column: nil,
    fingerprint_column: nil,
    type: @model_class.name.demodulize.underscore.to_sym,
    push_on: %i[create update destroy],
    parent: nil,
    parent_class: nil,
    exclude: %i[id created_at updated_at],
    fields: {},
    include_all_columns: false,
    mdm_timeout: 1.minute,
    ensure_type: true,
    if: nil, unless: nil
  }.freeze
end
parse(options_hash = {}) click to toggle source
# File lib/global_registry_bindings/options/entity_options_parser.rb, line 30
def parse(options_hash = {})
  validate_options! options_hash
  merge_defaults options_hash
  update_association_classes
  update_excludes
  @options
end

Private Instance Methods

association_class(name) click to toggle source
# File lib/global_registry_bindings/options/entity_options_parser.rb, line 80
def association_class(name)
  @model_class.reflect_on_association(name)&.klass
end
association_foreign_key(name) click to toggle source
# File lib/global_registry_bindings/options/entity_options_parser.rb, line 76
def association_foreign_key(name)
  @model_class.reflect_on_association(name)&.foreign_key&.to_sym
end
merge_defaults(options_hash = {}) click to toggle source
# File lib/global_registry_bindings/options/entity_options_parser.rb, line 45
def merge_defaults(options_hash = {})
  @options = defaults.merge(options_hash) do |key, oldval, newval|
    if key == :exclude
      case newval
      when Proc, Symbol
        newval
      else
        oldval.concat Array.wrap(newval)
      end
    else
      newval
    end
  end
end
update_association_classes() click to toggle source
# File lib/global_registry_bindings/options/entity_options_parser.rb, line 60
def update_association_classes
  unless @options[:parent_class] # rubocop:disable Style/GuardClause
    @options[:parent_class] = association_class @options[:parent]
  end
end
update_excludes() click to toggle source
# File lib/global_registry_bindings/options/entity_options_parser.rb, line 66
def update_excludes
  return unless @options[:exclude].is_a? Array
  @options[:exclude] << @options[:id_column]
  @options[:exclude] << @options[:mdm_id_column] if @options[:mdm_id_column].present?
  @options[:exclude] << @options[:fingerprint_column] if @options[:fingerprint_column].present?

  parent_id_column = association_foreign_key @options[:parent]
  @options[:exclude] << parent_id_column.to_sym if parent_id_column
end
validate_options!(options = {}) click to toggle source
# File lib/global_registry_bindings/options/entity_options_parser.rb, line 40
def validate_options!(options = {})
  unknown = options.keys - defaults.keys
  raise ArgumentError, "global-registry-bindings: Unknown options (#{unknown.join ', '})" unless unknown.empty?
end