Class: Undo::Serializer::ActiveModel::Connector
- Inherits:
-
Object
- Object
- Undo::Serializer::ActiveModel::Connector
- Defined in:
- lib/undo/serializer/active_model/connector.rb
Instance Method Summary collapse
- #associate(object, association, associations) ⇒ Object
- #constantize(class_name) ⇒ Object
- #get_option(name, options) ⇒ Object
- #initialize_object(meta) ⇒ Object
- #persist(object, object_meta) ⇒ Object
- #primary_key_attributes(object, attributes) ⇒ Object
- #serialize_attributes(object) ⇒ Object
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, ) .fetch name.to_sym do .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() object_class = constantize get_option :class_name, pk_attributes = get_option :pk_attributes, 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, ) persister.call object unless [false, nil, 0, "false"].include? get_option :persisted, 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 |