Module: Shared::IsData::Stripper

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/shared/is_data/stripper.rb

Overview

Utilities for comparing

Class Method Summary collapse

Class Method Details

.add_class_list(list) ⇒ Array

Returns of strings to be ignored.

Parameters:

  • list (Array)

    of symbols to be ignored

Returns:

  • (Array)

    of strings to be ignored



32
33
34
35
36
37
38
# File 'app/models/concerns/shared/is_data/stripper.rb', line 32

def self.add_class_list(list)
  ig_list  = RESERVED_ATTRIBUTES.dup
  add_list = list.dup if list.any?
  ig_list  += add_list if add_list
  # convert ignore list from symbols to strings for subsequent include test
  return ig_list.map(&:to_s)
end

.strip_identical_attributes(klass, attr = {}) ⇒ Hash

Parameters:

  • attr (Hash) (defaults to: {})

    is hash

Returns:

  • (Hash)


20
21
22
23
24
25
26
27
28
# File 'app/models/concerns/shared/is_data/stripper.rb', line 20

def self.strip_identical_attributes(klass, attr = {})
  begin # test to see if this class has an IGNORE_IDENTICAL constant
    ig = add_class_list(klass::IGNORE_IDENTICAL)
  rescue NameError
    ig = RESERVED_ATTRIBUTES.dup.map(&:to_s)
  end
  attr.delete_if{ |kee, _value| ig.include?(kee) }
  attr
end

.strip_similar_attributes(klass, attr = {}) ⇒ Hash

Parameters:

  • attr (Hash) (defaults to: {})

    is hash

Returns:

  • (Hash)


8
9
10
11
12
13
14
15
16
# File 'app/models/concerns/shared/is_data/stripper.rb', line 8

def self.strip_similar_attributes(klass, attr = {})
  begin # test to see if this class has an IGNORE_SIMILAR constant
    ig = add_class_list(klass::IGNORE_SIMILAR)
  rescue NameError
    ig = RESERVED_ATTRIBUTES.dup.map(&:to_s)
  end
  attr.delete_if{|kee, _value| ig.include?(kee) }
  attr
end

.tested_attributes(klass, compare = :identical) ⇒ Array

Returns of symbols which represent the attributes which will be tested for a given class.

Parameters:

  • klass (Class)
  • compare (Symbol, String) (defaults to: :identical)
    • one of [:identical, :similar] (optional, defaults to :identical)

Returns:

  • (Array)

    of symbols which represent the attributes which will be tested for a given class



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/concerns/shared/is_data/stripper.rb', line 43

def self.tested_attributes(klass, compare = :identical)
  case compare.to_s
  when 'similar'
    obj = klass.new
    obj.attributes.each_key { |kee| obj[kee] = 1 }
    strip_similar_attributes(klass, obj.attributes).collect { |kee, _val| kee.to_sym }
    # when 'identical'
    #   strip_identical_attributes(klass, klass.new.attributes).collect { |kee, _val| kee.to_sym }
  else
    strip_identical_attributes(klass, klass.new.attributes).collect { |kee, _val| kee.to_sym }
  end
end