Class: Vk::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vk/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, items_class, options = {}, merged_attributes = {}) ⇒ Result

Returns a new instance of Result.

Examples:

Vk::Result.new('database.getCountries', Vk::Country, options)


7
8
9
10
11
12
13
14
15
16
17
# File 'lib/vk/result.rb', line 7

def initialize(method, items_class, options = {}, merged_attributes = {})
  @items = []
  @amount = 0
  @client = options.delete(:client) { Vk.client }
  @items_class = items_class
  @method = method
  @options = options
  @options[:offset] ||= 0
  @options[:count] ||= 100
  @merged_attributes = merged_attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments, &block) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/vk/result.rb', line 52

def method_missing(name, *arguments, &block)
  if respond_to_missing?(name, false)
    @items_class.public_send(name, *arguments, &block)
  else
    super
  end
end

Instance Attribute Details

#items<Vk::Base> (readonly)

Returns:



31
32
33
# File 'lib/vk/result.rb', line 31

def items
  @items
end

Instance Method Details

#all<Vk::Base>

Returns:



44
45
46
47
48
49
50
# File 'lib/vk/result.rb', line 44

def all
  while amount && count && amount < count
    increase_offset!
    load_items
  end
  @items
end

#amountFixnum

Returns:

  • (Fixnum)


26
27
28
# File 'lib/vk/result.rb', line 26

def amount
  @items.size
end

#countFixnum

Returns:

  • (Fixnum)


20
21
22
23
# File 'lib/vk/result.rb', line 20

def count
  load_items unless @count
  @count
end

#each(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/vk/result.rb', line 33

def each(&block)
  @items.each do |item|
    block.call(item)
  end
  while amount && count && amount < count
    increase_offset!
    load_items(&block)
  end
end

#respond_to_missing?(name, include_all) ⇒ Boolean

Parameters:

  • name (String)
  • include_all (Boolean)

Returns:

  • (Boolean)


62
63
64
# File 'lib/vk/result.rb', line 62

def respond_to_missing?(name, include_all)
  @items_class.respond_to?(name, include_all)
end