module SocialStream::Models::Subtype

Common methods for models that have a {SocialStream::Models::Supertype}

Examples of subtypes are {User} and {Group}, which have {Actor} as supertype, or {Post} and {Comment}, which have {ActivityObject} as supertype

Methods are documented taking User as example of {SocialStream::Models::Subtype}

Attributes

supertype_name[R]
supertype_options[R]

Public Instance Methods

_delegate_to_supertype?(method) click to toggle source
# File lib/social_stream/models/subtype.rb, line 87
def _delegate_to_supertype?(method)
  # These methods must not be delegated to avoid loops
  # (the @supertype_name association (e.g. :actor) calls here again)
  exceptions = [ "_#{ self.class.supertype_foreign_key }".to_sym ] # [ :_actor_id ]

  ! exceptions.include?(method)
end
method_missing(method, *args, &block) click to toggle source

Delegate missing methods to supertype, if they exist there

Calls superclass method
# File lib/social_stream/models/subtype.rb, line 52
      def method_missing(method, *args, &block)
        super
      rescue NameError => subtype_error
        raise subtype_error unless _delegate_to_supertype?(:method)

        begin
          res = supertype!.__send__(method, *args, &block)

          # Cache method
          self.class.class_eval <<-STR, __FILE__, __LINE__ + 1
            def #{ method } *args, &block
              supertype!.__send__ :#{ method }, *args, &block
            end
          STR

          res

        # We rescue supertype's NameErrors so methods not defined are raised from
        # the subtype. Example: user.foo should raise "foo is not defined in user"
        # and not "in actor"
        rescue NameError => supertype_error
          if supertype_error.name == subtype_error.name &&
               supertype_error.message =~ /#{ self.class.supertype_name.to_s.classify }/
            raise subtype_error
          else
            raise supertype_error
          end
        end
      end
respond_to?(*args) click to toggle source

{SocialStream::Models::Supertype} handles some methods

Calls superclass method
# File lib/social_stream/models/subtype.rb, line 83
def respond_to? *args
  super || _delegate_to_supertype?(:method) && supertype!.respond_to?(*args)
end