Sha256: 6b1455421f1153f86e911a7e687163c38b86dd42163d08967fca6ca28dfe15d6

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

module ActsAsAmico
  module AmicoUser

    def self.included(base)
      base.extend ClassMethods
    end

    module ClassMethods

      def amico_key
        @amico_key ||= "id"
      end
      def amico_key= value
        @amico_key = value
      end

      def acts_as_amico *args
        options = args.extract_options!
        options.assert_valid_keys(:amico_key)
        @amico_key = options[:amico_key] ? options[:amico_key] : "id"
        include ActsAsAmico::AmicoUser::InstanceMethods
      end
    end

    module InstanceMethods

      def method_missing(sym, *args, &block)
        if Amico.respond_to? sym
          args[0] = args[0].send(args[0].class.amico_key) if not args[0].nil? and args[0].respond_to?(:id)
          args.unshift(self.send(self.class.amico_key))
          if sym.nil?
            Amico.send(*args, &block)
          else
            Amico.send(sym, *args, &block)
          end
        else
          super
        end
      end

      def respond_to?(sym)
        pass_sym_to_amico(sym) || super(sym)
      end

      # Named destructive methods
      def follow! obj, *args
        Amico.follow(self.send(self.class.amico_key), obj.send(obj.class.amico_key), *args)
      end
      def unfollow! obj, *args
        Amico.unfollow(self.send(self.class.amico_key), obj.send(obj.class.amico_key), *args)
      end
      def accept! obj, *args
        Amico.accept(self.send(self.class.amico_key), obj.send(obj.class.amico_key), *args)
      end
      def block! obj, *args
        Amico.block(self.send(self.class.amico_key), obj.send(obj.class.amico_key), *args)
      end
      def unblock! obj, *args
        Amico.unblock(self.send(self.class.amico_key), obj.send(obj.class.amico_key), *args)
      end

      private

      def pass_sym_to_amico sym
        Amico.respond_to? sym
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_amico-0.1.1 lib/acts_as_amico/amico_user.rb
acts_as_amico-0.1.0 lib/acts_as_amico/amico_user.rb