Class: Undo::Serializer::ActiveModel::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/undo/serializer/active_model/connector.rb

Instance Method Summary collapse

Instance Method Details

#associate(object, association, associations) ⇒ Object



40
41
42
# File 'lib/undo/serializer/active_model/connector.rb', line 40

def associate(object, association, associations)
  associator.call object, association, associations
end

#constantize(class_name) ⇒ Object



48
49
50
# File 'lib/undo/serializer/active_model/connector.rb', line 48

def constantize(class_name)
  class_name.split('::').inject(Kernel) { |object, name| object = object.const_get(name); object }
end

#get_option(name, options) ⇒ Object



52
53
54
55
56
# File 'lib/undo/serializer/active_model/connector.rb', line 52

def get_option(name, options)
  options.fetch name.to_sym do
    options.fetch name.to_s
  end
end

#initialize_object(meta) ⇒ Object



29
30
31
32
33
34
# File 'lib/undo/serializer/active_model/connector.rb', line 29

def initialize_object(meta)
  object_class = constantize get_option :class_name, meta
  pk_attributes = get_option :pk_attributes, meta

  object_initializer.call object_class, pk_attributes
end

#persist(object, object_meta) ⇒ Object



36
37
38
# File 'lib/undo/serializer/active_model/connector.rb', line 36

def persist(object, object_meta)
  persister.call object unless [false, nil, 0, "false"].include? get_option :persisted, object_meta
end

#primary_key_attributes(object, attributes) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/undo/serializer/active_model/connector.rb', line 21

def primary_key_attributes(object, attributes)
  fields = Array(primary_key_fetcher.call(object)).map(&:to_sym)

  fields.each_with_object({}) do |field, pk_attributes|
    pk_attributes[field] = attributes[field] || attributes[field.to_s]
  end
end

#serialize_attributes(object) ⇒ Object



44
45
46
# File 'lib/undo/serializer/active_model/connector.rb', line 44

def serialize_attributes(object)
  attribute_serializer.call(object) || {}
end