module GlobalRegistry::Bindings::Model::Entity

Public Instance Methods

entity_attributes_to_push() click to toggle source
# File lib/global_registry_bindings/model/entity.rb, line 9
def entity_attributes_to_push
  entity_attributes = entity_columns_to_push.map do |name, type|
    value_for_global_registry(name, type)
  end.compact.to_h
  entity_attributes[:client_integration_id] = id unless global_registry_entity.exclude
                                                                              .include?(:client_integration_id)
  if respond_to?(:updated_at) && updated_at.present?
    entity_attributes[:client_updated_at] = updated_at.to_s(:db)
  end
  if global_registry_entity.parent_is_self?
    entity_attributes[:parent_id] = global_registry_entity.parent_id_value
  end
  entity_attributes
end
entity_columns_to_push() click to toggle source
# File lib/global_registry_bindings/model/entity.rb, line 40
def entity_columns_to_push
  @columns_to_push ||= if global_registry_entity.include_all_columns?
                         self
                           .class
                           .columns
                           .collect do |c|
                           { c.name.underscore.to_sym => normalize_entity_column_type(c.type, c.name) }
                         end # rubocop:disable Style/MultilineBlockChain
                           .reduce(&:merge)
                           .reject { |k, _v| global_registry_entity.exclude.include? k }
                           .merge(global_registry_entity.fields)
                       else
                         global_registry_entity.fields
                       end
end
value_for_global_registry(name, type) click to toggle source
# File lib/global_registry_bindings/model/entity.rb, line 24
def value_for_global_registry(name, type)
  value = send(name)
  return [name, value] if value.nil?
  value = case type
          when :datetime, :date
            value.to_s(:db)
          when :boolean
            value ? 'true' : 'false'
          else
            value.to_s.strip
          end
  [name, value]
rescue ::NoMethodError
  nil
end

Protected Instance Methods

normalize_entity_column_type(type, name) click to toggle source
# File lib/global_registry_bindings/model/entity.rb, line 58
def normalize_entity_column_type(type, name)
  if type.to_s == 'text'
    :string
  elsif name.ends_with?('_id')
    :uuid
  else
    type
  end
end