Module: SocialStream::Models::Subtype

Extended by:
ActiveSupport::Concern
Includes:
ActivityStreams::Subtype
Defined in:
lib/social_stream/models/subtype.rb

Overview

Common methods for models that have a 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 Subtype

Defined Under Namespace

Modules: ActiveRecord, ClassMethods

Instance Method Summary collapse

Methods included from ActivityStreams::Subtype

#as_object_type

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Delegate missing methods to supertype, if they exist there



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# 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

Instance Method Details

#_delegate_to_supertype?(method) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# 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

#respond_to?(*args) ⇒ Boolean

SocialStream::Models::Supertype handles some methods

Returns:

  • (Boolean)


83
84
85
# File 'lib/social_stream/models/subtype.rb', line 83

def respond_to? *args
  super || _delegate_to_supertype?(:method) && supertype!.respond_to?(*args)
end