Class: TableSync::Publishing::Helpers::Attributes

Inherits:
Object
  • Object
show all
Defined in:
lib/table_sync/publishing/helpers/attributes.rb

Constant Summary collapse

BASE_SAFE_JSON_TYPES =
[
  NilClass,
  String,
  TrueClass,
  FalseClass,
  Numeric,
  Symbol,
].freeze
NOT_MAPPED =

add custom seializables?

Object.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Attributes

Returns a new instance of Attributes.



20
21
22
# File 'lib/table_sync/publishing/helpers/attributes.rb', line 20

def initialize(attributes)
  @attributes = attributes.deep_symbolize_keys
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



18
19
20
# File 'lib/table_sync/publishing/helpers/attributes.rb', line 18

def attributes
  @attributes
end

Instance Method Details

#filter_safe_for_serialization(object) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/table_sync/publishing/helpers/attributes.rb', line 28

def filter_safe_for_serialization(object)
  case object
  when Array
    object.each_with_object([]) do |value, memo|
      value = filter_safe_for_serialization(value)
      memo << value if object_mapped?(value)
    end
  when Hash
    object.each_with_object({}) do |(key, value), memo|
      key = filter_safe_for_serialization(key)
      value = filter_safe_hash_values(value)
      memo[key] = value if object_mapped?(key) && object_mapped?(value)
    end
  when Float::INFINITY
    NOT_MAPPED
  when *BASE_SAFE_JSON_TYPES
    object
  else # rubocop:disable Lint/DuplicateBranch
    NOT_MAPPED
  end
end

#filter_safe_hash_values(value) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/table_sync/publishing/helpers/attributes.rb', line 50

def filter_safe_hash_values(value)
  case value
  when Symbol
    value.to_s # why?
  else
    filter_safe_for_serialization(value)
  end
end

#object_mapped?(object) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/table_sync/publishing/helpers/attributes.rb', line 59

def object_mapped?(object)
  object != NOT_MAPPED
end

#serializeObject



24
25
26
# File 'lib/table_sync/publishing/helpers/attributes.rb', line 24

def serialize
  filter_safe_for_serialization(attributes)
end