Class: Para::ModelFieldParsers::Store

Inherits:
Base
  • Object
show all
Defined in:
lib/para/model_field_parsers/store.rb

Instance Attribute Summary

Attributes inherited from Base

#fields_hash, #mappings, #model

Instance Method Summary collapse

Methods inherited from Base

#find_attributes_for_mapping, #initialize, register

Constructor Details

This class inherits a constructor from Para::ModelFieldParsers::Base

Instance Method Details

#applicable?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/para/model_field_parsers/store.rb', line 31

def applicable?
  !model.stored_attributes.empty? || model_includes_json_fields?
end

#json_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/para/model_field_parsers/store.rb', line 41

def json_field?(field)
  field.type.to_s.in?(%w(json jsonb))
end

#model_includes_json_fields?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/para/model_field_parsers/store.rb', line 35

def model_includes_json_fields?
  fields_hash.any? do |_, field|
    json_field?(field)
  end
end

#parse!Object



6
7
8
9
# File 'lib/para/model_field_parsers/store.rb', line 6

def parse!
  process_stored_attributes
  process_remaining_json_fields
end

#process_remaining_json_fieldsObject

Duplicate fields to avoid updating the hash while iterating through it then remove remaining json fields from the hash



25
26
27
28
29
# File 'lib/para/model_field_parsers/store.rb', line 25

def process_remaining_json_fields
  fields_hash.dup.each do |key, field|
    fields_hash.delete(key) if json_field?(field)
  end
end

#process_stored_attributesObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/para/model_field_parsers/store.rb', line 11

def process_stored_attributes
  model.stored_attributes.each do |store_key, field_names|
    fields_hash.delete(store_key)

    field_names.each do |field_name|
      fields_hash[field_name] = AttributeField::Base.new(
        model, name: field_name, type: :string, searchable: false
      )
    end
  end
end