Sha256: cafd8449659c1418a9466ea4f404c88af33b2433f0ef22b0495cf3254b98979d

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

module ActsAsAmico
  module AmicoObject

    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::AmicoObject::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

1 entries across 1 versions & 1 rubygems

Version Path
acts_as_amico-0.2.0 lib/acts_as_amico/amico_object.rb