module Extendible::ClassMethods

Public Instance Methods

extendible(*children) click to toggle source
# File lib/extendible.rb, line 38
def extendible(*children)
  # iterate through all obects that are supposed to be extendible
  children.each do |child_sym|
    attributes child_sym
    define_method child_sym do
      # nil if child is nonexistent
      return nil if object.try(child_sym).nil?
      if extended_objects.has_key?(child_sym) # params[:extend] includes child, extend attributes
        if extended_objects[child_sym].nil? # no attributes are specified, extend with all
          perform_serialization(object, child_sym).as_json
        else # only extend with attributes provided with dot notation
          serialized_object = perform_serialization(object, child_sym)
          mapper = (serialized_object.class < ActiveModel::Serializer) ? :to_sym : :to_s
          serialized_object.attributes.extract!(*extended_objects[child_sym].map(&mapper))            
        end
      else # child is not in params[:extend], only include id
        object.try(child_sym).attributes.extract!('id')
      end
    end
  end
end