Sha256: 53c52e3b202f8f451d4db6bd40689312471e7c5de5008b0cff2b93231c799a93

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

require 'vk/post'

require 'delegate'

module Vk
  class Post
    class Wall
      extend ActiveSupport::Memoizable

      attr_accessor :uid, :count, :posts

      def initialize(uid, count, posts)
        self.uid, self.count, self.posts = uid, count, posts
      end

      def user
        Vk::User.find(uid)
      end
      memoize :user

      def [](index)
        raise "Post #{index} is not exist"  if index > count + 1
        load_posts_to(index)                if index >= loaded_posts
        if post = posts[index]
          Vk::Post.find(Vk::Post.id_for(post), data: post)
        end
      end

      def first; self[0]; end

      def each(&block)
        load_all_posts
        0.upto(count) do |index|
          block.call(self[index])
        end
      end

      def method_missing(method, *args)
        if posts.respond_to?(method)
          posts.send(method, *args)
        else
          super
        end
      end

      protected

      def load_post(index)
        count, *new_posts = Vk::Base.loader.get_wall(uid, offset: loaded_posts, count: index - loaded_posts + 1)
        self.posts += new_posts
        posts[index]
      end

      def load_posts_to(count)
        load_post(loaded_posts + 100) while loaded_posts < count
      end

      def load_all_posts
        load_posts_to(count)
      end

      def loaded_posts
        posts.size
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
vk-0.0.4 lib/vk/post/wall.rb
vk-0.0.3 lib/vk/post/wall.rb