Class: Swivel::List

Inherits:
Response show all
Defined in:
lib/swivel.rb

Overview

Encapsulates lists of resources. Typically, items contained within the list are subclasses of Swivel::Response.

data_sets = swivel.call '/data_sets'
data_sets.class # => Swivel::List
data_sets.collect do |d| d.class end # => [Swivel::DataSet, Swivel::DataSet, ...]

users = swivel.call '/users'
users.class # => Swivel::List
users.collect do |u| u.class end # => [Swivel::User, Swivel::User, ...]

Instance Attribute Summary collapse

Attributes inherited from Response

#disable_auto_refresh, #hash_doc, #refreshed_at

Instance Method Summary collapse

Methods inherited from Response

#[], #id, resource, #to_param, #to_s, #to_xml

Constructor Details

#initialize(xml, connection, hashdoc, resource, count, total) ⇒ List

Instantiate a new Swivel::List. Calls super, then does a bit more extra processing.



577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/swivel.rb', line 577

def initialize xml, connection, hashdoc, resource, count, total
  super(xml, connection, hashdoc)
  unless @processed
    @count = count.nil? ? 0 : count.to_i 
    @total = total.nil? ? 0 : total.to_i
    @list = Array.new
    @hash_doc ||= []
    @hash_doc = [@hash_doc] if !@hash_doc.is_a?(Array)
    @hash_doc.each do |h|
      @list << Response.class_for(resource).new(nil, @connection, h)
    end
    @processed = true
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegates methods to the underlying Array instance. Allows you to call Array methods on a Swivel::List.

data_sets = swivel.call '/data_sets'
# try some Array methods...
data_sets.length.is_a? Integer # => true
data_sets.first.is_a? Swivel::DataSet #=> true


610
611
612
613
614
# File 'lib/swivel.rb', line 610

def method_missing method_id, *args, &block
  @list.send method_id, *args, &block
rescue NoMethodError => e
  super
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



574
575
576
# File 'lib/swivel.rb', line 574

def count
  @count
end

#resourceObject

Returns the value of attribute resource.



574
575
576
# File 'lib/swivel.rb', line 574

def resource
  @resource
end

#totalObject

Returns the value of attribute total.



574
575
576
# File 'lib/swivel.rb', line 574

def total
  @total
end

Instance Method Details

#refresh!(force = false) ⇒ Object



592
593
594
# File 'lib/swivel.rb', line 592

def refresh! force = false
  self # don't call refresh! on a list
end

#to_aObject

Get the underlying Array for this Swivel::List object.



598
599
600
# File 'lib/swivel.rb', line 598

def to_a 
  @list
end